general use of nvcc

I am relatively new to cuda and tried some examples which work fine.

Now I have an own c++ program, where I want to export some calculations to the gpu.

Beginning with small steps I tried to compile the c++ code using nvcc without any changes for gpu implementation,
which means: I tried to compile the same code I used for the gcc compiler.

After hundreds of errors my terminal stoped and i recognized that it wont be as easy as i hoped…

Without referring to my project,
isnt it possible to use the nvcc as a normal c++ compiler without any cuda programming ? Or has it just few different syntaxes ?

And do you know some documented programming samples, where normal c++ programs got some cuda implementation ?

Thanks for any help and best regards

For starters, nvcc (more correctly nvopencc, nvcc is just a compiler driver) is officially a C compiler which supports a small subset of C++ features (mostly templating). That alone means what you are trying to do won’t work, but the are a whole slew of other reasons why the whole notion of what you are trying to do is doomed to fail.

It might be time to look at some working CUDA code (from the SDK) to get a feel for the programming model and host/device roles, and then read the documentation.

  1. From reading other forums posts, I think avidday knows far more on this subject than I do.

  2. I would try just giving the code the fileending .cu instead of c/cpp/whatever and see what happens. I don’t have nvcc here at the moment, but IIRC that’s been an issue for me.

  3. There not actual need to compile your code with nvcc. Add a trivial hello world function to a, let nvcc deal with that, and g++ with the rest, link them together.

that sounds really good!

now for example I write a “hello World.c” with a simple hello world output and for the nvcc I use the

can you tell me how i link them together ?

thanks a lot !

You can’t. also contains a main() declaration and you would get a linking error.

In general, you just use gcc to link object files(whether from gcc or nvcc) exactly as you would with any program composed of multiple object files.

okay, sounds like I have to do some homework!

in general my program looks like this:

include header.h
include header2.h

code like hello World

algorithm for some simulation

code for the output

in this case i would prefer to write an and link it to the main program code.

I still dont know exactly how to compile both files together but I will try

The key point is that you don’t compile them together, you link the resulting object code together. Like this

nvcc -c -o

g++ -c -o -o

g++ -L<path to cuda libraries> -o myprog -lcudart

that is very helpful !

I will come back after some tries and hope to be able to offer some working results.

thank you for fast response !

If you’re familiar with the C++ STL then you will want to take a look at Thrust which provides vector containers and many algorithms.

If you have any questions about Thrust feel free to ask them on our mailing list.