views:

156

answers:

2

I am doing some programming with nVidia's CUDA C. I am using Visual Studio 2008 as my development environment and I am having some troubles with some linking and I am wondering if someone knows a way to fix it or has had the same problem and could offer a solution.

My program is made up of 3 files. 1 header file (stuff.h), 1 C source file (stuff.c) and 1 CUDA C file (main.cu). (The names are fake but it's just to illustrate the point).

Now stuff.h/stuff.c define/implement some helper functions that I call from inside main.cu.

I am using visual studio 2008 and the Cuda.rules from nVidia's GPU Computing SDK and everything compiles fine but... when it comes to linking all of the files together it fails. It seems that all of the functions defined in stuff.h (and implemented in stuff.c) are not being linked in correctly as they are flagged as "unresolved external symbols".

What are the possible causes and how could I fix this?

Many thanks,

ExtremeCoder


Okay so I have managed to get it all compiling. It seems all I had to do was change the extension of the stuff implementation file from .c to .cpp (meaning compiling as c++ works whereas compiling as c does not!).

What could be causing this? I would rather keep everything as a .c instead of .cpp (as this is really meant to be C code...

Any pointers?

+1  A: 

There's a VS 2005 project that uses CUDA to convert images to their grayscale representation here. It uses OpenCV, though. If you have already installed it should be pretty straight forward.

But even if you don't have OpenCV and dont want to compile the application, VS 2008 can convert and open this project, and you'll be able to see how you should separate CUDA source code from the C/C++ code and how to configure the project properties correctly.

I should also point out this great thread:

http://stackoverflow.com/questions/2046228/how-do-i-start-a-new-cuda-app-in-visual-studio-2008

karlphillip
Thanks for the reply, it has helped me a bit further but not to what I hoped to achieve.I have actually taken a look at some other projects, messed a round a bit and it seems that if all I do is change the extension of stuff.c to stuff.cpp everything works fine.What could be causing this?Thanks.
ExtremeCoder
Good! Using .cpp extension is a problem for you? If you need help understanding your problem, we need you to edit your question and paste the error messages VS 2008 is giving you.
karlphillip
+2  A: 

The main.cu file is being processed by nvcc which, by default, is a C++ compiler (actually it's a wrapper around the underlying CUDA compiler and cl.exe, the default MS compiler). As a result it is looking for the functions with C++ binding, whereas by compiling them as C you have the C bindings.

If you want to keep your code as C then you can either edit stuff.h to declare the functions as extern "C":

/* in stuff.h */
if defined(__cplusplus)
    extern "C"
    {
#endif
/* ... your declarations ... */
if defined(__cplusplus)
    }
#endif

Or you can wrap the inclusion of stuff.h in main.cu:

// in main.cu
extern "C"
{
#include "stuff.h"
}
Tom
Thanks for the help. I got it working. Is there any way to make .cu files compile as C files and not as C++ files then?Thanks.
ExtremeCoder
You can try the `--host-compilation=c` option, but see this post for the caveats: http://forums.nvidia.com/index.php?showtopic=101609. In general I prefer to keep my `main` function and everything else in standard C/C++ files and then only have kernels and wrappers in CU files. A lot of the Thrust stuff also needs to go into CU files...
Tom