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.
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.

#1
Posted 05/04/2012 04:26 AM   
Hi,
normally, a command line such as this one should work (depending on the extra libraries you need)
[code]
gcc -o myBinary *.o -L/path_to_cuda_distribution/cuda/lib -lcudart -lcuda[/code]
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.
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.

#2
Posted 05/04/2012 05:51 AM   
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.
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.

Always check return codes of CUDA calls for errors. Do not use __syncthreads() in conditional code unless the condition is guaranteed to evaluate identically for all threads of each block. Run your program under cuda-memcheck to detect stray memory accesses. If your kernel dies for larger problem sizes, it might exceed the runtime limit and trigger the watchdog timer.

#3
Posted 05/04/2012 01:35 PM   
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.
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.

#4
Posted 05/05/2012 09:27 PM   
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.
Declare getmcf() as extern "C" int getmcf(...) 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.

Always check return codes of CUDA calls for errors. Do not use __syncthreads() in conditional code unless the condition is guaranteed to evaluate identically for all threads of each block. Run your program under cuda-memcheck to detect stray memory accesses. If your kernel dies for larger problem sizes, it might exceed the runtime limit and trigger the watchdog timer.

#5
Posted 05/05/2012 09:40 PM   
[quote name='tera' date='05 May 2012 - 02:40 PM' timestamp='1336254040' post='1404736']
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.
[/quote]


Thank you. As you know this worked.
[quote name='tera' date='05 May 2012 - 02:40 PM' timestamp='1336254040' post='1404736']

Declare getmcf() as extern "C" int getmcf(...) 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.

#6
Posted 05/05/2012 11:24 PM   
Scroll To Top