I am trying to include some CUDA code in a C project. To do this, I am compiling the CUDA source (.cu file) to a static library (.lib, I’m developing in Windows). I then wish to link in this static library to my C project. However, my linker is complaining about this step, and I believe it’s because of a C/C++ linkage problem. The specific errors I am getting look something like this:
Undefined symbol '_cudaMallocHost@8' referenced in "cufft object file.lib".
I am using CUDA 5.0 and I’m working in Visual Studio 10 to write and compile the CUDA source on a Windows 7 machine.
First, I have a .cu source file which compiles to a static library (“cufft object file.lib”) just fine. I wrapped the whole thing in extern C { }. It looks something like this:
extern "C" {
#include <cuda.h>
#include <cufft.h>
int myCUDAfunction()
{
//Some CUDA code
}
}
To go along with this, I made a header file with a list of functions define in the .cu source (“myCUDAheader.h”) whose contents are just a series of function prototypes:
int myCUDAfunction();
etc
Finally in my C project I add the static library and include the header file.
Before I started declaring the functions in the CUDA source .cu file with extern "C", the linker complained about missing the symbols for those functions. They must be getting compiled correctly to C linkage now, but the actual CUDA functions (such as cudaMalloc) still seem to have C++ linkage. How do I get around this?
Have you included the CUDA headers and linked cudart.lib to your C application? If not you will probably need to do this as your static library will not include the actual CUDA library implementation. Also, I think the order in which you link them matters, I can’t remember off the top my head if it’s your library then cudart.lib or the other way around. If the worst comes to the worst you may have to set up your C project as a CUDA project.