简体   繁体   中英

CUDA compiler doesn't compile C files in same project

I have main.cu file that includes test.h which is header for test.c and all three files are in same project.

test.h code:

typedef struct {
    int a;
} struct_a;

void a(struct_a a);

test.c code:

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

main.cu code:

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

Output when building the project:

"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

Errors from building:

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

If i include test.c instead of test.h in main.cu it works. I tried to separately compile test.c , i guess CUDA compiler doesn't include/compile/link(?) test.c file?

As talonmies mentioned, CUDA uses C++ linkage. You need to add extern "C" qualifier to function declaration in test.h :

#ifdef __cplusplus
extern "C"
#endif
void a(struct_a a);

See In C++ source, what is the effect of extern "C"? for explanation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM