Arul
Arul

Reputation: 303

CMake passes all gcc flags to nvcc as well

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

Upvotes: 1

Views: 1977

Answers (2)

Edd Inglis
Edd Inglis

Reputation: 1105

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.

Upvotes: 4

Luisjomen2a
Luisjomen2a

Reputation: 251

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.

Upvotes: 2

Related Questions