The following error message may be encountered when building CUDA code with CMake:
error LNK2019: unresolved external symbol __cudaRegisterLinkedBinary
It is caused by generated glue code not being linked in. The glue code is generated into files named with the project and a suffix:
myprojectname.device-link.obj
Ensure CUDA is enabled as a language for the project:
PROJECT(GPUProcessing LANGUAGES CUDA CXX)
Assuming you want separable compilation, you must enable resolving device symbols:
set_target_properties(${PROJECT_NAME}
PROPERTIES
CUDA_SEPARABLE_COMPILATION ON
CUDA_RESOLVE_DEVICE_SYMBOLS ON
)
Note that the CUDA C++ language will follow the default C++ language level, so if you enable C++17 you will need to specify C++14 for CUDA (as of 9.2) as it does not yet support C++17:
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CUDA_STANDARD 14)
Also the CUDAToolkit
package is the preferred and modern way to build with CUDA, thus:
find_package(CUDAToolkit)
add_executable(
binary_linking_to_cudart
my_cpp_file_using_cudart.cpp
)
target_link_libraries(binary_linking_to_cudart PRIVATE CUDA::cudart)
Useful links:
- https://stackoverflow.com/questions/41385346/error-undefined-reference-to-cudaregisterlinkedbinary-when-compiling-cuda-w
- https://cmake.org/pipermail/cmake/2014-July/058119.html
- https://gitlab.kitware.com/cmake/cmake/-/issues/17520
- https://gitlab.kitware.com/cmake/cmake/-/merge_requests/3491
- https://stackoverflow.com/questions/51756562/obtaining-the-cuda-include-dir-in-c-targets-with-native-cuda-support-cmake