我有一个CMake文件,用于在Windows上构建一些CUDA(NVCC / MSVC)。我正在尝试将MSVC警告级别设置为/ W4
,方法如下:
add_compile_options("$<$<COMPILE_LANGUAGE:CUDA>:--compiler-options=/W4>")
在使用CMake 3.9构建时,我遇到了以下警告:
(CudaBuildCore target) ->
cl : Command line warning D9025: overriding '/W4' with '/W3'
CMake 3.15版本更改了策略,不再自动将 /W3
设置为CUDA标志,但使用该版本我会得到以下结果:
(CudaBuildCore target) ->
cl : Command line warning D9025: overriding '/W4' with '/W1'
如果我使用--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 --machine 64 --compile -cudart static --compiler-options=/W4 --compiler-options= -Xcompiler="/EHsc -Zi -Ob0" -g -D_WINDOWS -D"FISH_BUILD_TYPE=\"DEBUG\"" -D"CMAKE_INTDIR=\"Debug\"" -D"CMAKE_INTDIR=\"Debug\"" -D_MBCS -Xcompiler "/EHsc /W3 /nologo /Od /Fdfish.dir\Debug\fish.pdb /FS /Zi /RTC1 /MDd /GR" -o fish.dir\Debug\fish_cuda.obj "C:\Users\eddi\Documents\temp\thur\sw\fish_cuda.cpp"
同时,随着3.15版本的发布:
"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 --machine 64 --compile -cudart static --compiler-options=/W4 --compiler-options= -Xcompiler="/EHsc -Zi -Ob0" -g -D_WINDOWS -D"FISH_BUILD_TYPE=\"DEBUG\"" -D"CMAKE_INTDIR=\"Debug\"" -D"CMAKE_INTDIR=\"Debug\"" -D_MBCS -Xcompiler "/EHsc /W1 /nologo /Od /Fdfish.dir\Debug\fish.pdb /FS /Zi /RTC1 /MDd /GR" -o fish.dir\Debug\fish_cuda.obj "C:\Users\eddi\Documents\temp\thur\sw\fish_cuda.cpp"
Spoiler:这两者是完全相同的,除了
-Xcompiler "/EHsc /W3 /nologo /Od /Fdfish.dir\Debug\fish.pdb /FS /Zi /RTC1 /MDd /GR"
这个部分。尽管我已经尽力了,但我无法找到引入这些标志的地方(由 CMake 等引入),因此无法开始解决如何改变行为。
编辑1:为故事增加颜色... 我在我的 CMake 安装中使用 grep 命令查找
/nologo
并找到了几个包含以下行的 .cmake
文件: 35: set(CMAKE_CL_NOLOGO "/nologo")
我把它们全部改成了变体。
35: set(CMAKE_CL_NOLOGO "/nologo_EDD")
但是在这一混乱的选项集合中,它们都没有出现。所以要么我漏掉了什么,要么它们是从CMake之外的某个东西引入的?
编辑2: @squareskittles说:
很好,我可以理解这种逻辑:它简单明了,并且直接解决了我的即时问题。但这似乎有点生硬,且没有解决第一次产生这些标志的地方。你可以从CMAKE_CXX_FLAGS_INIT变量中删除默认标志。
总之,撇开异议,我把这个不好看的片段添加到了我的脚本中:
message( "--------------------------- ${CMAKE_CXX_FLAGS_INIT}")
string (REGEX REPLACE "/W[0-4]" "" CMAKE_CXX_FLAGS_INIT "${CMAKE_CXX_FLAGS_INIT}")
message( "--------------------------- ${CMAKE_CXX_FLAGS_INIT}")
在CMake 3.9中,我看到:
--------------------------- /DWIN32 /D_WINDOWS /W3 /GR /EHsc
--------------------------- /DWIN32 /D_WINDOWS /GR /EHsc
如期望一样。但是在3.15版本中,我看到:
--------------------------- /DWIN32 /D_WINDOWS /GR /EHsc
--------------------------- /DWIN32 /D_WINDOWS /GR /EHsc
即使没有政策变化,也不存在/W3
标志(更不用说/W1
标志了)。但更糟糕的是,在两种情况下,主要问题仍然存在:
nvcc.exe ... --compiler-options=/W4 -Xcompiler="/EHsc -Zi -Ob0" ... -Xcompiler "/EHsc /W3 /nologo /Od /Fdfish.dir\Debug\fish.pdb /FS /Zi /RTC1 /MDd /GR" ...
编辑3:有了一些进展! 通过这个简单的改变:
#add_compile_options("$<$<COMPILE_LANGUAGE:CUDA>:--compiler-options=/W4>")
add_compile_options("$<$<COMPILE_LANGUAGE:CUDA>:-Xcompiler=/W4>")
没有更多的抱怨了!混淆的标记仍然存在,但现在的阅读方式是:
-Xcompiler "/EHsc /W4 /nologo /Od /Ffish.dir\Debug\fish.pdb
/FS /Zi /RTC1 /MDd /GR"
例如,CMake发现了/W4
并覆盖了其默认值。
但是,尽管仔细查看了CMake源代码,我仍然没有确定所有这些操作都在哪里完成。
编辑4:MSVC对所有这些都有一定的帮助。
这些“神秘”的选项是通过从.vcxproj
文件自动生成的。如果我使用-Xcompile=/W4
方法,则项目XML包含:
<CudaCompile>
...
<AdditionalOptions>%(AdditionalOptions) -Xcompiler="/EHsc -Zi -Ob1"</AdditionalOptions>
...
<Warning>W4</Warning>
</CudaCompile>
但是使用原始选项 --compiler-options=/W4
,我得到以下结果:
<CudaCompile>
<AdditionalOptions>%(AdditionalOptions) --compiler-options=/W4 -Xcompiler="/EHsc -Zi -Ob1"</AdditionalOptions>
...
</CudaCompile>
这一切都有些合理; 如果未指定警告级别,MSVC必须默认为W1,因此出现了警告。
此外,有一个线索是:构建步骤中有以下内容:
-Xcompiler="/EHsc -Zi -Ob0"
-Xcompiler "/EHsc /W1 /nologo /Od ..."
注意一个等号有,而另一个没有,这暗示它们来自不同的地方。
我认为目前我的调查已经足够深入了。
/W4
,并删除/W3
或/W1
,则可以按照链接答案中的建议从CMAKE_CXX_FLAGS_INIT
变量中删除默认标志。 - Kevin