CUDA 编译器不编译同一项目中的 C 文件

CUDA compiler doesn't compile C files in same project

我有 main.cu 文件,其中包含 test.h,对于 test.c 是 header,所有三个文件都在同一个项目中。

test.h代码:

typedef struct {
    int a;
} struct_a;

void a(struct_a a);

test.c代码:

void a(struct_a a) {
    printf("%d", a.a);
}

main.cu代码:

struct_a b;
b.a=2;
a(b);

构建项目时的输出:

"nvcc.exe" -gencode=arch=compute_20,code=\"sm_20,compute_20\" --use-local-env --cl-version 2013 -ccbin "CUDA\v7.0\include" -I "CUDA\v7.0\include"  -G   --keep-dir Debug -maxrregcount=0  --machine 32 --compile -cudart static  -g   -DWIN32 -D_DEBUG -D_CONSOLE -D_MBCS -Xcompiler "/EHsc /W3 /nologo /Od /Zi /RTC1 /MDd  " -o Debug\main.cu.obj "CudaTest\CudaTest\main.cu" 
1>  main.cu
1>  test.c

构建错误:

Error   1   error LNK2019: unresolved external symbol "void __cdecl a(struct struct_a)" (?a@@YAXUstruct_a@@@Z) referenced in function _main

如果我在 main.cu 中包含 test.c 而不是 test.h,它就可以工作。 我尝试单独编译 test.c,我猜 CUDA 编译器没有 include/compile/link(?) test.c 文件?

正如 talonmies 提到的,CUDA 使用 C++ 链接。您需要在 test.h:

中的函数声明中添加 extern "C" 限定符
#ifdef __cplusplus
extern "C"
#endif
void a(struct_a a);

请参阅 In C++ source, what is the effect of extern "C"? 了解说明。