Can't link to my CUDA static library with Dynamic Parallelism: unresolved external symbol __fatbinwrap_38_cuda_

I’m trying to create the most basic CUDA application to demonstrate Dynamic Parallelism, Separate Compilation and Linking, a CUDA kernel in a static library, and I’m trying to use CMake to generate a Visual Studio solution. I’m using CMake 3.21.3, CUDA 11.4, and Visual Studio 2019 (16.11.5).

I have a .h and a .cu file, which I’m compiling into a static library. I also have a main.cpp file which includes the header from my library and links to it. This file is compiled to an executable. The code for my library and for my executable are in separate folders, like this:

src
 |-MyLib
 |  |-mylib.h
 |  |-mylib.cu
 |  |-CMakeLists.txt
 |
 |-MyMain
 |  |-main.cpp
 |  |-CMakeLists.txt
 |
 |-CMakeLists.txt

mylib.h and mylib.cu contain a function to initialize CUDA, two kernels: a parent one and a child one, and a host function to invoke the parent kernel. mylib.h #includes both cuda_runtime.h and device_launch_parameters.h to make Visual Studio happy.

main.cpp simply #includes mylib.h, calls the initCUDA function, then calls the host function to invoke the kernel.

The CMakeLists file for the library looks like this:

cmake_minimum_required(VERSION 3.17 FATAL_ERROR)
project(MyLib LANGUAGES CXX CUDA)

find_package(CUDAToolkit REQUIRED)

add_library(${PROJECT_NAME} STATIC mylib.h mylib.cu)

target_compile_options(${PROJECT_NAME} PRIVATE "$<$<AND:$<CONFIG:Debug>,$<COMPILE_LANGUAGE:CUDA>>:-G;-src-in-ptx>") # enable device debug flags

set_target_properties(${PROJECT_NAME} PROPERTIES CUDA_ARCHITECTURES "52") # this is to make CMake happy
set_target_properties(${PROJECT_NAME} PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
set_target_properties(${PROJECT_NAME} PROPERTIES CUDA_RESOLVE_DEVICE_SYMBOLS ON)  # this is required for some reason

target_link_libraries(${PROJECT_NAME} ${CUDAToolkit_LIBRARY_DIR}/cudart.lib)

and the CMakeLists file for main.cpp looks like this:

cmake_minimum_required(VERSION 3.17 FATAL_ERROR)

project(CUDA_Dynamic_Parallelism)

add_executable(${PROJECT_NAME} main.cpp)
set_target_properties(${PROJECT_NAME} PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
target_link_libraries(${PROJECT_NAME} MyLib)

CMake configures and generates the solution, no problem. However, when I try to build, the library seems to build ok, but when the executable is linking, I get the following error:
MyLib.lib(MyLib.device-link.obj) : error LNK2001: unresolved external symbol __fatbinwrap_38_cuda_device_runtime_compute_86_cpp1_ii_8b1a5d37

Any ideas why this is happening and how to resolve it?

It looks like the object file containing device code is not passed to the host linker , the dlink is not performed well . You may try reporting a ticket to us with the complete cmake project attachment to CUDAIsseus@nvidia.com . We’ll take a look
Getting Help with CUDA NVCC Compiler

For everyone’s benefit, Yuki pointed out (via email) that my executable was not linking to cudadevrt.lib.

Two approaches to solve this:

1 - Via Visual Studio: In the project properties for the executable, go to Linker > Input > Additional Dependencies > Edit… Make sure the “Inherit from parent or project defaults” box is checked.
2 - Via CMake. In the CMakeLists.txt file for the executable, we need to link to cudadevrt.lib. Simply add a target_link_libraries() command, like so:

cmake_minimum_required(VERSION 3.17 FATAL_ERROR)

project(CUDA_Dynamic_Parallelism)

add_executable(${PROJECT_NAME} main.cpp)
set_target_properties(${PROJECT_NAME} PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
target_link_libraries(${PROJECT_NAME} MyLib)
target_link_libraries(${PROJECT_NAME} ${CUDAToolkit_LIBRARY_DIR}/cudadevrt.lib)

Note that you might need to call the find_package() command higher up in the CMake hierarchy, rather than down in the CMakeLists file for the static library.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.