我正在尝试编译一些 CUDA,并且希望显示编译器警告。相当于:
g++ fish.cpp -Wall -Wextra
Run Code Online (Sandbox Code Playgroud)
除了 NVCC 不理解这些,你必须通过它们:
nvcc fish.cu --compiler-options -Wall --compiler-options -Wextra
nvcc fish.cu --compiler-options "-Wall -Wextra"
Run Code Online (Sandbox Code Playgroud)
(我喜欢后一种形式,但最终,这并不重要。)
鉴于此 CMakeLists.txt (一个非常精简的示例):
cmake_minimum_required(VERSION 3.9)
project(test_project LANGUAGES CUDA CXX)
list(APPEND cxx_warning_flags "-Wall" "-Wextra") # ... maybe others
add_compile_options("$<$<COMPILE_LANGUAGE:CUDA>:--compiler-options ${cxx_warning_flags}>")
add_executable(test_cuda fish.cu)
Run Code Online (Sandbox Code Playgroud)
但这扩展到:
nvcc "--compiler-options -Wall" -Wextra ...
Run Code Online (Sandbox Code Playgroud)
这显然是错误的。(省略生成器表达式周围的引号只会让我们陷入破碎的扩展地狱。)
...跳过蒙特卡洛编程的数千次迭代...
我已经到达了这个宝石:
set( temp ${cxx_warning_flags} )
string (REPLACE ";" " " temp "${temp}")
set( temp2 "--compiler-options \"${temp}\"" )
message( "${temp2}" )
Run Code Online (Sandbox Code Playgroud)
打印出看起来令人鼓舞的
--compiler-options "-Wall -Wextra"
Run Code Online (Sandbox Code Playgroud)
但是之后
add_compile_options("$<$<COMPILE_LANGUAGE:CUDA>:${temp2}>")
Run Code Online (Sandbox Code Playgroud)
扩展到:
nvcc "--compiler-options \"-Wall …Run Code Online (Sandbox Code Playgroud) 在 Windows 批处理文件中,如果我启用了回显(默认),我可以通过添加前缀来禁用回显单个命令@,例如:
some_command_that_will_be_echoed
@some_command_that_wont_be_echoed
Run Code Online (Sandbox Code Playgroud)
但是,我已禁用回显@echo off,但有时我想回显该命令。有什么神奇的等价物吗?例如:
@echo off
some_command_that_wont_be_echoed
<unknown_magic_prefix> some_command_that_will_be_echoed
Run Code Online (Sandbox Code Playgroud)
我发现了这个问题:暂时打开回声,但这基本上是说“打开回声,做你的事,然后再次关闭它”。我可以这样做,但我希望有更优雅的东西。
编辑:此Windows 批处理文件:如何启用相关问题列表中出现的命令的内联回显;这基本上和我的问题一样,而且成功了!
EDIT2:为了提供上下文,下面是我要做的部分。请注意,为了这篇文章,我已经用 C 风格的评论装饰了它;他们不在真正的剧本中。
@echo off // Because I don't want the bulk of the commands displayed.
// (In fact it's worse than that, this file is called from another
// with echo already disabled.)
title CMD Window with SVN enabled
set PATH= ... // my new path which happens to include …Run Code Online (Sandbox Code Playgroud) 我有一个 CMake 文件,用于在 Windows 上构建一些 CUDA (NVCC/MSVC)。我正在尝试使用以下命令将 MSVC 警告级别设置为/W4:
add_compile_options("$<$<COMPILE_LANGUAGE:CUDA>:--compiler-options=/W4>")
Run Code Online (Sandbox Code Playgroud)
使用 CMake 3.9 构建时,我收到以下警告:
(CudaBuildCore target) ->
cl : Command line warning D9025: overriding '/W4' with '/W3'
Run Code Online (Sandbox Code Playgroud)
在 CMake 3.15 中,该策略已更改为不会/W3在 CUDA 标志中自动设置,但在该版本中我得到:
(CudaBuildCore target) ->
cl : Command line warning D9025: overriding '/W4' with '/W1'
Run Code Online (Sandbox Code Playgroud)
如果我执行构建步骤,--verbose我会在 3.9 中看到以下内容:
"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin\nvcc.exe" -gencode=arch=compute_30,code=\"compute_30,compute_30\" --use-local-env -ccbin "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x64" -x cu -IC:\Users\eddi\Documents\temp\thur\sw\include -IC:\Users\eddi\Documents\temp\thur\sw\shared\common\include -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\include" --keep-dir x64\Debug -maxrregcount=0 …Run Code Online (Sandbox Code Playgroud) 我正在编写一些可以编译为 C++ 或 CUDA 的代码。在后一种情况下,它使用 CUDA 内核,在前一种情况下,它只运行常规代码。
创建了一个名为 test.cpp 的文件后,我可以手动编译它:
g++ test.cpp # build as C++ with GCC
nvcc -x cu test.cpp # build as CUDA with NVCC
Run Code Online (Sandbox Code Playgroud)
在哪里 -x cu告诉 nvcc 虽然它是一个 .cpp 扩展名,但我希望它把它当作 CUDA。到现在为止还挺好。
但是,当我迁移到使用 CMake 时,我不知道如何做同样的事情。即:如何要求 CMake 使用 NVCC 而不是 GCC 编译 .cpp 文件。
cmake_minimum_required(VERSION 3.9)
project(cuda_test LANGUAGES CUDA CXX)
add_executable(cuda_test test.cpp) # builds with GCC
Run Code Online (Sandbox Code Playgroud)
如果我创建指向原始文件的符号链接:
cmake_minimum_required(VERSION 3.9)
project(cuda_test LANGUAGES CUDA CXX)
add_executable(cuda_test test.cpp) # builds with GCC
Run Code Online (Sandbox Code Playgroud)
然后更改 CMakeLists.txt:
add_executable(cuda_test test.cu) # builds with …Run Code Online (Sandbox Code Playgroud) 在我的课程幻灯片中,我有这个例子,但没有太多解释:
char*f="char*f=%c%s%c;main(){printf(f,34,f,34,10);}%c";main(){printf(f,34,f,34,10);}
Run Code Online (Sandbox Code Playgroud)
我理解quine 程序的一般含义,但我不太明白上面的代码中发生了什么。这是我运行它时得到的输出:
char*f="char*f=%c%s%c;main(){printf(f,34,f,34,10);}%c";main(){printf(f,34,f,34,10);}
Run Code Online (Sandbox Code Playgroud)
但它如何复制自己的代码呢?我真的不明白输出是如何产生的。