Linking C and CUDA files with NVCC AND GCC

PLAESE help me!!

That’s a minimal situation but I actually have to merge a BIG program written in C

with a tiny extension of CUDA program calling a single Cuda function from the C source.

[codebox]$ ll

total 148K

-rw-r–r–. 1 **** **** 81 2010-06-08 22:00 func.cu

-rw-r–r–. 1 **** **** 18 2010-06-08 21:58 func.h

-rw-r–r–. 1 **** **** 130K 2010-06-08 22:00 func.o <<<compiled with NVCC

-rw-r–r–. 1 **** **** 91 2010-06-08 21:58 main.c

-rw-r–r–. 1 **** **** 1.4K 2010-06-08 22:01 main.o <<< compiled with GCC

[/codebox]

[codebox]$ gcc -o prog *.o -lcuda -L/usr/local/cuda/lib -L/home/binde/NVIDIA_CUDA_SDK/lib

func.o: In function `__cudaUnregisterBinaryUtil()':

tmpxft_0000080c_00000000-1_func.cudafe1.cpp:(.text+0xf): undefined reference to `__cudaUnregisterFatBinary’

func.o: In function `__threadfence()':

tmpxft_0000080c_00000000-1_func.cudafe1.cpp:(.text+0x4458): undefined reference to `__cudaSynchronizeThreads’

func.o: In function `__threadfence_block()':

tmpxft_0000080c_00000000-1_func.cudafe1.cpp:(.text+0x4486): undefined reference to `__cudaSynchronizeThreads’

func.o: In function `__iAtomicAdd(int*, int)':

tmpxft_0000080c_00000000-1_func.cudafe1.cpp:(.text+0x4b1b): undefined reference to `__cudaMutexOperation’

tmpxft_0000080c_00000000-1_func.cudafe1.cpp:(.text+0x4b3c): undefined reference to `__cudaMutexOperation’

func.o: In function `__uAtomicAdd(unsigned int*, unsigned int)':

tmpxft_0000080c_00000000-1_func.cudafe1.cpp:(.text+0x4b5a): undefined reference to `__cudaMutexOperation’

tmpxft_0000080c_00000000-1_func.cudafe1.cpp:(.text+0x4b7b): undefined reference to `__cudaMutexOperation’

func.o: In function `__iAtomicExch(int*, int)':

tmpxft_0000080c_00000000-1_func.cudafe1.cpp:(.text+0x4b99): undefined reference to `__cudaMutexOperation’

func.o:tmpxft_0000080c_00000000-1_func.cudafe1.cpp:(.text+0x4bb5): more undefined references to `__cudaMutexOperation’ follow

func.o: In function `__threadfence_system()':

tmpxft_0000080c_00000000-1_func.cudafe1.cpp:(.text+0x72f1): undefined reference to `__cudaSynchronizeThreads’

func.o: In function `__itexfetchi(void const*, int4)':

tmpxft_0000080c_00000000-1_func.cudafe1.cpp:(.text+0x8048): undefined reference to `__cudaTextureFetch’

func.o: In function `__utexfetchi(void const*, int4)':

tmpxft_0000080c_00000000-1_func.cudafe1.cpp:(.text+0x80a1): undefined reference to `__cudaTextureFetch’

func.o: In function `__ftexfetchi(void const*, int4)':

tmpxft_0000080c_00000000-1_func.cudafe1.cpp:(.text+0x80fa): undefined reference to `__cudaTextureFetch’

func.o: In function `__itexfetch(void const*, float4, int)':

tmpxft_0000080c_00000000-1_func.cudafe1.cpp:(.text+0x816c): undefined reference to `__cudaTextureFetch’

func.o: In function `__utexfetch(void const*, float4, int)':

tmpxft_0000080c_00000000-1_func.cudafe1.cpp:(.text+0x81cc): undefined reference to `__cudaTextureFetch’

func.o:tmpxft_0000080c_00000000-1_func.cudafe1.cpp:(.text+0x822c): more undefined references to `__cudaTextureFetch’ follow

func.o: In function `__sti____cudaRegisterAll_39_tmpxft_0000080c_00000000_4_f

unc_cpp1_ii_b81a68a1()':

tmpxft_0000080c_00000000-1_func.cudafe1.cpp:(.text+0xfb05): undefined reference to `__cudaRegisterFatBinary’

tmpxft_0000080c_00000000-1_func.cudafe1.cpp:(.text+0xfb66): undefined reference to `__cudaRegisterFunction’

func.o: In function `cudaError cudaLaunch(char*)':

tmpxft_0000080c_00000000-1_func.cudafe1.cpp:(.text.Z10cudaLaunchIcE9cudaErrorPT[cud

aError cudaLaunch(char*)]+0x14): undefined reference to `cudaLaunch’

func.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0’

main.o: In function `main’:

main.c:(.text+0x5): undefined reference to `func’

collect2: ld returned 1 exit status

[/codebox]

Thank’s

Try to add -lcudart

by the way, are you using 64 bit system or 32 ? If the former, you may need to link lib64 instead of lib

I’m using a 64bit system but doesn’t work anyway.

Any solution?

g++ -o prog *.o -L/usr/local/cuda/lib64 -lcudart

or if you want to use gcc, you also need to link the stdc++ library:

gcc -o prog *.o -L/usr/local/cuda/lib64 -lcudart -lstdc++

How can I make visible the C function in func.cu to the main()?

I compile (nvcc/gcc -c) correctly producing the 2 object files.

But in the linking phase:

main.o: In function `main’:

main.c:(.text+0xa): undefined reference to `func1’

collect2: ld returned 1 exit status

Keep present that the similar process made with gcc and .c files works flawless.

[codebox]

$ cat func.cu

include <cuda.h>

include “func.h”

void func1(void){

return;

}

global void kernel(void){

int a;

a=0;

a++;

return;

}

[/codebox]

[codebox]

$ cat func.h

void func1(void);

[/codebox]

[codebox]

$ cat main.c

include <stdio.h>

include <stdlib.h>

int main(){

func1();

return 0;

}

[/codebox]

You need to declare func1 as extern “C”, because nvcc is funcamentally a C++ compiler:

$ cat func.cu

#include <cuda.h>

#include "func.h"

extern "C"

void func1(void){

	return;

}

__global__ void kernel(void){

	int a;

	a=0;

	a++;

	return;

}

func.h is a bit tricky, as it needs to be compilable as either a C or C++ header:

$ cat func.h 

#ifdef __cplusplus

extern "C"

#endif

void func1(void);

Just to add a bit of pedantry, nvcc isn’t a compiler at all - but it does use the host C++ compiler to compile host code inside .cu files by default.

Ahh, that’s why I wanted to write “functionally”. External Image

Many thank’s,

I solved my problem following your advices, I hope this thread will be useful for many others.

Hi,

Sorry if I hijack this thread, but can you write all the commands on how to get it work, I’ve been trying and trying but no luck :S

Thanks