Linking problem with nvcc

I’m afraid I’m misunderstading some basic issue about linking with nvcc. In short, nvcc wouldn’t link the compiled object files and instead throws an ‘undefined reference’ error in a situation that would be perfectly fine with C and gcc.

In order to keep it simple I’ve taken the matrix multiplication example from the docs (7.2) and added a new function called ‘test’ that does nothing: void test( void ) { } All of this is in a file

This is test2.c:

void test( void );

int main( int argc, char ** argv )


    test( );

    return 0;


So it really does nothing else than calling the empty function that is defined in the other file. Then both and test2.c was compiled with nvcc -c producing test.o and test2.o

which I tried linking with nvcc -o test test.o test2.o but it complained about an undefined reference to `test’.

Any ideas?

Nvcc is mangling the name, so you need to use extern “C” if you want to link with gcc.

extern "C"


void test_cuda( void )




void test_cuda( void );

int main( int argc, char ** argv )


   test_cuda( );

   return 0;

nvcc -c

gcc test_cuda.o test.c -L/usr/local/cuda/lib -lcudart


I think it is easier if you only compile .cu files with nvcc and normal files with the appropriate compiler. Then link everything together with your normal linker and just pass it the cuda libraries: -lcuda -lcudart -lcutil, I think.

And @mfatica, I think in the code he displayed in his origianl post, that was only a function prototype with the actual function contained in a seperate file…

Massimiliano, thanks, that worked.

Eelsen, thanks also for the comment, the code actually was the whole function, an empty one, it was just for the sake of testing. Anyway Massimiliano’s suggestion works fine, BTW the linker options are only -lcuda -lcudart without -lcutil.

You need cutil if you use any of the functionality in the common directory of the SDK. Bank checker, timer, etc…

I see, thanks, I didn’t install the SDK yet, that’s why the cutil was not found :)

I’m having problems linking cutil library. Every cutil library function that I use in my code complains as an undefined reference. For example:

tmpxft_00002b99_00000000-8.i:(.text+0x6ca7): undefined reference to `cutCreateTimer’

I’ve compiled CUDA SDK and am able to run all the examples. I also have “libcutil.a”. Somehow I’m not able to link libcutil.a library. Did anyone come across this problem ?

[s]I had the same problem until I compiled the SDK/common, which you obviously have. maybe it would help to take a look at the link line, mine looks like this

“g++ -fPIC -o …/…/bin/linux/release/matrixMul obj/release/matrixMul_gold.cpp_o obj/release/matrixMul.cu_o -L/usr/local/cuda/lib -L…/…/lib -L…/…/common/lib -lcuda -lcudart -lGL -lGLU -lcutil”

I think “make verbose=1” will work, though I commented out the $(VEBOSE) on the link line.

also try doing an ls on /lib. I have libcutil.a at ~148kB.[/s]

EDIT: I should read what you wrote… make sure you have the correct -L flag, and then you might need to edit LD_LIBRARY_PATH or edit /etc/ and run ldconfig.