Under The Wire

Under The Wire

Wednesday, April 15, 2009

Integrate a CUDA kernel in your projects

Recently I told you how to setup a Virtual Machine with VirtualBox running openSUSE (Yeah, this time I spelled it right).

As I wrote on the previous post all the mess was due to a project with nVidia CUDA, porting some data intensive tasks on the GPU.

Working on an existing project I had some troubles integrating the new code with the existing sources editing the makefile.

To build your executable with a CUDA kernel you have to do this:

  • Compile CUDA sources with nvcc (You have to install CUDA toolkit to have it), generating an object file.
    Be sure that when you include functions headers from your C project into the CUDA sources you state that what you are importing is C code. To do that use extern “C” {#incude “whateverYouWant.h”} and it will be fine.
    $ nvcc --compiler-options -fno-strict-aliasing -I. -I/opt/cuda/NVIDIA_CUDA_SDK/common/inc -I/opt/cuda/include -o objectCUDA.o -c SOURCE.cu
  • Compile the project C code with gcc, producing the object files, as you do usually.
  • Link all together, using gcc. Be sure that you link all the required libraries.
    gcc -o EXECUTABLE object1.o object2.o objectCUDA.o -L/opt/cuda/lib -lcudart -L/opt/cuda/NVIDIA_CUDA_SDK/lib -lcutil

With this tips you should be able to edit easily your project makefile and have all the things working.


Abhijit Kiran

Hi, I liked your post, because it seems to clarify a doubt I have.

My friend and I are working on porting an existing library to NVIDIA CUDA. The library has a GNU Build System defined for its compilation and installation. We have suitably edited the configure.ac and Makefile.am files to compile with nvcc instead of gcc. Our aim is to generate a shared library.

We also used "nvcc" for the linking stage, which caused errors as certain flags generated in the libtool.sh file which is in turn generated by configure.ac file aren't recognized by nvcc.

My doubt is whether we can simply use gcc for linking. Please Help!!
Extremely sorry for the long comment.


I'm sorry but I can't answer to your question. I don't know if it possible to use the gcc in the linking stage.

I suggest you to ask this question on http://forums.nvidia.com/ where is more likely you will find somebody that knows the answer to your question.

Abhijit Kiran

Alright. Thanks!



I have been successful in integrating my Cuda kernel in my project, but i am not able to use the CUDA profiler on it which gives "Unable to read Output" as the error. Any suggestions ?


price per head service

Nice post, I would like to request you to one more post about that Keep it up

  © Blogger template 'Minimalist E' by Ourblogtemplates.com 2008

Back to TOP