# Setting linker prefix for CMAKE\_BUILD\_RPATH

**URL:** <https://discourse.cmake.org/t/setting-linker-prefix-for-cmake-build-rpath/8981>\
**Category:** Code\
**Created:** [September 15, 2023, 5:28am UTC](https://discourse.cmake.org/t/setting-linker-prefix-for-cmake-build-rpath/8981 "2023-09-15T05:28:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![torrance](https://discourse.cmake.org/user_avatar/discourse.cmake.org/torrance/32/3831_2.png) [@torrance](https://discourse.cmake.org/u/torrance)\
**Post date:** [September 15, 2023, 5:28am UTC](https://discourse.cmake.org/t/setting-linker-prefix-for-cmake-build-rpath/8981/1 "2023-09-15T05:28:14Z")

</div>

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

```auto
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:

```auto
set(CMAKE_CXX_LINKER_WRAPPER_FLAG "-Xlinker" " ")

```

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

```auto
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`?**

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [September 15, 2023, 3:38pm UTC](https://discourse.cmake.org/t/setting-linker-prefix-for-cmake-build-rpath/8981/2 "2023-09-15T15:38:38Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![torrance](https://discourse.cmake.org/user_avatar/discourse.cmake.org/torrance/32/3831_2.png) [@torrance](https://discourse.cmake.org/u/torrance)\
**Post date:** [September 16, 2023, 3:32am UTC](https://discourse.cmake.org/t/setting-linker-prefix-for-cmake-build-rpath/8981/3 "2023-09-16T03:32:42Z")

</div>

Thanks @ben.boeckel , if I set:

```auto
set(CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG "-Xlinker=-rpath,")

```

Then things do work.

> [@ben.boeckel](#):
>
> I suspect that `nvcc` is being detected as some other compiler and getting its settings for this instead of something more `nvcc`-specific.

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.

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [September 16, 2023, 12:15pm UTC](https://discourse.cmake.org/t/setting-linker-prefix-for-cmake-build-rpath/8981/4 "2023-09-16T12:15:07Z")

</div>

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

Cc: @robert.maynard

---

<div class="post-metadata">

**Author:** ![robert.maynard](https://discourse.cmake.org/user_avatar/discourse.cmake.org/robert.maynard/32/4_2.png) [@robert.maynard](https://discourse.cmake.org/u/robert.maynard)\
**Post date:** [September 18, 2023, 12:27pm UTC](https://discourse.cmake.org/t/setting-linker-prefix-for-cmake-build-rpath/8981/5 "2023-09-18T12:27:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![torrance](https://discourse.cmake.org/user_avatar/discourse.cmake.org/torrance/32/3831_2.png) [@torrance](https://discourse.cmake.org/u/torrance)\
**Post date:** [September 18, 2023, 12:35pm UTC](https://discourse.cmake.org/t/setting-linker-prefix-for-cmake-build-rpath/8981/6 "2023-09-18T12:35:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![robert.maynard](https://discourse.cmake.org/user_avatar/discourse.cmake.org/robert.maynard/32/4_2.png) [@robert.maynard](https://discourse.cmake.org/u/robert.maynard)\
**Post date:** [September 18, 2023, 1:14pm UTC](https://discourse.cmake.org/t/setting-linker-prefix-for-cmake-build-rpath/8981/7 "2023-09-18T13:14:42Z")

</div>

That would break things like [CMAKE\_CUDA\_RUNTIME\_LIBRARY — CMake 3.27.5 Documentation](https://cmake.org/cmake/help/latest/variable/CMAKE_CUDA_RUNTIME_LIBRARY.html)

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.
