Setting linker prefix for CMAKE_BUILD_RPATH

BUILD_RPATH is being automatically populated on one of my machines and during linking is output as:

nvcc [...] -o test  -Wl,-rpath,/usr/path/something:/usr/path/anotherthing [...]

(Yes, I’m linking with nvcc. And yes, I want this.)

The problem is cmake it prefixing the RPATH with -Wl which nvcc doesn’t understand. Instead, I want -Xlinker.

I have tried setting:


And while this properly works when I set, e.g.

target_link_options(test PRIVATE "LINKER:-lpthread")

However, it has no effect on the RPATH prefix.

How can I correctly set the RPATH option prefix from -Wl to -Xlinker?

It looks like CMAKE_SHARED_LIBRARY_RUNTIME_<LANG>_FLAG is what controls this. I suspect that nvcc is being detected as some other compiler and getting its settings for this instead of something more nvcc-specific.

Can you show what compiler it is being detected as in your setup?

Thanks @ben.boeckel , if I set:


Then things do work.

If I print the output of CMAKE_CXX_COMPILER_ID and CMAKE_CXX_COMPILER_VERSION, it shows that this is incorrectly being identified as GNU 12.1.0.

I thought this might have been because I the executable I was actually calling was hipcc (which can front either clang or nvcc depending on GPU setup), but even if I directly call out to nvcc it still thinks it’s GNU 12.1.0.

To set the compiler, I’m explicitly setting CMAKE_CXX_COMPILER to hipcc (or nvcc) prior to calling project(). I understood this was the right way, but I might be wrong.

I suspect we’d need a new ID for nvcc-as-C++ compiler.

Cc: @robert.maynard

Since nvcc is merely a wrapper around the host compiler, why not set the underlying host compiler as the C++ compiler?

Nvcc does some other magic including automatically linking in cuda libraries that allows me to avoid having to fuss about their locations from machine to machine. So I’m this case I’d still like to be able to call nvcc as the linker

That would break things like CMAKE_CUDA_RUNTIME_LIBRARY — CMake 3.27.5 Documentation

My recommendation is to use FindCUDAToolkit and specify the cuda runtime explicitly as that is the only CUDA library that nvcc will auto link too.