CUDA: Visual Studio projects do not link to cudadevrt
According to CUDA docs there are some cases where a CUDA binary "...must be linked against the CUDA device runtime (cudadevrt) library". As of CUDA 8 (maybe earlier?) nvcc
adds -lcudadevrt
before the -lcudart
or -lcudart_static
when using -cudart=shared
or -cudart=static
. Only with -cudart=none
does nvcc not list it. Since the cudadevrt
library is always static the linker will not actually use it if no symbols from it are needed, so it doesn't hurt to add it.
When using a Ninja or Makefile generator we detect nvcc's implicit library list and get (e.g. on Windows):
set(CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES "cudadevrt.lib;cudart_static.lib")
# ...
set(CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES "cudadevrt.lib;cudart_static.lib")
However, the CUDA Toolkit's Visual Studio integration drives linking using link.exe
instead of nvcc
so there are no implicit libraries at all. Instead .vcxproj
files are expected to explicitly list them. We do this for cudart
or cudart_static
already. We should add cudadevrt
as nvcc would.
Without cudadevrt
we may see errors like the following seen in our test suite:
CudaOnlyLinkSystemDeviceLibraries.device-link.obj : error LNK2001: unresolved external symbol __fatbinwrap_66_tmpxft_000009c4_00000000_16_cuda_device_runtime_compute_70_cpp1_ii_8b1a5d37 [...\Tests\CudaOnly\LinkSystemDeviceLibraries\CudaOnlyLinkSystemDeviceLibraries.vcxproj]