Sample CUDA C++ code on WSL2 for VS 2022?

I have successfully installed CUDA on WSL2 (Ubuntu) including Docker, but I don’t know how to compile the first sample C++ program in Visual Studio 2022 on Windows 10 i.e. create a new project. Can you please give me a link to the sample code with instructions?

Although I have no problem to compile and run a C++ CUDA program (CUDA 11.8 Runtime project header, e.g. and a C++ wsl2 program (CMake project header, e.g. TestWSL.cpp and TestWSL.h) on my machine with Visual Studio 2022 (VS 2022), I don’t know how to create a proper wsl2 program with CUDA support in VS 2022 when I have a program on github with and sha256cu.h files (which run on Ubuntu with CUDAToolkit nicely).

CUDAToolkit on WSL 2 is installed successfully.

wsl cat /proc/version

Linux version (oe-user@oe-host) (x86_64-msft-linux-gcc (GCC) 9.3.0, GNU ld (GNU Binutils) #1 SMP Wed Mar 2 00:30:59 UTC 2022

nvcc --version

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2022 NVIDIA Corporation
Built on Wed_Sep_21_10:33:58_PDT_2022
Cuda compilation tools, release 11.8, V11.8.89
Build cuda_11.8.r11.8/compiler.31833905_0

I think that creating the C++ wsl2 program in VS 2022 and renaming the (CMake project header) generated *.cpp file to *.cu is not the correct way to make wsl2 with CUDAToolkit project.

The only problem I have now is that commands in CMakeLists.txt copy the project files to the wrong directory.


cmake_minimum_required (VERSION 3.8)

set(CMAKE_CUDA_COMPILER /usr/local/cuda-11.8/bin/nvcc)

project ("TrainWSL" CUDA)

if (POLICY CMP0074)
  cmake_policy(SET CMP0074 NEW)

project (CUDA)
set(CUDAToolkit_ROOT "${CMAKE_BINARY_DIR}/extracted/cuda")


add_executable(TrainWSL ${PROJECT_SOURCE_DIR}/

To wrong directory:


Instead to correct directory:


The compiler than can’t find the files of course.

How to fix it?

Finally, I managed to successfully compile the program on WSL2 in VS 2022 as CMake project WSL2 (Ubuntu) and run it on WSL.

Don’t forget to set environment varables for WSL permanently. On Ubuntu you need to add them manually e.g. by VIM editor to the .bashrc file see In Ubuntu WSL, how can you store permanent environment variables? Otherwise find_package(CUDAToolkit) can’t find it.

CMakeLists.txt file:

cmake_minimum_required (VERSION 3.8)

if (POLICY CMP0074)
cmake_policy(SET CMP0074 NEW)

project (“TestWSL2” CXX CUDA)

set(CMAKE_CUDA_COMPILER /usr/local/cuda-11.8/bin/nvcc)


find_package(Threads REQUIRED)

add_subdirectory (TestWSL2)

After running the program on WSL:

Hashes (32239104000) Seconds (345.671000) Hashes/sec (93265000)
Solution: XCjib6Q4dajD5VaxQ4QQ
Hashes processed: 32239104000
Time: 345673
Hashes/sec: 93264000

i am also struggling with this. I managed to get a simple program building and running but the intellisense for cuda libraries isnt finding header files that i can see exist in the header cache. Did you have the same issue?