I am using CMake 3.10 and have a problem linking a compiled library to a test executable in CMake. I searched a lot and found that in earlier versions there was a problem where you could not link intermediate libraries in the result executable. I was not able to tell if this was resolved or still an issue.
My CMake files look like this:
Algo:
cmake_minimum_required (VERSION 3.9)
project(${MODULE_NAME}_core LANGUAGES CXX CUDA)
add_subdirectory("${core_impl_dir}" implementation)
set(cuda_src "parallel/ParallelComputation.cu")
set(cuda_hdr "parallel/ParallelComputation.h")
add_library(${PROJECT_NAME} STATIC "${cuda_src}" "${cuda_hdr}"
)
target_include_directories (${PROJECT_NAME} PUBLIC "include/"
"parallel/"
)
source_group("parallel" FILES "${cuda_src}" "${cuda_hdr}")
set_property(TARGET ${PROJECT_NAME} PROPERTY FOLDER ${MODULE_NAME})
Test:
project(${MODULE_NAME}_gtest LANGUAGES CXX CUDA)
add_subdirectory("${gtest_impl_dir}" implementation)
add_executable(${PROJECT_NAME} "${gtest_impl_src}")
target_link_libraries(${PROJECT_NAME} ${MODULE_NAME}_core)
enable_testing()
find_package(GTest REQUIRED)
include_directories("${GTEST_INCLUDE_DIRS}")
target_link_libraries(${PROJECT_NAME} ${GTEST_BOTH_LIBRARIES})
source_group("Implementation\\Source Files" FILES "${gtest_impl_src}" )
set_property(TARGET ${PROJECT_NAME} PROPERTY FOLDER ${MODULE_NAME})
add_test(${PROJECT_NAME} ${PROJECT_NAME})
Building just Algo works fine, but when also building Test, I get linking errors, for example
../implementation/libmatrix1_testCuda_core.a(ParallelComputation.cu.o): In Funktion 'cudaError cudaMalloc(float**, unsigned long)': tmpxft_00005ad0_00000000-5_ParallelComputation.cudafe1.cpp:(.text+0x4f2): Undefined reference 'cudaMalloc'
EDIT
using make VERBOSE=1
I got this linking command:
/usr/bin/c++ -Wl,--no-as-needed -pthread -g -std=c++14 -Wall
CMakeFiles/matrix1_testCuda_gtest.dir//tests/eclipseProject/algos/testCuda/test/src/main.cpp.o CMakeFiles/matrix1_testCuda_gtest.dir/cmake_device_link.o -o matrix1_testCuda_gtest ../implementation/libmatrix1_testCuda_core.a /usr/lib/libgtest.a /usr/lib/libgtest_main.a
To use a different installed version of the toolkit set the environment variable CUDA_BIN_PATH before running cmake (e.g. CUDA_BIN_PATH=/usr/local/cuda1. 0 instead of the default /usr/local/cuda ) or set CUDA_TOOLKIT_ROOT_DIR after configuring.
I got this to work by calling
find_package(CUDA 9.0 REQUIRED)
in both CMake files. Also, in the Algo file (which contains the device code), I had to do
target_link_libraries(${PROJECT_NAME} ${CUDA_LIBRARIES})
I was expecting that the language support for CUDA would make those steps unnecessary, but apparently not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With