为什么官网上的 add.cu 程序不需要 cuda 相关头文件

222 天前
 xiaopanzi

今天开始学习 CUDA 编程,有个疑问:

比如官网上的入门程序add.cu,为什么它不需要使用任何 cuda 相关的头文件?

我的猜测:nvcc编译器实际上在编译之前会自动插入头文件再完成编译。

请大佬解答疑惑。

1331 次点击
所在节点    NVIDIA
3 条回复
learningman
222 天前
__global__是 CUDA C++的关键字,实际上是另外一种语言
xiaopanzi
222 天前
@learningman 按我理解,它只是扩展了 C/C++,像头文件这些内容肯定是保留的。比如`cudaMallocManaged`函数,没有头文件就能编译成功,这该如何理解?
xiaopanzi
222 天前
好像和我的猜测是一致的,

> 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.

参考[Does NVCC include header files automatically?]( https://forums.developer.nvidia.com/t/does-nvcc-include-header-files-automatically/48972)

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/978317

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX