I’ve been trying to compile a set of C and CUDA code. The problem lies within the linking stage of the compilation when I make the file. I made a wrapper function to be executed on the host to allocate memory on the device, copy data to it, and run the kernel code. Also, the wrapper code is contained within the same file as the kernel code. The wrapper . Here is how the code calls the function:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "LAMMPS.h"
...
main(int argc, char **argv)
{
...
tortuosityTotal = gradient(nbAtoms, topAtoms, bottomAtoms, nTop, nBottom, allConnections, atomlist, ysize);
Here is the function definition in the header file “LAMMPS.h” which I made:
float gradient(unsigned short nbAtoms, unsigned short *topAtoms,unsigned short
*bottomAtoms, unsigned short nTop, unsigned short nBottom, struct connection
**allConnections,struct atoms *atomlist, double *ysize);
And here is the makefile I am using:
all: tortGPU
tortGPU: gradient_kernel.o buildNeighborList.o dumpRead.o tortuosityGPU.c
nvcc tortuosityGPU.c buildNeighborList.o dumpRead.o gradient_kernel.o -lm -o tortGPU
buildNeighborList.o: dumpRead.o buildNeighborList.c
gcc -c buildNeighborList.c
dumpRead.o: dumpRead.c
gcc -c dumpRead.c
gradient_kernel.o:
nvcc -c gradient_kernel.cu -arch=sm_20
clean: rm -rf *.o program
Finally, the compilation steps work fine, however when I go to link them all together (the final step with tortGPU I get the following error message:
/tmp/tmpxft_000068c7_00000000-1_tortuosityGPU.o: In function `main':
tortuosityGPU.c:(.text+0x520): undefined reference to `gradient'
collect2: ld returned 1 exit status
make: *** [tortGPU] Error 1
I tried using gcc with the addition -L and gotten the exact same error code.
Thanks!
Although the language is often called
cuda-C, it is more correct to call itcuda-C++asnvccis a C++ compiler. The function namedgradientis being subjected to C++ name mangling (or, more accurately,nvccis looking for a mangled name). You could either wrap your declarations withextern "C"or compile your code as C++.