Does NVCC include header files automatically?

Hello.
I wrote some CUDA code not including any CUDA header files like cuda.h. And I used some CUDA functions like cudaMalloc(). But the code could be compiled and run successfully.
Does NVCC include header files automatically? Is there any document to mention this feature?
Thanks!

1 Like

nvcc automatically includes CUDA-specific header files when handling code in .cu files. You still need to include the standard C/C++ header files and header files for any libraries delivered with CUDA, if you use functions exported by these header files.

Thanks for your reply. I think that is the answer I want.

Hi can you please specify an official document that support this feature

Programming Guide, p. 43:

[…] nvcc implicitly includes cuda_runtime.h at the top of the translation unit.

If you look at cuda_runtime.h it pulls in a number of other CUDA-specific header files. Until just now I have never looked where this is documented, as this has been the case ever since the CUDA runtime API was created around the year 2006 and I was its first users.

I vaguely recall that the idea behind this header file pre-inclusion was to make writing CUDA programs as similar to writing normal C programs as possible (CUDA was initially based on C and did not switch to C++ until 2009 or so), which was considered important for broad acceptance of a new programming environment.

If you use APIs other than the CUDA runtime API you will still need to include the header files for those. I greped through the nvcc.exe binary just now and see no evidence of header files other than cuda_runtime.h being pre-included.

you can also see what nvcc includes using dryrun or verbose compile options (also confirms it is only cuda_runtime.h)

1 Like