CMake passes all gcc flags to nvcc as well

£可爱£侵袭症+ 提交于 2020-06-27 04:11:22

问题


My project uses cuda kernel for a small module and needs nvcc for compiling. During compilation, cmake pass the same linker and compiler flags intended for gcc to nvcc as well. In my particular case, I get the following error.

nvcc fatal   : Unknown option 'Wl,--no-as-needed'

Following the accepted answer in this thread, I managed to remove the compiler flags for the target that needs nvcc as follows.

get_target_property(_target_cxx_flags target_that_needs_nvcc COMPILE_OPTIONS)
list(REMOVE_ITEM _target_cxx_flags "-fcolor-diagnostics")

Using this, I avoided the errors due to wrong compiler flags like this:

nvcc fatal   : Unknown option 'fdiagnostics-color'

But I cannot use the same procedure to remove linker flags because get_target_property fetches only compiler flags and not linker flags.

I am looking for a solution to disable the linker flags for just one target compilation.

The cmake minimum version expected is VERSION 3.0


回答1:


I think what you are looking for is to turn off the propagation of flags from gcc to nvcc. Take a look at the option CUDA_PROPAGATE_HOST_FLAGS in the legacy cuda support variable in the find cuda module.




回答2:


An alternative to removing flags you don't want is to never add them in the first place. You can be language-specific using generator expressions. eg:

add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:${my_cxx_flags}>")
add_compile_options("$<$<COMPILE_LANGUAGE:CUDA>:${my_cuda_flags}>")

I realise you're asking about linker flags not compiler flags, but hopefully this might set you off in a useful direction.



来源:https://stackoverflow.com/questions/58854539/cmake-passes-all-gcc-flags-to-nvcc-as-well

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!