Compiling error: undefined reference to `cudaMalloc'

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

Try

g++ main.c cmal.o -L/usr/local/cuda/lib -lcudart

or if you are on a 64bit system

g++ main.c cmal.o -L/usr/local/cuda/lib64 -lcudart

You can also use nvcc to compile all your code

Oh it was such a stupid mistake…

Thank for the help!

Hi,
I guess the issue comes from the order of linking: the linker resolves missing dependences into libraries from left to right only, and since cudaMalloc is undefined in cmal.o, which is after -lcudart, the dependency cannot be satisfied.
Try this:
g++ main.c cmal.o -L/usr/local/cuda/lib -lcudart -lcuda

HTH

Gilles

Yes, it was totally that the issue. Now it works.
Thanks again.

#include<stdio.h>
#include<cuda.h>

global void matrix(int *ad,int *bd, int *cd,int n)
{

int row,col,i,m,p;
int pvalue=0;
  
row=blockIdx.y*blockDim.y+threadIdx.y;
col=blockIdx.x*blockDim.x+threadIdx.x;
for(i=0;i<n;i++)
{
	m=ad[row*n+i];
	p=bd[i*n+col];
	pvalue=pvalue+(m*p);

}
cd[row*n+col]=pvalue;

}

int main()
{
int *a,*b,*c,n;
unsigned int i,j;

int *ad,*bd,*cd;
printf("size?\n");
scanf("%d",&n);

size_t size=n*n*sizeof(int);
a=(int *)malloc(size);

b=(int *)malloc(size);

c=(int *)malloc(size);


cudaMalloc(&ad,size);
cudaMelloc(&bd,size);
cudaMelloc(&cd,size);


printf("1st matrix?\n");
for(i=0;i<n;i++)
{
	for(j=0;j<n;j++)
	{

         scanf("%d",&a[i*n+j]);
	}


}


printf("2nd matrix?\n");
for(i=0;i<n;i++)
{
	for(j=0;j<n;j++)
	{

         scanf("%d",&b[i*n+j]);
	}


}

cudaMemcpy(ad,a,size,cudaMemcpyHostToDevice);

cudaMemcpy(bd,b,size,cudaMemcpyHostToDevice);

dim3 gridsize(n/2,n/2);
dim3 blocksize(2,2);

matrix<<<gridsize,blocksize>>>(ad,bd,cd,n);

cudaMemcpy(c,dc,size,cudaMemcpyDeviceToHost);
printf(“result\n”);

for(i=0;i<n;i++)
{
	for(j=0;j<n;j++)
	{

         printf("%d",c[i*n+j]);
	}


}

free(a);
free(b);
free(c);
cudaFree(ad);
cudaFree(bd);
cudaFree(cd);
enter code here
return(0);

}
please find the errors…im unable to find! :(

Is this a joke? Did you just type this code into your browser?

Because the compiler will find these errors for you:

cudaMelloc(&bd,size);
cudaMelloc(&cd,size);

and:

enter code here

Anyway, after you’ve fixed the compile errors, start by adding proper cuda error checking to your code (google: “proper cuda error checking” then take the first hit, then study it.) Then run your code with cuda-memcheck.

im getting the errors in compiler…what i meant is …i find no mistak in dis code…but still errors are coming…so just pasted the code over here!

Error 1 error : identifier “cudaMelloc” is undefined c:\Users\Lenovo\documents\visual studio 2012\Projects\monu\monu\kernel.cu 46 1 monu
Error 2 error MSB3721: The command ““C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v6.5\bin\nvcc.exe” -gencode=arch=compute_20,code=“sm_20,compute_20” --use-local-env --cl-version 2012 -ccbin “C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin” -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v6.5\include” -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v6.5\include" -G --keep-dir Debug -maxrregcount=0 --machine 32 --compile -cudart static -g -DWIN32 -D_DEBUG -D_CONSOLE -D_MBCS -Xcompiler “/EHsc /W3 /nologo /Od /Zi /RTC1 /MDd " -o Debug\kernel.cu.obj “c:\Users\Lenovo\documents\visual studio 2012\Projects\monu\monu\kernel.cu”” exited with code 2. C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V110\BuildCustomizations\CUDA 6.5.targets 593 9 monu
9 IntelliSense: expected a ‘)’ c:\Users\Lenovo\Documents\Visual Studio 2012\Projects\monu\monu\kernel.cu 79 14 monu
12 IntelliSense: expected a ‘)’ c:\Users\Lenovo\Documents\Visual Studio 2012\Projects\monu\monu\kernel.cu 81 18 monu
14 IntelliSense: expected a ‘)’ c:\Users\Lenovo\Documents\Visual Studio 2012\Projects\monu\monu\kernel.cu 82 17 monu
19 IntelliSense: expected a ‘)’ c:\Users\Lenovo\Documents\Visual Studio 2012\Projects\monu\monu\kernel.cu 88 13 monu
4 IntelliSense: expected a ‘;’ c:\Users\Lenovo\Documents\Visual Studio 2012\Projects\monu\monu\kernel.cu 5 12 monu
16 IntelliSense: expected a ‘;’ c:\Users\Lenovo\Documents\Visual Studio 2012\Projects\monu\monu\kernel.cu 85 7 monu
5 IntelliSense: expected a declaration c:\Users\Lenovo\Documents\Visual Studio 2012\Projects\monu\monu\kernel.cu 51 2 monu
6 IntelliSense: expected a declaration c:\Users\Lenovo\Documents\Visual Studio 2012\Projects\monu\monu\kernel.cu 64 2 monu
21 IntelliSense: expected a declaration c:\Users\Lenovo\Documents\Visual Studio 2012\Projects\monu\monu\kernel.cu 91 2 monu
32 IntelliSense: expected a declaration c:\Users\Lenovo\Documents\Visual Studio 2012\Projects\monu\monu\kernel.cu 109 2 monu
33 IntelliSense: expected a declaration c:\Users\Lenovo\Documents\Visual Studio 2012\Projects\monu\monu\kernel.cu 111 1 monu
27 IntelliSense: identifier “ad” is undefined c:\Users\Lenovo\Documents\Visual Studio 2012\Projects\monu\monu\kernel.cu 105 11 monu
23 IntelliSense: identifier “b” is undefined c:\Users\Lenovo\Documents\Visual Studio 2012\Projects\monu\monu\kernel.cu 103 7 monu
8 IntelliSense: identifier “bd” is undefined c:\Users\Lenovo\Documents\Visual Studio 2012\Projects\monu\monu\kernel.cu 79 12 monu
29 IntelliSense: identifier “bd” is undefined c:\Users\Lenovo\Documents\Visual Studio 2012\Projects\monu\monu\kernel.cu 106 11 monu
18 IntelliSense: identifier “c” is undefined c:\Users\Lenovo\Documents\Visual Studio 2012\Projects\monu\monu\kernel.cu 88 12 monu
25 IntelliSense: identifier “c” is undefined c:\Users\Lenovo\Documents\Visual Studio 2012\Projects\monu\monu\kernel.cu 104 7 monu
31 IntelliSense: identifier “cd” is undefined c:\Users\Lenovo\Documents\Visual Studio

these are the errors im getting!!

There is no function “cudaMelloc” (with an ‘e’). There is only “cudaMalloc” (with an ‘a’).

found it !!..thank you!

got it!! now the program is running!!

Is there a tool included with the CUDA toolkit that will check makefiles and identify linking errors?