# How to edit rpath flag format for linking library

**URL:** <https://discourse.cmake.org/t/how-to-edit-rpath-flag-format-for-linking-library/7896>\
**Category:** Code\
**Tags:** os:linux\
**Created:** [April 12, 2023, 8:17pm UTC](https://discourse.cmake.org/t/how-to-edit-rpath-flag-format-for-linking-library/7896 "2023-04-12T20:17:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![agustin-vaca](https://discourse.cmake.org/user_avatar/discourse.cmake.org/agustin-vaca/32/3332_2.png) [@agustin-vaca](https://discourse.cmake.org/u/agustin-vaca)\
**Post date:** [April 12, 2023, 8:17pm UTC](https://discourse.cmake.org/t/how-to-edit-rpath-flag-format-for-linking-library/7896/1 "2023-04-12T20:17:16Z")

</div>

I’m building a project that uses the CUDA Runtime library, as well as some tests for the project using CTest. Here is an example to illustrate my problem.

**Directory structure:**

```
Dir/
├── CMakeLists.txt
├── main.c
├── main.h
├── kernel.cu
└── kernel.h
├──tests/
    ├──CMakeLists.txt
    ├──test1.c

```

**Environment:**

NVCC: 12.1

CMake: 3.25

**kernel.h**

> void f()

**kernel.cu**

```
> extern "C" {

```

> ```
> #include "kernel.h"
> }
> 
> extern "C" void f() {
> //implementation
> //use cuda functions
> }
> 
> ```

**main.c**

```
> #include "kernel.h"

```

> ```
> void fmain(){
> f();
> }
> 
> ```
> 
> int main() {  
> return 0;  
> }

**main.h**

> void fmain();

**CMakeLists.txt**

```
> cmake_minimum_required(VERSION 3.18)

```

> ```
> if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
> set(CMAKE_CUDA_ARCHITECTURES 70)
> endif()
> 
> project(test LANGUAGES C CXX CUDA)
> 
> add_library(cuda_comp SHARED kernel.h kernel.cu)
> 
> set_target_properties(cuda_comp
> PROPERTIES
> CUDA_RUNTIME_LIBRARY Shared
> )
> 
> add_executable(main main.c)
> 
> target_link_libraries(main cuda_comp)
> 
> mark_as_advanced( BUILD_TESTS )
> set( BUILD_TESTS true CACHE BOOL "Tests build target available if true" )
> if( BUILD_TESTS )
> enable_testing()
> add_subdirectory( tests )
> endif()
> 
> ```

This main target builds with no problem, however, when linking the tests I get an unresolved reference to `f()`.

**tests/test1.c**

```
> #include "main.h"

```

> ```
> int main() {
> fmain(); //fmain() calls f()
> return 0;
> }
> 
> ```

**tests/CMakeLists.txt**

```
> add_executable(test1 test1.c)

```

> ```
> add_test( NAME "_test1" COMMAND "test1" )
> 
> ```

After adding `target_link_libraries(test1 PRIVATE cuda_comp)` , therefore having:

**tests/CMakeLists.txt**

```
> add_executable(test1 test1.c)

```

> ```
> target_link_libraries(test1 PRIVATE cuda_comp)
> add_test( NAME "_test1" COMMAND "test1" )
> 
> ```

I get a compiler flag error, this is the relevant output:

```
*Linking C executable test1*

```

- test1 -Wl,-rpath,/pathToMainDirectory -lm …/libcuda\_comp.so \*

- nvcc fatal : Unknown option ‘-Wl,-rpath,/pathToMainDirectory’\*

I know that the -WL,-rpath format is incorrect for NVCC. How can I correct the format of the rpath?

---

<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:** [April 24, 2023, 5:07am UTC](https://discourse.cmake.org/t/how-to-edit-rpath-flag-format-for-linking-library/7896/2 "2023-04-24T05:07:30Z")

</div>

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:** [April 24, 2023, 3:30pm UTC](https://discourse.cmake.org/t/how-to-edit-rpath-flag-format-for-linking-library/7896/3 "2023-04-24T15:30:24Z")

</div>

Can you please post the verbose build output.

Locally and in our CI, CMake passes the correct flags for rpath addition for nvcc.

---

<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:** [April 24, 2023, 5:20pm UTC](https://discourse.cmake.org/t/how-to-edit-rpath-flag-format-for-linking-library/7896/4 "2023-04-24T17:20:17Z")

</div>

> [@agustin-vaca](#):
>
> nvcc fatal : Unknown option ‘-Wl,-rpath,/pathToMainDirectory’\*

The `test1` executable is a `C` program which means that your C compiler should be doing the linking, and not `nvcc` which is only a `CUDA` compiler.
