Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking of CUDA library in CMake

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

like image 236
kutschkem Avatar asked Jan 30 '18 17:01

kutschkem


People also ask

How do I specify Cuda path in CMake?

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.


1 Answers

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.

like image 66
kutschkem Avatar answered Oct 18 '22 18:10

kutschkem