我正在试图编译一些CUDA,并希望显示编译器警告。相当于:
g++ fish.cpp -Wall -Wextra除非NVCC不理解这些,而且您必须通过它们:
nvcc fish.cu --compiler-options -Wall --compiler-options -Wextra
nvcc fish.cu --compiler-options "-Wall -Wextra"(我赞成后一种形式,但最终,这并不重要。)
考虑到这个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)但这一范围扩大到:
nvcc "--compiler-options -Wall" -Wextra ...这显然是错误的。(省略生成器表达式中的引号,只会让我们陷入崩溃的膨胀地狱。)
..。跳过几千次蒙特卡罗程序的迭代..。
我来到这颗宝石:
set( temp ${cxx_warning_flags} )
string (REPLACE ";" " " temp "${temp}")
set( temp2 "--compiler-options \"${temp}\"" )
message( "${temp2}" )上面印出了鼓舞人心的表情
--compiler-options "-Wall -Wextra"但是后来
add_compile_options("$<$<COMPILE_LANGUAGE:CUDA>:${temp2}>")扩展到:
nvcc "--compiler-options \"-Wall -Wextra\"" ...我茫然不知所措,难道我走到了死胡同吗?还是我错过了一些关键的标点符号组合?
发布于 2019-12-20 14:07:07
我正在回答我自己的问题,因为我找到了一些可行的解决方案,但我仍然有兴趣知道是否有更好的(阅读:更干净、更规范)的方法。
TL;博士
foreach(flag IN LISTS cxx_warning_flags)
add_compile_options("$<$<COMPILE_LANGUAGE:CUDA>:--compiler-options=${flag}>")
endforeach()逐笔帐户
我试过这个:
foreach(flag IN LISTS cxx_warning_flags)
add_compile_options("$<$<COMPILE_LANGUAGE:CUDA>:--compiler-options ${flag}>")
endforeach()但这仍然给了我们
nvcc "--compiler-options -Wall" "--compiler-options -Wextra"
nvcc fatal : Unknown option '-compiler-options -Wall'然而,增加一个临时的:
foreach(flag IN LISTS cxx_warning_flags)
set( temp --compiler-options ${flag}) # no quotes
add_compile_options("$<$<COMPILE_LANGUAGE:CUDA>:${temp}>")
endforeach()给出了新的结果:
nvcc --compiler-options -Wall -Wextra ...
nvcc fatal : Unknown option 'Wextra'我在这里假设的是,CMake正在组合重复的--compiler-options标志,但我只是推测。
所以,我试着用一个相等的词来消除空格:
foreach(flag IN LISTS cxx_warning_flags)
add_compile_options("$<$<COMPILE_LANGUAGE:CUDA>:--compiler-options=${flag}>")
endforeach()太棒了!我们有一个胜利者:
nvcc --compiler-options=-Wall --compiler-options=-Wextra ...我们不需要循环就行吗?
add_compile_options("$<$<COMPILE_LANGUAGE:CUDA>:--compiler-options=${cxx_warning_flags}>")不工作(--compiler-options=-Wall -Wextra),但是:
string (REPLACE ";" " " temp "${cxx_warning_flags}")
add_compile_options("$<$<COMPILE_LANGUAGE:CUDA>:--compiler-options=${temp}>")确实有效("--compiler-options=-Wall -Wextra")。
我对最后一个选择有点惊讶,但我想这是有道理的。总的来说,我认为循环方案的意图是最明确的。
编辑:在Confusing flags passed to MSVC through NVCC with CMake中,我花了很多时间发现使用:
add_compile_options("$<$<COMPILE_LANGUAGE:CUDA>:-Xcompiler=${flag}>")由于CMake似乎对标志进行了一些合理化,以消除重复和模糊,但并没有意识到--compiler-options与其青睐的-Xcompiler是一样的。
https://stackoverflow.com/questions/59425220
复制相似问题