Reputation: 24795
I have wrote a cuda application which has a main.cpp
that includes a Common.h
file
#include "Common.h"
int main(int argc , char **argv)
{
...
DeviceFunc(a_h , numvar , b_h); //Showing the data
....
}
Then, Common.h
contains:
#ifndef __Common_H
#define __Common_H
#endif
void DeviceFunc(float * , int , float *);
Also, DeviceFunc.cu
is in the same folder:
#include<cuda.h>
#include<stdio.h>
#include "Common.h"
__device__ __global__ void Kernel(float *, float * ,int );
void DeviceFunc(float *temp_h , int numvar , float *temp1_h)
{
float *a_d , *b_d;
//Memory allocation on the device
cudaMalloc(&a_d,sizeof(float)*(numvar)*(numvar+1));
cudaMalloc(&b_d,sizeof(float)*(numvar)*(numvar+1));
//Copying data to device from host
cudaMemcpy(a_d, temp_h, sizeof(float)*numvar*(numvar+1),cudaMemcpyHostToDevice);
//Defining size of Thread Block
dim3 dimBlock(numvar+1,numvar,1);
dim3 dimGrid(1,1,1);
//Kernel call
Kernel<<<dimGrid , dimBlock>>>(a_d , b_d , numvar);
//Coping data to host from device
cudaMemcpy(temp1_h,b_d,sizeof(float)*numvar*(numvar+1),cudaMemcpyDeviceToHost);
//Deallocating memory on the device
cudaFree(a_d);
cudaFree(b_d);
}
}
Now when I compile the code with nvcc -o main main.cpp
, I get this error main.cpp:(.text+0x3a0): undefined reference to 'DeviceFunc(float*, int, float*)'
What is the problem
Upvotes: 1
Views: 5500
Reputation: 5470
Undefined function reference occurs when the compiler finds the prototype of the function and don't find the reference to the function during link. To avoid this linking error, you should 1) compile-link whole files at one command, or 2) separate the compile and link process. I recommend the latter as follows:
nvcc -c main.cpp
nvcc -c DeviceFunc.cu
nvcc -c Kernel.cu
nvcc main.o DeviceFunc.o Kernel.o -o main
NOTICE that your shown codes miss the file containing the body Kernel
function. I've supposed the body of Kernel
function is included in Kernel.cu
.
Upvotes: 4