Calling a function located in *.cu file in a standard *.c file in Visual Studio 2008

Hi all;

I have a problem here in calling the function located in *.cu file in *.c file in Visual Studio 2008.

Here is the program structure :

kernel.cuh
network.h

network.c
kernel.cu
main.c

In kernel.cu, I have a function called AddVector() and I would like it to execute it in the main.c file together with some of the CPU code.

Here is my code snippet in main.c file

#include “kernel.cuh”
#include “network.h”

int main() {

AddVector();   // function located in kernel.cu file 
DisplayValue();  // standard C code declare in network.h and implement in network.c 
return 0;

}

In kernel.cuh

#include “cuda_runtime.h”
#include “device_launch_parameters.h”

#include <stdio.h>

cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size);
global void addKernel(int *c, const int *a, const int *b);
int AddVector();

When I try to compile, I got the following error message below.

1>main.obj : error LNK2019: unresolved external symbol _AddVector referenced in function _main
1>G:\MEHB1BPA_ME121104\FYP\SourceCode\Main\Debug\Main.exe : fatal error LNK1120: 1 unresolved externals

What is the setting that I missed out and I should check ?
Can anyone here give some shed of light ??

-Wen Jian-

Additional notes: I’m using CUDA Toolkit version 5.5

And would it all work when You put Your code into cu/cuh files? Seems like the code written in normal C and one in CUDA C waren’t linked together.

MK

It work when i put my code into all cu / cuh files.

Is there a way to get the code written in normal C and one in CUDA C linked together in VIsual Studio 2008 ?

Only one I know is to use driver API. But this means You are not actally linking it together, like in a typical compilation. The host part is compiled with normal compiler, while any cu/cuh files are compiled with nvcc. The device code is compiled to PTX (mostly) in such a scenario and then loaded as modules to an app, very much like the GLSL shdares are loaded from a text file. This separates device and host code completely and is more portable, I think.

I dunno whether can it be done anyhow else. When I do programming in runtime API I always put all my code to cu/cuh files.

Cheers,
MK

CUDA code in a .cu file compiled with nvcc is processed as C++ code, not C code. This means that all function names get decorated according to C++ rules, and therefore do not match the function names that the C compiler uses. That problem is common to any mix of C and C++ sources, so C++ has a mechanism that forces functions to conform to C naming convention. Such functions are declared as extern “C”. It’s easiest to apply that to the function prototype in the header file, then include the header file into both your .c file and your .cu file.

You will not be able to call a global function directly from C code. In your .cu file, create a wrapper function with extern “C” attribute that invokes the global function. You can then call the wrapper function from your c. file.

Hi all;

Thanks :)
This one I able to solve it ! … using the wrapper function with extern “C” attribute :)