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…
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.
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).
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 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 ?¿?¿