【问题标题】:CUDA compiler doesn't compile C files in same projectCUDA 编译器不会在同一个项目中编译 C 文件
【发布时间】:2015-07-07 10:23:49
【问题描述】:

我有 main.cu 文件,其中包括 test.h,这是 test.c 的标头,并且所有三个文件都在同一个项目中。

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编译器不包含/编译/链接(?)test.c文件?

【问题讨论】:

  • 这看起来像是一个 C++ 与 C 链接问题。
  • 如果我在 main.cu 中包含 test.c 而不是 test.h 它可以工作。
  • 将 test.c 重命名为 test.cpp 为什么你的输出显示 kernel.cu
  • kernel.cu 因为为了清楚起见,我用 main 替换了内核,但是当我将其更改为 .cpp 时它工作...我可以有任何机会获得带有 .c 扩展名的 c 源代码吗?
  • 是的,您可以使用 .c 文件。但是 nvcc 使用 c++ 样式的链接,因此您必须正确处理链接,这就是@talonmies 所指出的。这个问题与 cuda 标签上已经提出的许多其他问题相同。我最终会找到一个重复的并标记它。

标签: c visual-studio-2013 cuda compiler-errors


【解决方案1】:

正如之前提到的,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"? 了解说明。

【讨论】:

  • 我在 main.cu 中使用了 extern "C" { #include test.h } 也可以
  • 感谢您的回答,但 extern C 使模板成为不可能,但模板在 main.cu 文件中有效。如何解决这个问题?
猜你喜欢
  • 2016-03-20
  • 2012-04-17
  • 1970-01-01
  • 1970-01-01
  • 2013-06-10
  • 2012-12-02
  • 1970-01-01
  • 2014-07-10
  • 1970-01-01
相关资源
最近更新 更多