Code::Blocks Semi-ignoring .cu files

Let’s say I have 3 files: test.c, test.h and I’m also using Code::Blocks with all the nvcc setup requirements.

I have a function: void cuda_setup(int x) initialized in test.h, declared in and referenced in test.c . Everything compiles fine BEFORE I put in the reference into test.c but after I call the function I get an ‘undefined reference to cuda_setup’ error. IS being compiled into an object and if I plant an error in the compiler complains. However, if i copy/paste cuda_setup(int x) into test.c (or even a fourth file) everything compiles and links fine.

ARgh, this is frustrating. I can’t figure out what is wrong. I’m actually building on another person’s project and their code is split across 20+ .c files. I can put cuda_setup() into any other file and it works fine. It’s only ignored when it’s defined in Any ideas why? Help would be saintly.

Try adding a few extern “C” lines:

/* e.g. when declaring */
extern “C” {
void cuda_setup(int x);

/* e.g. when defining */
extern “C”
void cuda_setup(int x)


That didn’t work :(

It’s compiling the .cu file into a .o, it complains when anything in the .cu file is wrong, and JUST now it even complained when I mismatched an ‘extern function()’ in the header with a ‘extern “C” function()’ in the actual declaration.

It just refuses to link the .cu object file with the rest of the same project. If i rename the .cu file to .c and remove the kernel everything works fine even without the ‘extern’. This must be a very very simple problem… that I apparently haven’t been able to figure out in days…

EDIT: I think i should have mentioned that the project I’m working off of is written in C and converting to C++ seems like it’ll be too much of a hassle. But, if there are no other alternatives…

Just a bump update: This isn’t a code::blocks error. I’ve got a seperate makefile and this problem still arises. The linker just refuses to see the functions within the .cu->object file.

Check the symbol names in the .o using nm.

If you have definitions like this:

extern “C” void cudafunction_(cuComplex *a, cuComplex *b, int *Np)

The symbol will have the same name:

nm Cuda_function.o |grep cudafunction
0000000000000260 T cudafunction_

If you omit the extern “C”, there will be C++ name mangling.

Thanks for the help. This was part of the problem. The solution (in my case at least) was that the main() function HAS to be in my .cu file. Once I switched that, defined external function calls within an extern ‘C’ the problem was fixed.