Linking .CU and .CPP files

Hello Guys,

I have been working with CUDA files for a while, and now I need to use them in .CPP files. Can anyone tell me the proper procedure to compile and link the .CU and .CPP files? I have seen several posts online that suggest extern “C” for functions that call CUDA kernels. The extern “C” functions and the CUDA kernel reside inside the .CU file. While I am able to generate object files, but when I link them, it gives me the error “undefined reference to ” where is the extern “C” function (residing inside .CU file) that calls the CUDA kernel. I am terribly stuck and will really appreciate if you can get me out of it External Image

Here is what I have been doing

bash $: g++ -c -I $(LIBRARIES) main.cpp
bash $: nvcc -c -I $(LIBRARIES) gpu.cu
bash $: nvcc -o gpuexec main.o gpu.o (also tried g++ but gives the same error)
bash $: main.o: In function ‘main’:
main.cpp:(.text+0x33c9): undefined reference to ‘gpucaller’
collect2: ld returned 1 exit status
make: *** …

‘gpucaller’ is the extern “C” function residing inside a the gpu.cu file. It calls the CUDA kernel global void mykernel…

Help guys!

Thanks,

Vivek

Check the names in both object files:

nm gpu.o |grep -y gpucaller
nm main.o |grep -y gpucaller

In gpu.o it is: 0000000000000000 T myprint
In main.o it is: U myprint

Actually I was playing around and changed the name of gpucaller to myprint.

You need to put both the kernel function (global) and the code that invokes it into the same source file. You can either create a thin wrapper (host-side function callKernel that will simply pass all the input to GPU kernel) or use #include to include the cu file into each CPP file. Note that if you use #include you do not need to pass the cu file to compiler and linker.

I am having the same problem even when I am not creating a kernel function. I have the files main.c, function.cu, and function.cuh as below

//main.c

#include <stdio.h>

#include <stdlib.h>

#include "function.cuh"

int main()

{

	float f1,f2,f3;

	

	f1 = 2;

	f2 = 4;

	f3 = add(f1,f2);

	printf("\n%f\n\n",f3);

	return 0;

}
//function.cu

#include <stdio.h>

#include <stdlib.h>

float add(float f1, float f2)

{

	float f3;

	f3 = f1 + f2;

	return f3;

}
//function.cuh

#ifndef FUNCTION_H

#define FUNCTION_H

float add(float f1, float f2);

#endif

When I compile and link I get the following:

$ gcc -c main.c

$ nvcc -c function.cu

$ gcc -o test main.o function.o -L/usr/local/cuda/lib64 -lcudart

main.o: In function `main’:

main.c:(.text+0x2c): undefined reference to `add’

collect2: ld returned 1 exit status

Seems like the same problem, any ideas?

I am having the same problem even when I am not creating a kernel function. I have the files main.c, function.cu, and function.cuh as below

//main.c

#include <stdio.h>

#include <stdlib.h>

#include "function.cuh"

int main()

{

	float f1,f2,f3;

	

	f1 = 2;

	f2 = 4;

	f3 = add(f1,f2);

	printf("\n%f\n\n",f3);

	return 0;

}
//function.cu

#include <stdio.h>

#include <stdlib.h>

float add(float f1, float f2)

{

	float f3;

	f3 = f1 + f2;

	return f3;

}
//function.cuh

#ifndef FUNCTION_H

#define FUNCTION_H

float add(float f1, float f2);

#endif

When I compile and link I get the following:

$ gcc -c main.c

$ nvcc -c function.cu

$ gcc -o test main.o function.o -L/usr/local/cuda/lib64 -lcudart

main.o: In function `main’:

main.c:(.text+0x2c): undefined reference to `add’

collect2: ld returned 1 exit status

Seems like the same problem, any ideas?

My first guess would be calling conventions - cu files are also compiled with C++ compiler. Add “extern “C”” to add function declaration.

My first guess would be calling conventions - cu files are also compiled with C++ compiler. Add “extern “C”” to add function declaration.

Thanks that worked! Also worked when I put the main function in the .cu file and the add function in the .cpp file (obviously had to switch from using .c file to .cpp file).

I appreciate the help
-David

Thanks that worked! Also worked when I put the main function in the .cu file and the add function in the .cpp file (obviously had to switch from using .c file to .cpp file).

I appreciate the help
-David

i have the same error. I created a shared library wich use cublas but at moment of linking with file the main.c I get the “undefined reference” error with the function of shared library.

So the problem is that the main function have be in a *.cu file ?¿?¿