Fatal error: cuda.h: No such file or directory
Asked Answered
M

4

16

I successfully installed CUDA 8.0 in my PC and I can see its files by running the following commands in my Ubuntu 16.10:

$ sudo find / -name nvcc

/usr/local/cuda-8.0/bin/nvcc

$ sudo find / -name cuda

/usr/local/cuda
/usr/local/cuda-8.0/targets/x86_64-linux/include/thrust/system/cuda
/usr/share/doc/cuda
/usr/include/nvidia-367/cuda

Then, I got the following source code (has_cuda.c) to check out if CUDA is installed:

#include<cuda.h>

int main ()
{
    int deviceCount;
    cudaError_t e = cudaGetDeviceCount(&deviceCount);
    return e == cudaSuccess ? deviceCount : -1;
}

But running this code returns me the following error:

$ gcc has_cuda.c 

has_cuda.c:1:17: fatal error: cuda.h: No such file or directory
#include<cuda.h>
             ^
compilation terminated.

I looked for cuda.h in my directories and found them at the following places:

$ sudo find / -name cuda.h

/usr/local/cuda-8.0/targets/x86_64-linux/include/cuda.h
/usr/include/nvidia-367/cuda/cuda.h
/usr/include/linux/cuda.h
/usr/src/linux-headers-4.8.0-22/include/linux/cuda.h
/usr/src/linux-headers-4.8.0-22/include/uapi/linux/cuda.h
/usr/src/linux-headers-4.8.0-32/include/linux/cuda.h
/usr/src/linux-headers-4.8.0-32/include/uapi/linux/cuda.h

I am quite rookie on this, so, what can be happening? should I have to export any variable to point out where cuda.h is? how can I do this?

Magnesia answered 23/12, 2016 at 20:56 Comment(1)
As a side note, a faster way to find a header file: locate cuda.h. This is of course assuming your locate database is up to date (i.e. if you just installed CUDA, it would not find it).Grit
R
6

I have never compiled a cuda project myself but I think you will need to link the library to the compiler.

Some quick googling says Nvidia has compiler for this which will handle everything. So you just need to install that and you should be good to go. It's called NVVC. Once installed just run:

nvcc helloworld.cu -o hello.out

With external libraries like these, you almost always need to link them. You don't have to do it for the standard library because the linker already knows where to find it.

Revengeful answered 23/12, 2016 at 21:7 Comment(1)
It seems worth highlighting that the most relevant point of this answer is to rename the source file to have a .cu extension, which instructs nvcc to treat it as CUDA code. This will link in the relevant libraries, but it will also include necessary header files making the #include <cuda.h> at the top redundant (at best, see Robert Crovella's answer for more details).Cannabin
M
11

The proper include header file for this is not cuda.h but cuda_runtime.h, assuming you want to use gcc as the compiler for this code. As the other answer points out, you could just use nvcc (which is already installed on your machine) which wouldn't require any include headers for this code at all.

If you want to use nvcc you probably should make sure that the appropriate PATH environment variable is set. This and other useful information is contained in the linux install guide.

So if you modify your code like this:

#include <cuda_runtime.h>

int main ()
{
    int deviceCount;
    cudaError_t e = cudaGetDeviceCount(&deviceCount);
    return e == cudaSuccess ? deviceCount : -1;
}

You should be able to compile it successfully using a command something like this:

gcc -I/usr/local/cuda/include -L/usr/local/cuda/lib64 has_cuda.c -lcudart -o has_cuda

The path after the -I switch should contain the path to cuda_runtime.h on your machine. Normally that would be set up as above, but I'm not sure if the cuda symlink is set up on your machine, and also it looks like your include directory may be at an unusual place, i.e.

/usr/local/cuda-8.0/targets/x86_64-linux/include

but you can use find just as you have been doing to locate it.

Likewise, the path after the -L switch needs to be the path to your cuda lib64 directory, which will contain libcudart.so and and its cousins. Again, that would normally be symlinked on the path I have shown, but your machine install may not conform to my expectations. You should be able to use find to locate the correct path.

And as indicated in the other answer, if you use nvcc (which you have already located), you won't need to explicitly select the -I and -L path. The easiest way to make this work is to rename your file from has_cuda.c to has_cuda.cu, then you should be able to compile like this:

nvcc has_cuda.cu -o has_cuda

Finally, Ubuntu 16.10 is not an officially supported distribution for CUDA at this time, so there may be some things unexpected about the way it got installed on your machine. I'd encourage you to read the previously linked install guide, as it contains useful information about post-installation setup steps, such as setting environment variables, and also how to "verify" the CUDA installation.

Any time you are running CUDA codes, and having any sort of trouble, make sure to use proper cuda error checking and run your codes with cuda-memcheck, like so:

cuda-memcheck ./has_cuda

Even if you don't understand the error information reported, it may be useful for those trying to help you.

Mechanistic answered 23/12, 2016 at 21:28 Comment(3)
Although I can locate nvcc every time I use it I have the following error: $ nvcc has_cuda.c The program 'nvcc' is currently not installed. You can install it by typing: sudo apt install nvidia-cuda-toolkit.Magnesia
cuda-memcheck has a similar error: cuda-memcheck ./has_cuda The program 'cuda-memcheck' is currently not installed. You can install it by typing: sudo apt install nvidia-cuda-toolkitMagnesia
You'll need to add the CUDA bin directory to your PATH environment variable. These instructions are contained in the install guide I linked.Mechanistic
R
6

I have never compiled a cuda project myself but I think you will need to link the library to the compiler.

Some quick googling says Nvidia has compiler for this which will handle everything. So you just need to install that and you should be good to go. It's called NVVC. Once installed just run:

nvcc helloworld.cu -o hello.out

With external libraries like these, you almost always need to link them. You don't have to do it for the standard library because the linker already knows where to find it.

Revengeful answered 23/12, 2016 at 21:7 Comment(1)
It seems worth highlighting that the most relevant point of this answer is to rename the source file to have a .cu extension, which instructs nvcc to treat it as CUDA code. This will link in the relevant libraries, but it will also include necessary header files making the #include <cuda.h> at the top redundant (at best, see Robert Crovella's answer for more details).Cannabin
Z
3

When try to install python cuda package lietorch, if you see
have fatal error: cuda.h: No such file or directory when cuda.h exists then run this:

pip install pycuda 

After that, the problem should be resolved.

Zillion answered 2/11, 2022 at 14:26 Comment(0)
G
-1

Install the package (cuda-cudart-dev) which provides the cuda.h file:

Example:

dpkg --force-all -i cuda-cudart-dev-11-7_11.7.99-1_amd64.deb

Gonorrhea answered 13/2 at 19:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.