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?