# Compiling error: undefined reference to \`cudaMalloc'

**URL:** <https://forums.developer.nvidia.com/t/compiling-error-undefined-reference-to-cudamalloc/25944>\
**Category:** CUDA Programming and Performance\
**Created:** [March 1, 2012, 2:30pm UTC](https://forums.developer.nvidia.com/t/compiling-error-undefined-reference-to-cudamalloc/25944 "2012-03-01T14:30:19Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![giajj](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@giajj](https://forums.developer.nvidia.com/u/giajj)\
**Post date:** [March 1, 2012, 2:30pm UTC](https://forums.developer.nvidia.com/t/compiling-error-undefined-reference-to-cudamalloc/25944/1 "2012-03-01T14:30:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mfatica](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@mfatica](https://forums.developer.nvidia.com/u/mfatica)\
**Post date:** [March 1, 2012, 3:18pm UTC](https://forums.developer.nvidia.com/t/compiling-error-undefined-reference-to-cudamalloc/25944/2 "2012-03-01T15:18:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![giajj](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@giajj](https://forums.developer.nvidia.com/u/giajj)\
**Post date:** [March 1, 2012, 3:22pm UTC](https://forums.developer.nvidia.com/t/compiling-error-undefined-reference-to-cudamalloc/25944/3 "2012-03-01T15:22:52Z")

</div>

Oh it was such a stupid mistake…

Thank for the help!

---

<div class="post-metadata">

**Author:** ![Gilles\_C](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Gilles\_C](https://forums.developer.nvidia.com/u/Gilles_C)\
**Post date:** [March 1, 2012, 3:23pm UTC](https://forums.developer.nvidia.com/t/compiling-error-undefined-reference-to-cudamalloc/25944/4 "2012-03-01T15:23:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![giajj](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@giajj](https://forums.developer.nvidia.com/u/giajj)\
**Post date:** [March 1, 2012, 3:37pm UTC](https://forums.developer.nvidia.com/t/compiling-error-undefined-reference-to-cudamalloc/25944/5 "2012-03-01T15:37:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mon\_ikag](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Mon\_ikag](https://forums.developer.nvidia.com/u/Mon_ikag)\
**Post date:** [April 22, 2015, 5:43pm UTC](https://forums.developer.nvidia.com/t/compiling-error-undefined-reference-to-cudamalloc/25944/6 "2015-04-22T17:43:39Z")

</div>

#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! :(

---

<div class="post-metadata">

**Author:** ![Robert\_Crovella](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/robert_crovella/32/14043_2.png) [@Robert\_Crovella](https://forums.developer.nvidia.com/u/Robert_Crovella)\
**Post date:** [April 22, 2015, 6:09pm UTC](https://forums.developer.nvidia.com/t/compiling-error-undefined-reference-to-cudamalloc/25944/7 "2015-04-22T18:09:16Z")

</div>

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

Because the compiler will find these errors for you:

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

```

and:

```auto
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.

---

<div class="post-metadata">

**Author:** ![Mon\_ikag](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Mon\_ikag](https://forums.developer.nvidia.com/u/Mon_ikag)\
**Post date:** [April 22, 2015, 11:38pm UTC](https://forums.developer.nvidia.com/t/compiling-error-undefined-reference-to-cudamalloc/25944/8 "2015-04-22T23:38:03Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Mon\_ikag](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Mon\_ikag](https://forums.developer.nvidia.com/u/Mon_ikag)\
**Post date:** [April 22, 2015, 11:44pm UTC](https://forums.developer.nvidia.com/t/compiling-error-undefined-reference-to-cudamalloc/25944/9 "2015-04-22T23:44:43Z")

</div>

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!!

---

<div class="post-metadata">

**Author:** ![njuffa](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@njuffa](https://forums.developer.nvidia.com/u/njuffa)\
**Post date:** [April 23, 2015, 12:15am UTC](https://forums.developer.nvidia.com/t/compiling-error-undefined-reference-to-cudamalloc/25944/10 "2015-04-23T00:15:25Z")

</div>

> [@](#):
>
> Error 1 error : identifier “cudaMelloc” is undefined

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

---

<div class="post-metadata">

**Author:** ![Mon\_ikag](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Mon\_ikag](https://forums.developer.nvidia.com/u/Mon_ikag)\
**Post date:** [April 23, 2015, 1:01am UTC](https://forums.developer.nvidia.com/t/compiling-error-undefined-reference-to-cudamalloc/25944/11 "2015-04-23T01:01:22Z")

</div>

found it !!..thank you!

---

<div class="post-metadata">

**Author:** ![Mon\_ikag](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Mon\_ikag](https://forums.developer.nvidia.com/u/Mon_ikag)\
**Post date:** [April 23, 2015, 1:02am UTC](https://forums.developer.nvidia.com/t/compiling-error-undefined-reference-to-cudamalloc/25944/12 "2015-04-23T01:02:26Z")

</div>

got it!! now the program is running!!

---

<div class="post-metadata">

**Author:** ![andorjkiss](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/andorjkiss/32/13041_2.png) [@andorjkiss](https://forums.developer.nvidia.com/u/andorjkiss)\
**Post date:** [December 19, 2019, 9:16pm UTC](https://forums.developer.nvidia.com/t/compiling-error-undefined-reference-to-cudamalloc/25944/13 "2019-12-19T21:16:10Z")

</div>

> [@Robert\_Crovella](#):
>
> Is this a joke? Did you just type this code into your browser?
> 
> Because the compiler will find these errors for you:
> 
> ```auto
> cudaMelloc(&bd,size);
> cudaMelloc(&cd,size);
> 
> ```
> 
> and:
> 
> ```auto
> 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.

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

---

<div class="post-metadata">

**Author:** ![user122569](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@user122569](https://forums.developer.nvidia.com/u/user122569)\
**Post date:** [November 2, 2022, 5:11pm UTC](https://forums.developer.nvidia.com/t/compiling-error-undefined-reference-to-cudamalloc/25944/14 "2022-11-02T17:11:13Z")

</div>

Probably my favorite forum post ever.
