Compile as library

Hi everybody.

I need to compile code that both uses CUDA and Qt. My intention was to compile the CUDA code as a library (static or shared, I don’t really care) and link it to the main program that uses Qt. Now I’m trying to simply compile a piece of CUDA coda as a library and add it to a simple C file:

cudatest.h

[codebox]#include "cudatest.h"

#define N (1<<20)

#define BLOCK_SIZE 64

global void function_gpu(float* A, float* C) {

unsigned int idx = threadIdx.x + BLOCK_SIZE*blockIdx.x;

if(idx<N-2) C[idx] = A[idx] + A[idx+1] + A[idx+2];

}

void function_cpu(float* A, float* C) {

int i;

for(i=0;i<N-2;i++) C[i]= A[i]+A[i+1]+A[i+2];

}

int cudamain(void) {

float *A, *C, *Ad, *Cd;

int i;

double error;

A = (float ) malloc(Nsizeof(float));

C = (float ) malloc(Nsizeof(float));

for(i=0;i<N;i++) A[i]=i;

cudaMalloc((void**)&Ad,N*sizeof(float));

cudaMalloc((void**)&Cd,N*sizeof(float));

cudaMemcpy(Ad,A,N*sizeof(float),cudaMemcpyHostToDevice);

cudaMemcpy(Cd,A,N*sizeof(float),cudaMemcpyHostToDevice);

dim3 dimBlock(BLOCK_SIZE);

dim3 dimGrid(N/BLOCK_SIZE);

function_gpu<<<dimGrid,dimBlock>>>(Ad,Cd);

cudaThreadSynchronize();

cudaMemcpy(C,Cd,N*sizeof(float),cudaMemcpyDeviceToHost);

cudaFree(Ad); cudaFree(Cd);

float Cc[N];

function_cpu(A,Cc);

error=0;

for(i=0;i<N-2;i++) error += abs(C[i]-Cc[i]);

printf(“Error=%f\n”,error);

if(error)

 printf("!!! ERROR !!!\n");

return 0;

}[/codebox]

test.c

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

prueba.c:(.text+0x7): undefined reference to `cudamain’

cudaprueba.o: In function `cudamain()’:

tmpxft_00006955_00000000-10_cudaprueba.ii:(.text+0xb8): undefined reference to `cudaMalloc’

tmpxft_00006955_00000000-10_cudaprueba.ii:(.text+0xcb): undefined reference to `cudaMalloc’

tmpxft_00006955_00000000-10_cudaprueba.ii:(.text+0xed): undefined reference to `cudaMemcpy’

tmpxft_00006955_00000000-10_cudaprueba.ii:(.text+0x10f): undefined reference to `cudaMemcpy’

tmpxft_00006955_00000000-10_cudaprueba.ii:(.text+0x193): undefined reference to `cudaConfigureCall’

tmpxft_00006955_00000000-10_cudaprueba.ii:(.text+0x1ae): undefined reference to `cudaThreadSynchronize’

tmpxft_00006955_00000000-10_cudaprueba.ii:(.text+0x1d0): undefined reference to `cudaMemcpy’

tmpxft_00006955_00000000-10_cudaprueba.ii:(.text+0x1db): undefined reference to `cudaFree’

tmpxft_00006955_00000000-10_cudaprueba.ii:(.text+0x1e6): undefined reference to `cudaFree’

cudaprueba.o: In function `__cudaUnregisterBinaryUtil’:

tmpxft_00006955_00000000-10_cudaprueba.ii:(.text+0x28b): undefined reference to `__cudaUnregisterFatBinary’

cudaprueba.o: In function `__threadfence’:

tmpxft_00006955_00000000-10_cudaprueba.ii:(.text+0x4725): undefined reference to `__cudaSynchronizeThreads’

cudaprueba.o: In function `__threadfence_block’:

tmpxft_00006955_00000000-10_cudaprueba.ii:(.text+0x4751): undefined reference to `__cudaSynchronizeThreads’

cudaprueba.o: In function `__iAtomicAdd’:

tmpxft_00006955_00000000-10_cudaprueba.ii:(.text+0x4dc1): undefined reference to `__cudaMutexOperation’

tmpxft_00006955_00000000-10_cudaprueba.ii:(.text+0x4de2): undefined reference to `__cudaMutexOperation’

cudaprueba.o: In function `__uAtomicAdd’:

tmpxft_00006955_00000000-10_cudaprueba.ii:(.text+0x4df9): undefined reference to `__cudaMutexOperation’

tmpxft_00006955_00000000-10_cudaprueba.ii:(.text+0x4e1a): undefined reference to `__cudaMutexOperation’

cudaprueba.o: In function `__iAtomicExch’:

tmpxft_00006955_00000000-10_cudaprueba.ii:(.text+0x4e31): undefined reference to `__cudaMutexOperation’

cudaprueba.o:tmpxft_00006955_00000000-10_cudaprueba.ii:(.text+0x4e4d): more undefined references to `__cudaMutexOperation’ follow

cudaprueba.o: In function `__itexfetchi’:

tmpxft_00006955_00000000-10_cudaprueba.ii:(.text+0x75bc): undefined reference to `__cudaTextureFetch’

cudaprueba.o: In function `__utexfetchi’:

tmpxft_00006955_00000000-10_cudaprueba.ii:(.text+0x7606): undefined reference to `__cudaTextureFetch’

cudaprueba.o: In function `__ftexfetchi’:

tmpxft_00006955_00000000-10_cudaprueba.ii:(.text+0x7650): undefined reference to `__cudaTextureFetch’

cudaprueba.o: In function `__itexfetch’:

tmpxft_00006955_00000000-10_cudaprueba.ii:(.text+0x769a): undefined reference to `__cudaTextureFetch’

cudaprueba.o: In function `__utexfetch’:

tmpxft_00006955_00000000-10_cudaprueba.ii:(.text+0x76e4): undefined reference to `__cudaTextureFetch’

cudaprueba.o:tmpxft_00006955_00000000-10_cudaprueba.ii:(.text+0x772e): more undefined references to `__cudaTextureFetch’ follow

cudaprueba.o: In function `_device_stub__Z7funcionPfS’:

tmpxft_00006955_00000000-10_cudaprueba.ii:(.text+0xd343): undefined reference to `cudaSetupArgument’

tmpxft_00006955_00000000-10_cudaprueba.ii:(.text+0xd369): undefined reference to `cudaSetupArgument’

cudaprueba.o: In function `__sti____cudaRegisterAll_45_tmpxft_00006955_00000000_4_c

udaprueba_cpp1_ii_bd1fe0ee’:

tmpxft_00006955_00000000-10_cudaprueba.ii:(.text+0xd3bd): undefined reference to `__cudaRegisterFatBinary’

tmpxft_00006955_00000000-10_cudaprueba.ii:(.text+0xd424): undefined reference to `__cudaRegisterFunction’

cudaprueba.o: In function `cudaError cudaLaunch(char*)’:

tmpxft_00006955_00000000-10_cudaprueba.ii:(.text.Z10cudaLaunchIcE9cudaErrorPT[cudaE

rror cudaLaunch(char*)]+0xd): undefined reference to `cudaLaunch’

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

collect2: ld returned 1 exit status[/codebox]

It seems that, on one side, it can’t link the .cu and .c files and, on the other, can’t link the .cu with the CUDA library (however, I can compile .cu files on its own).

Any help?

Thanks