Problems building project with both .cu and .c files

Hey everyone,

I am working on a project that has multiple source files, 2 .c files and 2 .cu files.

How it works (in a nutshell) is that there is a main.cu file that uses functions declared in the other 2 .c files, and cuda kernels declared in the .cu file. The main function has to be in a .cu file (I imagine) because CUDA syntax is required even to call a kernel.

Before, when I was just writing the framework in C, it compiled cleanly and there were no problems whatsoever. At this point, however, the main file was a .c to simplify development. I then started writing kernels in a new source file, changed main to a .cu file and tried integrating them, but I am now encountering build problems.

My makefile builds the 2 .cu files with

[codebox]main.o: In function `main':

tmpxft_000058d0_00000000-10_main.ii:(.text+0x56): undefined reference to `errorfn(char*)’

tmpxft_000058d0_00000000-10_main.ii:(.text+0x9a): undefined reference to `errorfn(char*)’

tmpxft_000058d0_00000000-10_main.ii:(.text+0xa6): undefined reference to `errorfn(char*)’

tmpxft_000058d0_00000000-10_main.ii:(.text+0xba): undefined reference to `radar_filter(unsigned char*)’

tmpxft_000058d0_00000000-10_main.ii:(.text+0xc2): undefined reference to `get_bitmap_from_image(_IO_FILE*, int ()(unsigned char), int*, int*)’

tmpxft_000058d0_00000000-10_main.ii:(.text+0xeb): undefined reference to `draw_bitmap(unsigned short**, _IO_FILE*, int, int)’

tmpxft_000058d0_00000000-10_main.ii:(.text+0x133): undefined reference to `cudaMalloc’

tmpxft_000058d0_00000000-10_main.ii:(.text+0x146): undefined reference to `cudaMalloc’

tmpxft_000058d0_00000000-10_main.ii:(.text+0x18e): undefined reference to `cudaMemcpy’

tmpxft_000058d0_00000000-10_main.ii:(.text+0x1fa): undefined reference to `cudaConfigureCall’

tmpxft_000058d0_00000000-10_main.ii:(.text+0x211): undefined reference to `reverseArrayBlock__entry(int*, int*)’

tmpxft_000058d0_00000000-10_main.ii:(.text+0x216): undefined reference to `cudaThreadSynchronize’

tmpxft_000058d0_00000000-10_main.ii:(.text+0x220): undefined reference to `checkCUDAError(char const*)’

tmpxft_000058d0_00000000-10_main.ii:(.text+0x23c): undefined reference to `cudaMemcpy’

tmpxft_000058d0_00000000-10_main.ii:(.text+0x246): undefined reference to `checkCUDAError(char const*)’

tmpxft_000058d0_00000000-10_main.ii:(.text+0x2a1): undefined reference to `cudaFree’

tmpxft_000058d0_00000000-10_main.ii:(.text+0x2ad): undefined reference to `cudaFree’

tmpxft_000058d0_00000000-10_main.ii:(.text+0x2d4): undefined reference to `free_bitmap(unsigned short**, int)’

main.o: In function `__cudaUnregisterBinaryUtil’:

tmpxft_000058d0_00000000-10_main.ii:(.text+0x302): undefined reference to `__cudaUnregisterFatBinary’

main.o: In function `__threadfence’:

tmpxft_000058d0_00000000-10_main.ii:(.text+0x4581): undefined reference to `__cudaSynchronizeThreads’

main.o: In function `__threadfence_block’:

tmpxft_000058d0_00000000-10_main.ii:(.text+0x45af): undefined reference to `__cudaSynchronizeThreads’

main.o: In function `__iAtomicAdd’:

tmpxft_000058d0_00000000-10_main.ii:(.text+0x4c8b): undefined reference to `__cudaMutexOperation’

tmpxft_000058d0_00000000-10_main.ii:(.text+0x4cac): undefined reference to `__cudaMutexOperation’

main.o: In function `__uAtomicAdd’:

tmpxft_000058d0_00000000-10_main.ii:(.text+0x4cca): undefined reference to `__cudaMutexOperation’

tmpxft_000058d0_00000000-10_main.ii:(.text+0x4ceb): undefined reference to `__cudaMutexOperation’

main.o: In function `__iAtomicExch’:

tmpxft_000058d0_00000000-10_main.ii:(.text+0x4d09): undefined reference to `__cudaMutexOperation’

main.o:tmpxft_000058d0_00000000-10_main.ii:(.text+0x4d25): more undefined references to `__cudaMutexOperation’ follow

main.o: In function `__itexfetchi’:

tmpxft_000058d0_00000000-10_main.ii:(.text+0x72ce): undefined reference to `__cudaTextureFetch’

main.o: In function `__utexfetchi’:

tmpxft_000058d0_00000000-10_main.ii:(.text+0x7327): undefined reference to `__cudaTextureFetch’

main.o: In function `__ftexfetchi’:

tmpxft_000058d0_00000000-10_main.ii:(.text+0x7380): undefined reference to `__cudaTextureFetch’

main.o: In function `__itexfetch’:

tmpxft_000058d0_00000000-10_main.ii:(.text+0x73f2): undefined reference to `__cudaTextureFetch’

main.o: In function `__utexfetch’:

tmpxft_000058d0_00000000-10_main.ii:(.text+0x7452): undefined reference to `__cudaTextureFetch’

main.o:tmpxft_000058d0_00000000-10_main.ii:(.text+0x74b2): more undefined references to `__cudaTextureFetch’ follow

main.o: In function `__sti____cudaRegisterAll_39_tmpxft_000058d0_00000000_4_m

ain_cpp1_ii_main’:

tmpxft_000058d0_00000000-10_main.ii:(.text+0xd5c4): undefined reference to `__cudaRegisterFatBinary’

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

forecaster.o: In function `__cudaUnregisterBinaryUtil’:

tmpxft_00005822_00000000-10_forecaster.ii:(.text+0xf): undefined reference to `__cudaUnregisterFatBinary’

forecaster.o: In function `__threadfence’:

tmpxft_00005822_00000000-10_forecaster.ii:(.text+0x428e): undefined reference to `__cudaSynchronizeThreads’

forecaster.o: In function `__threadfence_block’:

tmpxft_00005822_00000000-10_forecaster.ii:(.text+0x42bc): undefined reference to `__cudaSynchronizeThreads’

forecaster.o: In function `__iAtomicAdd’:

tmpxft_00005822_00000000-10_forecaster.ii:(.text+0x4998): undefined reference to `__cudaMutexOperation’

tmpxft_00005822_00000000-10_forecaster.ii:(.text+0x49b9): undefined reference to `__cudaMutexOperation’

forecaster.o: In function `__uAtomicAdd’:

tmpxft_00005822_00000000-10_forecaster.ii:(.text+0x49d7): undefined reference to `__cudaMutexOperation’

tmpxft_00005822_00000000-10_forecaster.ii:(.text+0x49f8): undefined reference to `__cudaMutexOperation’

forecaster.o: In function `__iAtomicExch’:

tmpxft_00005822_00000000-10_forecaster.ii:(.text+0x4a16): undefined reference to `__cudaMutexOperation’

forecaster.o:tmpxft_00005822_00000000-10_forecaster.ii:(.text+0x4a32): more undefined references to `__cudaMutexOperation’ follow

forecaster.o: In function `__itexfetchi’:

tmpxft_00005822_00000000-10_forecaster.ii:(.text+0x6fdb): undefined reference to `__cudaTextureFetch’

forecaster.o: In function `__utexfetchi’:

tmpxft_00005822_00000000-10_forecaster.ii:(.text+0x7034): undefined reference to `__cudaTextureFetch’

forecaster.o: In function `__ftexfetchi’:

tmpxft_00005822_00000000-10_forecaster.ii:(.text+0x708d): undefined reference to `__cudaTextureFetch’

forecaster.o: In function `__itexfetch’:

tmpxft_00005822_00000000-10_forecaster.ii:(.text+0x70ff): undefined reference to `__cudaTextureFetch’

forecaster.o: In function `__utexfetch’:

tmpxft_00005822_00000000-10_forecaster.ii:(.text+0x715f): undefined reference to `__cudaTextureFetch’

forecaster.o:tmpxft_00005822_00000000-10_forecaster.ii:(.text+0x71bf): more undefined references to `__cudaTextureFetch’ follow

forecaster.o: In function `__sti____cudaRegisterAll_45_tmpxft_00005822_00000000_4_f

orecaster_cpp1_ii_caf66c8c’:

tmpxft_00005822_00000000-10_forecaster.ii:(.text+0xd2d1): undefined reference to `__cudaRegisterFatBinary’

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

collect2: ld returned 1 exit status

make: *** [forecaster] Error 1

[/codebox]

I have tried linking with the CUDA libraries (even though I already set the environment variables $PATH and $LD_LIBRARY_PATH as directed by the install script), but I get the same error. When I try building the object files into an executable using:

[codebox]main.o: In function `main':

tmpxft_000058d0_00000000-10_main.ii:(.text+0x56): undefined reference to `errorfn(char*)’

tmpxft_000058d0_00000000-10_main.ii:(.text+0x9a): undefined reference to `errorfn(char*)’

tmpxft_000058d0_00000000-10_main.ii:(.text+0xa6): undefined reference to `errorfn(char*)’

tmpxft_000058d0_00000000-10_main.ii:(.text+0xba): undefined reference to `radar_filter(unsigned char*)’

tmpxft_000058d0_00000000-10_main.ii:(.text+0xc2): undefined reference to `get_bitmap_from_image(_IO_FILE*, int ()(unsigned char), int*, int*)’

tmpxft_000058d0_00000000-10_main.ii:(.text+0xeb): undefined reference to `draw_bitmap(unsigned short**, _IO_FILE*, int, int)’

tmpxft_000058d0_00000000-10_main.ii:(.text+0x211): undefined reference to `reverseArrayBlock__entry(int*, int*)’

tmpxft_000058d0_00000000-10_main.ii:(.text+0x220): undefined reference to `checkCUDAError(char const*)’

tmpxft_000058d0_00000000-10_main.ii:(.text+0x246): undefined reference to `checkCUDAError(char const*)’

tmpxft_000058d0_00000000-10_main.ii:(.text+0x2d4): undefined reference to `free_bitmap(unsigned short**, int)’

collect2: ld returned 1 exit status

make: *** [forecaster] Error 255[/codebox]

All of the functions it is complaining about are defined in the source files, declared in the header and properly included.

Any idea of what I could possibly do to solve this?

If anyone has a simple project that mixes pure C files with CUDA files and would be willing to provide the makefile and a brief description, it would be GREATLY appeciated!
This may be a simpler solution for me than trying to find out what is specifically wrong with mine.

Do the link step using gcc and explicity add the cuda libraries like this:

gcc -o exe object1.o object2.o .... -L/path/to/cuda/lib -lcudart

Should just work. LD_LIBRARY_PATH and PATH have no effect on compilation and linking behaviour (beyond the shell finding the compiler anyway), they only influence the runtime link loader.