Search code examples
cvisual-studio-codedebuggingcuda

Why is include cuda_runtime.h marked red in vscode despite cuda toolkit being installed properly?


I have a c - cuda project. It has a main.c file and a deviceFunctions.cu file. Visual Studio Code is used to work on the project also this extension: ms-vscode.cpptools is installed to get Code completion and error checking etc.

The helper.h file has all the include Statements that are needed in the device and host code. Then the header is included in both those files. This statement: #include <cuda_runtime.h> is needed to run cuda code but its marked red. Why is that despite proper cuda toolkit installation and no errors when running the code? Also the code completion by the extension is not working well too.

The code is compiled using these commands:

#Compile Cuda object file:                                                                                                                                                              
nvcc -O3 -c -g -o target.o -arch=sm_75 deviceFunctions.cu                                                                                                                          
#Device Link (what exactly is meant by this idk)                                                                                                  
nvcc -O3 -g -o dlink.o -arch=sm_75 -dlink target.o                                                                                                                  
#Compile C code
gcc -std=gnu99 -c -g -o main main.c -I/usr/local/cuda-12.3/include -L/usr/local/cuda-12.3/lib64 -lcudadevrt -lm -lrt                                                                                                                                                                                  
#Final Link                                                                                                                                                                  
gcc -g -o compute_output main dlink.o target.o -I/usr/local/cuda-12.3/include -L/usr/local/cuda-12.3/lib64 -lcudadevrt -lcudart -lm -lrt

Solution

  • The answer to this is that the extension needs information about how the code is compiled to understand everything, like the include statements. For example the cuda toolkit library is included in the compile commands but the extension needs that information aswell.

    For that purpose you need to add a "compile_commands.json" file to the root directory of your project. In this case that file can't be auto generated. If you use Cmake to build the project it can be. In this case however the file should look like this:

    [
      {
        "directory": "/path/to/project",
        "command": "nvcc -O3 -c -g -o target.o --cuda-gpu-arch=sm_75 deviceFunctions.cu",
        "file": "cudafunction.cu"
      },
      {
        "directory": "/path/to/project",
        "command": "nvcc -O3 -g -o dlink.o --cuda-gpu-arch=sm_75 -dlink target.o",
        "file": "dlink.o"
      },
      {
        "directory": "/path/to/project",
        "command": "gcc -std=gnu99 -c -g -o main main.c -I/usr/local/cuda-12.3/include -L/usr/local/cuda-12.3/lib64 -lcudadevrt -lcublas -lcublasLt -lcudart -lm -lrt",
        "file": "main.c"
      },
      {
        "directory": "/path/to/project",
        "command": "gcc -g -o compute_output main dlink.o target.o -I/usr/local/cuda-12.3/include -L/usr/local/cuda-12.3/lib64 -lcudadevrt -lcublas -lcublasLt -lcudart -lm -lrt",
        "file": "compute_output"
      }
    ]
    

    If this file is correct as well as the cuda toolkit installation the extension will not mark the include statement as red anymore and the code completion will work as well with the device code and the host code.