1

I have a cuda file test.cu that include a file cuda.h.

the cuda.h contains the following function defintion used in test.cu.

extern void check_error(cudaError_t status);

this function is defined in cuda.c as follow:

void check_error(cudaError_t status)
{
    cudaError_t status2 = cudaGetLastError();
    if (status != cudaSuccess)
    {   
        const char *s = cudaGetErrorString(status);
        char buffer[256];
        printf("CUDA Error: %s\n", s);
        assert(0);
        snprintf(buffer, 256, "CUDA Error: %s", s);
        error(buffer);
    } 
    if (status2 != cudaSuccess)
    {   
        const char *s = cudaGetErrorString(status);
        char buffer[256];
        printf("CUDA Error Prev: %s\n", s);
        assert(0);
        snprintf(buffer, 256, "CUDA Error Prev: %s", s);
        error(buffer);
    } 
}

I use Visual studio 2015 for compiling. cuda.c is compiled as a C file.

There is no Compilation errors. But I get the following linkage error:

test.cu.obj : error LNK2001: unresolved external symbol "void __cdecl check_error(enum cudaError)" (?check_error@@YAXW4cudaError@@@Z)

How to solve this error?

this is not a duplicate of Name mangling in CUDA and C++ because it ask about the reverse order. Call a C function from a Cuda code. in the above question it is for calling a cuda fuction from a C file.

Community
  • 1
  • 1
ProEns08
  • 1,856
  • 2
  • 22
  • 38

1 Answers1

2

When compiling C there is no name mangling.

When you compile C++ you need to turn off name mangling for the declaration with extern "C":

#ifdef __cplusplus
extern "C" {
#endif
    extern void check_error(cudaError_t status);
#ifdef __cplusplus
}
#endif
Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
  • It resolve the issue. Apparently, cuda code is compiled as C code. Thanks, +1. – ProEns08 May 26 '16 at 13:53
  • Why, then they require C name and dont support C++ name mangling? – ProEns08 May 26 '16 at 13:54
  • In my case, I was compiling code (C++/class/templates etc) in .cu file. I was not mentioning nvcc compiler in visual studio. As a result the .cu files were not compiled and I was getting a linker error. I used custom build option to solve this issue. Right click project -> Build Dependencies -> Build Customizations -> select CUDA targets. – Saleh Aug 22 '18 at 10:41