我需要从一个C语言函数main()
(位于main.c中)中调用位于gpu.cu中的CUDA C函数foo()
。我尝试了以下代码:
main.c (caller):
#include "gpu.h" int main(); int main() { foo(); }
gpu.h (
foo()
declaration): <--- problem hereextern void foo();
gpu.cu (
foo()
definition):#include "gpu.h" extern "C" void foo() { ... }
I get the error:
gpu.cu(2): error: linkage specification is incompatible with previous "foo" gpu.h(1): here
然而,如果不使用头文件,以下内容仍然可以正常工作:
main.c (caller):
void foo(); int main(); int main() { foo(); }
gpu.cu (
foo()
declaration and definition):extern "C" void foo(); extern "C" void foo() { ... }
当然,我更喜欢在纯C和CUDA C代码中使用单个头文件,那么在头文件中使用的正确语法是什么(即使这是C++的事情,我们是否仍需要 extern "C"
)?我需要一个 .cuh 扩展名吗?
我正在使用 NVCC 编译和链接(即用于纯C和CUDA C代码)。
非常感谢。