Linking gcc and nvcc compiled code nvcc compiled object files not recognized by gcc obj files

I am trying to link files compile with gcc and files compiled with nvcc. The files compiled with nvcc have CUDA device function defined within them. I looked at the object files with nm and readelf and it appears that nvcc creates a different entry point than gcc and this is why I am getting “undefined reference to” when I try to link the object code.

Any help or direction with this would be appreciated. I am new to CUDA programming.

Hi,

normally, a command line such as this one should work (depending on the extra libraries you need)

gcc -o myBinary *.o -L/path_to_cuda_distribution/cuda/lib -lcudart -lcuda

depending whether you’re on a 32 or 64 bit environment, you might want to replace “lib” by “lib64”.

And then, should you have to link with some cuda libraries like cublas or cufft, place them right before “-lcudart” in the above command line, like this: “-lcublas -lcufft -lcudart -lcuda”

Other CPU libraries can be added either before or after the cuda libraries list.

Linking gcc- and nvcc-generated code definitely works, I do it all the time with all CUDA versions since 2.something.

However, you do not link to device functions. Rather, from your gcc-compiled code you call a host function within the nvcc-compiled code that then launches kernels on the device as desired.

I am still having trouble sorting this out.

Here is one file.

int getmcf( double *OTF,
double *OTFc,
long Nx,
long Ny,
long Nz,
double NA,
double nimm,
double delxy,
double delz,
int nthread );

int main(int argc, char* argv)
{
double *ctf;
double *cone_filter;
long xpadded;
long ypadded;
long zpadded;
double na;
double nimm;
double delxy;
double delz;
int nthread;
int result;
result = getmcf(
ctf,
cone_filter,
xpadded,
ypadded,
zpadded,
na,
nimm,
delxy,
delz,
nthread
);

return 0;

}
I compile it with
gcc -Wall -c core2_d.c -o core2_d.c

The compilation returns without errors or warnings.

Here is the second file.

int getmcf(
double *OTF,
double *OTFc,
long Nx,
long Ny,
long Nz,
double NA,
double nimm,
double delxy,
double delz,
int nthread )
{
return 0;

}

I compile it with
nvcc -c getmcf.cu -o getmcf.o
The compilation returned without errors or warnings.

Here is the link command and the errors I get.
gcc -o core2_decon *.o -L/usr/local/cuda/lib64 -lcudart -lcuda
core2_decon.o: In function main': core2_decon.c:(.text+0x6d): undefined reference to getmcf’
collect2: ld returned 1 exit status

Any help is appreciated.

Declare getmcf() as [font=“Courier New”]extern “C” int getmcf(…)[/font] in the second file. nvcc always acts as a C++ compiler and c-style linkage thus has to be enforced with the extern “C” keyword.

Thank you. As you know this worked.