Hey guys!
I’m trying to compile a very simple project divided in a .cu file and a .c file to make a test because I need to do something like that for a bigger job. But it doesnt work I don’t know why. Here you go the code:
main.c
void cmal();
int main()
{
cmal();
return 0;
}
cmal.cu
#define SIZE 10
#include <stdio.h>
// Kernel definition
global void vecAdd(float* A, float* B, float* C)
{
// threadIdx.x is a built-in variable provided by CUDA at runtime
int i = threadIdx.x;
A[i]=0;
B[i]=i;
C[i] = A[i] + B[i];
}
void cmal()
{
int N=SIZE;
float A, B, C;
float *devPtrA;
float *devPtrB;
float *devPtrC;
int memsize= SIZE * sizeof(float);
cudaMalloc((void**)&devPtrA, memsize);
cudaMalloc((void**)&devPtrB, memsize);
cudaMalloc((void**)&devPtrC, memsize);
cudaMemcpy(devPtrA, A, memsize, cudaMemcpyHostToDevice);
cudaMemcpy(devPtrB, B, memsize, cudaMemcpyHostToDevice);
vecAdd<<<1, N>>>(devPtrA, devPtrB, devPtrC);
cudaMemcpy(C, devPtrC, memsize, cudaMemcpyDeviceToHost);
for (int i=0; i<SIZE; i++)
printf("C[%d]=%f\n",i,C[i]);
cudaFree(devPtrA);
cudaFree(devPtrB);
cudaFree(devPtrC);
}
Command to compile:
nvcc -c cmal.cu
g++ -lcudart cmal.o main.c -L/usr/local/cuda/lib
Error:
cmal.o: In function `cmal()':
tmpxft_00001475_00000000-1_cmal.cudafe1.cpp:(.text+0x25): undefined reference to `cudaMalloc’
tmpxft_00001475_00000000-1_cmal.cudafe1.cpp:(.text+0x37): undefined reference to `cudaMalloc’
tmpxft_00001475_00000000-1_cmal.cudafe1.cpp:(.text+0x49): undefined reference to `cudaMalloc’
tmpxft_00001475_00000000-1_cmal.cudafe1.cpp:(.text+0x6a): undefined reference to `cudaMemcpy’
tmpxft_00001475_00000000-1_cmal.cudafe1.cpp:(.text+0x8e): undefined reference to `cudaMemcpy’
tmpxft_00001475_00000000-1_cmal.cudafe1.cpp:(.text+0x111): undefined reference to `cudaConfigureCall’
tmpxft_00001475_00000000-1_cmal.cudafe1.cpp:(.text+0x152): undefined reference to `cudaMemcpy’
tmpxft_00001475_00000000-1_cmal.cudafe1.cpp:(.text+0x196): undefined reference to `cudaFree’
tmpxft_00001475_00000000-1_cmal.cudafe1.cpp:(.text+0x1a1): undefined reference to `cudaFree’
tmpxft_00001475_00000000-1_cmal.cudafe1.cpp:(.text+0x1ac): undefined reference to `cudaFree’
cmal.o: In function `__cudaUnregisterBinaryUtil()':
tmpxft_00001475_00000000-1_cmal.cudafe1.cpp:(.text+0x1c1): undefined reference to `__cudaUnregisterFatBinary’
cmal.o: In function `_device_stub__Z6vecAddPfS_S(float*, float*, float*)':
tmpxft_00001475_00000000-1_cmal.cudafe1.cpp:(.text+0x1e4): undefined reference to `cudaSetupArgument’
tmpxft_00001475_00000000-1_cmal.cudafe1.cpp:(.text+0x208): undefined reference to `cudaSetupArgument’
tmpxft_00001475_00000000-1_cmal.cudafe1.cpp:(.text+0x22c): undefined reference to `cudaSetupArgument’
cmal.o: In function `__sti____cudaRegisterAll_39_tmpxft_00001475_00000000_4_cmal_cpp1_ii_a311767b()':
tmpxft_00001475_00000000-1_cmal.cudafe1.cpp:(.text+0x28a): undefined reference to `__cudaRegisterFatBinary’
tmpxft_00001475_00000000-1_cmal.cudafe1.cpp:(.text+0x2f1): undefined reference to `__cudaRegisterFunction’
cmal.o: In function `cudaError cudaLaunch(char*)':
tmpxft_00001475_00000000-1_cmal.cudafe1.cpp:(.text.Z10cudaLaunchIcE9cudaErrorPT[cudaError cudaLaunch(char*)]+0xd): undefined reference to `cudaLaunch’
collect2: ld returned 1 exit status
I looked around for the forum without finding a solution. I also tried to add -lcuda as option but nothing changed. Do you have any idea how to solve it?
Thanks a lot!
Giacomo