Nvcc compiler not seeing cuCtxSynchronize at linking

Hi,

using visual studio toolchain (Windows 10) with a CMakeLists.txt inside the JetBrain CLion-Nova IDE I am able to compile but not link onto the cuCtxSynchronize and I am getting an error

====================[ Build | SystemProgrammingCuda | Debug-Visual Studio ]=====
"C:\Users\Frederic\AppData\Local\Programs\CLion Nova\bin\cmake\win\x64\bin\cmake.exe" --build C:\Users\Frederic\OneDrive\UVPD-Perpignan\SourceCodes\CLionProjects\SystemProgrammingCuda\cmake-build-debug-visual-studio --target SystemProgrammingCuda -j 6
[1/1] Linking CUDA executable SystemProgrammingCuda.exe
FAILED: SystemProgrammingCuda.exe 
cmd.exe /C "cd . && "C:\Users\Frederic\AppData\Local\Programs\CLion Nova\bin\cmake\win\x64\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\SystemProgrammingCuda.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x64\mt.exe --manifests  -- C:\PROGRA~2\MIB055~1\2019\COMMUN~1\VC\Tools\MSVC\1429~1.301\bin\Hostx64\x64\link.exe /nologo CMakeFiles\SystemProgrammingCuda.dir\main.cu.obj CMakeFiles\SystemProgrammingCuda.dir\utils\timming.cu.obj CMakeFiles\SystemProgrammingCuda.dir\utils\cmdLine.cu.obj CMakeFiles\SystemProgrammingCuda.dir\utils\get_deviceQuery_gpu.cu.obj CMakeFiles\SystemProgrammingCuda.dir\utils\get_systemQuery_cpu.cu.obj CMakeFiles\SystemProgrammingCuda.dir\utils\deviceTools_gpu.cu.obj CMakeFiles\SystemProgrammingCuda.dir\src\Sockets.cu.obj CMakeFiles\SystemProgrammingCuda.dir\src\carte_mesh_3D.cu.obj CMakeFiles\SystemProgrammingCuda.dir\src\dataDeviceManag.cu.obj CMakeFiles\SystemProgrammingCuda.dir\src\resmap_Sizes.cu.obj CMakeFiles\SystemProgrammingCuda.dir\src\common_Helpr_gpu.cu.obj CMakeFiles\SystemProgrammingCuda.dir\src\common_krnl_gpu.cu.obj CMakeFiles\SystemProgrammingCuda.dir\global.cu.obj CMakeFiles\SystemProgrammingCuda.dir\cmake_device_link.obj  /out:SystemProgrammingCuda.exe /implib:SystemProgrammingCuda.lib /pdb:SystemProgrammingCuda.pdb /version:0.0 /debug /INCREMENTAL  "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.2\lib\x64\cufft.lib"  cudadevrt.lib  cudart_static.lib  kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib -LIBPATH:"C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.2/lib/x64"  && cd ."
LINK Pass 1: command "C:\PROGRA~2\MIB055~1\2019\COMMUN~1\VC\Tools\MSVC\1429~1.301\bin\Hostx64\x64\link.exe /nologo CMakeFiles\SystemProgrammingCuda.dir\main.cu.obj CMakeFiles\SystemProgrammingCuda.dir\utils\timming.cu.obj CMakeFiles\SystemProgrammingCuda.dir\utils\cmdLine.cu.obj CMakeFiles\SystemProgrammingCuda.dir\utils\get_deviceQuery_gpu.cu.obj CMakeFiles\SystemProgrammingCuda.dir\utils\get_systemQuery_cpu.cu.obj CMakeFiles\SystemProgrammingCuda.dir\utils\deviceTools_gpu.cu.obj CMakeFiles\SystemProgrammingCuda.dir\src\Sockets.cu.obj CMakeFiles\SystemProgrammingCuda.dir\src\carte_mesh_3D.cu.obj CMakeFiles\SystemProgrammingCuda.dir\src\dataDeviceManag.cu.obj CMakeFiles\SystemProgrammingCuda.dir\src\resmap_Sizes.cu.obj CMakeFiles\SystemProgrammingCuda.dir\src\common_Helpr_gpu.cu.obj CMakeFiles\SystemProgrammingCuda.dir\src\common_krnl_gpu.cu.obj CMakeFiles\SystemProgrammingCuda.dir\global.cu.obj CMakeFiles\SystemProgrammingCuda.dir\cmake_device_link.obj /out:SystemProgrammingCuda.exe /implib:SystemProgrammingCuda.lib /pdb:SystemProgrammingCuda.pdb /version:0.0 /debug /INCREMENTAL C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.2\lib\x64\cufft.lib cudadevrt.lib cudart_static.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib -LIBPATH:C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.2/lib/x64 /MANIFEST /MANIFESTFILE:CMakeFiles\SystemProgrammingCuda.dir/intermediate.manifest CMakeFiles\SystemProgrammingCuda.dir/manifest.res" failed (exit code 1120) with the following output:
   Creating library SystemProgrammingCuda.lib and object SystemProgrammingCuda.exp
LINK : warning LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs; use /NODEFAULTLIB:library
main.cu.obj : error LNK2019: unresolved external symbol cuCtxSynchronize referenced in function main
SystemProgrammingCuda.exe : fatal error LNK1120: 1 unresolved externals
ninja: build stopped: subcommand failed.

as if I did I forgot to link onto a library. I have included

#include <cuda_runtime.h>
#include <cuda_runtime_api.h>
#include <cuda_device_runtime_api.h>
#include <cufftXt.h>
#include <cufft.h>
#include <cuda.h>

and I am trying to link to the cufft library in the CMakeLists.txt this way (cufftXt link does not work but I am
not sure that it is really needed):

set(FIND_CUDA_BIN_PATHS C:/Program\ Files/NVIDIA\ GPU\ Computing\ Toolkit/CUDA/v12.1/bin/)
set(FIND_CUFFT_PATHS C:/Program Files/NVIDIA\ GPU\ Computing\ Toolkit/CUDA/v12.2)

set(CMAKE_CUDA_HOST_COMPILER "${FIND_CUDA_BIN_PATHS}")
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Xcompiler -fPIC --ptxas-options=-v")

find_path(CUFFT_INCLUDE_DIR  cufft.h PATH_SUFFIXES include PATHS ${FIND_CUFFT_PATHS})
find_path(CUFFTXT_INCLUDE_DIR  cufftXt.h PATH_SUFFIXES include PATHS ${FIND_CUFFTXT_PATHS})

find_library(CUFFT_LIBRARY NAMES cufft PATH_SUFFIXES lib/x64 PATHS ${FIND_CUFFT_PATHS})

include_directories(${CUFFT_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} LINK_PUBLIC ${CUFFT_LIBRARY})

How to make the linker see cuCtxSynchroze() which I use in almost all my kernel calls.
Have I forgotten something or missing a library link somewhere ?

Thanks,
F.

you need to link against the cuda library (cuda.lib on windows). I won’t be able to tell you how to do that with CMake, and this forum is for questions about nvcc, not CMake (which is not a NVIDIA product).

If you are issuing a compile command via nvcc, adding -lcuda will do the necessary linking to allow usage of cuCtxSynchronize, or any part of the CUDA driver API.

Here is a question/answer that should be useful, I think. notice my answer there pertains to the old CMake CUDA methodology, Alex Reinking’s answer there pertains to the new CMake CUDA methodology.

Dear Robert,

thanks so much I saw it in the cuda.h header and I corrected it all in my CMakeLists.txt

cmake_minimum_required(VERSION 3.27)
set(FIND_CUDA_PATHS C:/Program Files/NVIDIA\ GPU\ Computing\ Toolkit/CUDA/v12.2)
set(FIND_CUDA_BIN_PATHS C:/Program\ Files/NVIDIA\ GPU\ Computing\ Toolkit/CUDA/v12.1/bin/)

set(CMAKE_CUDA_HOST_COMPILER "${FIND_CUDA_BIN_PATHS}")
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Xcompiler -fPIC --ptxas-options=-v")

project(SystemProgrammingCuda CUDA)

set(CMAKE_CUDA_STANDARD 17)
set(CMAKE_CXX_STANDARD 17)

add_definitions(-DWINDOWS -DCUDA)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")

set(SOURCE_FILES
        include/dataDeviceManag.cuh
        include/Exception.cuh
        include/Sockets.cuh
        include/common.cuh
        include/Sockets.cuh
        include/GetPID.h
        include/common_krnl.cuh
        include/carte_mesh_3D.cuh
        include/cuAtomics.cuh
        include/timming.cuh
        include/get_deviceQuery_gpu.cuh
        include/get_systemQuery_cpu.cuh
        include/cmdLine.cuh
        include/resmap_Sizes.cuh
        include/common_Helpr_gpu.cuh
        include/deviceTools_gpu.cuh
        include/testing_unitTest.cuh
        utils/timming.cu
        utils/cmdLine.cu
        utils/get_deviceQuery_gpu.cu
        utils/get_systemQuery_cpu.cu
        utils/deviceTools_gpu.cu
        src/Sockets.cu
        src/GetPID.c
        src/carte_mesh_3D.cu
        src/dataDeviceManag.cu
        src/resmap_Sizes.cu
        src/common_Helpr_gpu.cu
        src/common_krnl_gpu.cu
        test_codes/cpu/testing_ChronoTime.cpp
        test_codes/gpu/testing_unitTest.cu
        global.cu
        global.cuh
)

add_executable(SystemProgrammingCuda main.cu ${SOURCE_FILES})

find_path(CUFFT_INCLUDE_DIR  cufft.h PATH_SUFFIXES include PATHS ${FIND_CUDA_PATHS})
find_path(CUFFTXT_INCLUDE_DIR  cufftXt.h PATH_SUFFIXES include PATHS ${FIND_CUFFTXT_PATHS})

find_library(CUFFT_LIBRARY NAMES cufft PATH_SUFFIXES lib/x64 PATHS ${FIND_CUDA_PATHS})
find_library(CUDA_LIBRARY NAMES cuda PATH_SUFFIXES lib/x64 PATHS ${FIND_CUDA_PATHS})
find_library(CUDART_LIBRARY NAMES cuda cudart PATH_SUFFIXES lib/x64 PATHS ${FIND_CUDA_PATHS})

include_directories(${CUFFT_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} LINK_PUBLIC ${CUFFT_LIBRARY} ${CUDA_LIBRARY} ${CUDART_LIBRARY})

set_target_properties(SystemProgrammingCuda PROPERTIES CUDA_SEPARABLE_COMPILATION ON)

It works now. Really cool.

F.