1

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

Arul
  • 303
  • 1
  • 5
  • 16
  • There are many target properties that *may* contain the flags you want to remove. Have you tried `LINK_FLAGS` or `LINK_OPTIONS`? There is a comprehensive list of target properties [here](https://cmake.org/cmake/help/latest/manual/cmake-properties.7.html#properties-on-targets). Perhaps, they are in `INTERFACE_LINK_OPTIONS`? – Kevin Nov 14 '19 at 12:38

2 Answers2

4

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.

Edd Inglis
  • 1,067
  • 10
  • 22
  • Thanks for the suggestions. Unfortunately, I am not setting these flags myself in my CMakeLists.txt file. I am inheriting these from the root CMakeLists.txt and I am trying not to tamper with it. – Arul Nov 14 '19 at 12:26
2

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.

Luisjomen2a
  • 241
  • 2
  • 16
  • 1
    Thanks for pointing out ```CUDA_PROPAGATE_HOST_FLAGS```. It helps to remove compiler flags (I don't need ```get_target_property``` to remove ```fdiagnostics-color``` anymore). Unfortunately, it doesn't do any change to the linker flags and my issue still exists. – Arul Nov 14 '19 at 11:12