CUDA/C++ Compiler issue Call a CUDA function in a C++ main

Hello everyone,

I started using CUDA some weeks ago and I don’t have any problems when I use only the nvcc compiler but now I need to use nvcc and g++.

If I simplify my programs, I have 2 files : cppSource.cpp and cuSource.cu.

and

According to the g++ man. I had to compile cuSource.cu first and then cppSource,cpp. Thus, I’ve done the following :

But doing that, g++ considered “printHello” as undeclared and if I include “cuSource.cu” in cppSource.cpp. it looks for cuda.h and can’t go further.

If somebody understand what I’m doing wrong, that will help me a lot !

Thanks

You simply need a header file where you declare the printHello() function. Normally a header file for .cu files has a .cuh ending.

Ok thanks for the advice !

So now, I have also a lib "cuLib " that I called in both cuSource.cu and cppSource.cpp !

To compile them. I do :

And there I get a lots of “undefined references” to cuda stuff! Of course if I use nvcc instead of g++ for the last command it will work but I need g++ to create the final program and not nvcc !

I think it’s possible according to another thread (http://forums.nvidia.com/index.php?showtopic=106692) where timon has posted its makefile in witch he uses g++ to make the final compilation.

You try to link your object-files together, but the linker (ld is called from gcc/g++) does not know that you are using a library. So you need to specify the library with -l. If your library’s name is “libmyLibrary.so”, set the linker option “-lmyLibrary”. If you’re using the CUDA runtime API, always set “-lcudart” when linking.

Ok thanks again.

I must admit that I never tried before to understand better how a compiler works !

In my case, i’m using C for CUDA and not the CUDA runtime API, so I tried your solution (if I understood it correctly) and i just type

And the linker ld answers “cannot find -l-xc+±header”. According to this strange message, I think I did not really understand the library functioning.

I noticed that before :)

In that case, you are using the runtime API. Imagine the following example:

You have 3 source and 2 header files:

main.cpp

helloWorld.h

helloWorld.cpp

myFirstCudaKernel.cu // with some cuda code

myFirstCudaKernel.cuh

The header files are normally included in one of the source files, so you don’t need to pass them explicitly to the compiler. The .cu files use the CUDA runtime API (all cuda*() functions) and therefore you need to link against this library. Now you type:

g++ -c main.cpp -o main.o

g++ -c helloWorld.cpp -o helloWorld.o

nvcc -c myFirstCudaKernel.cu -o myFirstCudaKernel.o

g++ -lcudart main.o helloWorld.o myFirstCudaKernel.o -o myApplication

The last step does the linking. The option “-lcudart” tells the linker that the final program uses this library.

Ok thanks a lot for all these explanations !
It doesn’t work with the -lcudart but after some researches in the nvcc guide I found more details about it and it works when I enter the path of the library ( /usr/local/cuda/lib/libcudart.so ) !
That’s strange because my LD_LIBRARY_PATH contains /usr/loca/cuda/lib/ so I’m not supposed to have to write the whole path but the most important is that it’s now working !
Thanks again for your help !