Can't get CUDA sample to compile under Visual Studio 2022 on Windows

Hey, it is my first post on the NVIDIA forum, since I’ve recently become interested in CUDA.

I’ve installed the CUDA 13.1 toolkit on my Windows 11 machine with an RTX 5060 Ti and tried to compile the sample program provided, when setting up a CUDA project under Visual Studio.

After project creation the following issues arise:

  1. The editor complains about the triple brackets used to call a CUDA function, which is tagged as an error.

  2. When trying to compile the sample, the compilation fails and the following output is given in the error list:

    The command ““C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.1\bin\nvcc.exe” -gencode=arch=compute_75,code="sm_75,compute_75"  -ccbin “C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\bin\HostX64\x64” -x cu   -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.1\include” -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.1\include"  -G   --keep-dir x64\Debug  -maxrregcount=0    --machine 64 --compile -cudart hybrid  -g  -DWIN32 -DWIN64 -D_DEBUG -D_CONSOLE -D_MBCS -Xcompiler “/EHsc /W3 /nologo /Od /FS /Zi /RTC1 /MDd " -Xcompiler “/Fdx64\Debug\vc143.pdb” -o D:\repository\CudaRuntime4\CudaRuntime4\x64\Debug\kernel.cu.obj “D:\repository\CudaRuntime4\CudaRuntime4\kernel.cu”” exited with code 1.
    
    
    

The project utilizes the Platform Toolset v143, 14.44.35207.

This is the generated build log:

CudaRuntime4.log (707.5 KB)

Does anyone know how this can be solved and could help?

Thank you for your help.

I use Visual studio 2022 create new project, there is one CUDA 13.1 runtime project template you can use.

Then you can build no problem.

After this, you can try different code.

Thanks for the quick response. When I use the CUDA 13.1 template provided in Visual Studio 2022 it results in the errors listed in my post.

There are two possibility for fix,

  1. In you log, you create project in d: disk, sometimes change to c: disk may help.
  2. CUDA Toolkit 13.1 includes some files that contain invalid or non‑ASCII characters, which can cause compiler or linker warnings. For example, the header cuda_runtime_api.h in the include directory contains 13 problematic characters, and my compiler reports:
    “C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.1\include\cuda_runtime_api.h(14276): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss.”
  

This is my pc build sample:

C:\Users\richa\source\repos\CudaRuntime1>“C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.1\bin\nvcc.exe” -gencode=arch=compute_75,code=“sm_75,compute_75” --use-local-env -ccbin “C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\bin\HostX64\x64” -x cu -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.1\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.1\include" -G --keep-dir CudaRuntime1\x64\Debug -maxrregcount=0 --machine 64 --compile -cudart hybrid -g -DWIN32 -DWIN64 -D_DEBUG -D_CONSOLE -D_MBCS -Xcompiler "/EHsc /W3 /nologo /Od /FS /Zi /RTC1 /MDd " -Xcompiler “/FdCudaRuntime1\x64\Debug\vc143.pdb” -o C:\Users\richa\source\repos\CudaRuntime1\CudaRuntime1\x64\Debug\kernel.cu.obj “C:\Users\richa\source\repos\CudaRuntime1\kernel.cu”
1>kernel.cu
1>tmpxft_0000b438_00000000-7_kernel.cudafe1.cpp
1> Creating library C:\Users\richa\source\repos\CudaRuntime1\x64\Debug\CudaRuntime1.lib and object C:\Users\richa\source\repos\CudaRuntime1\x64\Debug\CudaRuntime1.exp
1>LINK : warning LNK4098: defaultlib ‘LIBCMT’ conflicts with use of other libs; use /NODEFAULTLIB:library
1>CudaRuntime1.vcxproj → C:\Users\richa\source\repos\CudaRuntime1\x64\Debug\CudaRuntime1.exe
1>Done building project “CudaRuntime1.vcxproj”.
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
========== Rebuild completed at 8:46 PM and took 01.975 seconds ==========

It has been a little bit since my last response… I’ve tried to compile the .cu module manually in the terminal by utilizing the command used by visual studio.

“C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.1\bin\nvcc.exe” -gencode=arch=compute_75,code=“sm_75,compute_75” --use-local-env -ccbin “C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\bin\HostX64\x64” -x cu -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.1\include” -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.1\include" -G --keep-dir x64\Debug -maxrregcount=0 --machine 64 --compile -cudart hybrid -g -DWIN32 -DWIN64 -D_DEBUG -D_CONSOLE -D_MBCS -Xcompiler “/EHsc /W3 /nologo /Od /FS /Zi /RTC1 /MDd " -Xcompiler “/Fdx64\Debug\vc143.pdb” -o “C:\Users\Loran Salvo\source\repos\CUDA Test\CUDA Test\x64\Debug\kernel.cu.obj” “C:\Users\Loran Salvo\source\repos\CUDA Test\CUDA Test\kernel.cu”

nvcc then gives the following output:

nvcc fatal : cl.exe in PATH (C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.44.35207/bin/HostX86/x86) is different than one specified with -ccbin (C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.44.35207/bin/HostX64/x64)

After removing --use-local-env the module was successfully compiled.

So it seams like Visual Studio is setting up a local environment pointing to a 32 bit compiler, which nvcc does not like. Removing –use-local-env prevents this. But I’m still not able to execute the program through Visual Studio after compilation. Do you have an idea on how to fix Visual Studio’s environment setup?

BTW I’ve also looked in the system’s environment variables for a PATH into a 32 bit compiler. There is none, which implies to me, to be some local environment setup inside Visual Studio.