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
2
Answers
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:
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.
I’ve ran into the same issue today after reinstalling my CUDA toolkit. It did not happen before despite the lack of a "compile_commands.json" file. My project compiles and runs fine, so the installation is probably correct. However, VScode seems unable to recognize it. I have updated my "c_cpp_properties.json" file to explicitly include the compiler and library paths:
}
I have also manually created a "compile_commands.json" file with the following contents:
]
Despite this, VSCode does not recognize any basic header files nor cuda libraries (such as "cuda_runtime.h") and so does not recognise as correct nor provide support for any cuda code such as global function call configurations.