user400055
user400055

Reputation:

Visual Studio Linking Problem with Cuda

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?

Upvotes: 6

Views: 2673

Answers (2)

Tom
Tom

Reputation: 21108

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"
}

Upvotes: 2

karlphillip
karlphillip

Reputation: 93410

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:

How do I start a new CUDA project in Visual Studio 2008?

Upvotes: 0

Related Questions