1

I've recently purchased the CUDA C By Example book, and I'm trying to get setup. I downloaded the toolkit and am trying to run this bit of simple code:

#include <stdio.h>

__global__ void add(int a, int b, int * c) 
{
    *c = a + b;
}

int main(void)
{
    printf("Hello World!");

    return 0;
}

The errors I'm getting are:

expected '(' to follow '__global__'                          line 2
'add' not in formal parameters list                          line 3
syntax error: missing ';' before '{'                         line 3
expected a ';'                                               line 3

Not sure what I'm doing wrong. Is there some #include statement that I'm missing? The book did not include one, and the examples I've read online don't seem to have another one either. For a bit more info, I made a Visual C++ project in Visual Studio, and made the file have a '.c' extension vs '.cpp'.

Any help would be appreciated, I really want to start working with this stuff :D

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
  • In visual studio, you need to specifically be in a CUDA C/C++ project (not an ordinary c/c++ project), and your file name should end in `.cu`, not `.c` or `.cpp`. I would suggest following the instructions in the [windows install guide](http://docs.nvidia.com/cuda/cuda-installation-guide-microsoft-windows/index.html#abstract) carefully, noting that the CUDA toolkit should be installed *after* VS is installed. Go through the verification steps, which involve building and running some sample projects. – Robert Crovella May 07 '16 at 00:48
  • @Matthew: The tookit contains a lot of samples. E.g. `CUDA Samples\v7.5\0_Simple\vectorAdd` is the "Hello World" of CUDA, and comes with properly configured VS project files that may help to get started. – Marco13 May 07 '16 at 01:36
  • @Robert Crovella Thank you very much! None of this was mentioned in the book I bought, which confuses the hell out of me. Working on getting the files verified, and I'll try and run some of the sample code as well. – Matthew Freihofer May 07 '16 at 14:38

1 Answers1

-1

your file should have ".cu" extension and you should include #include <host_defines.h> or more preferably #include <cuda_runtime.h> for CUDA programs.

swapnilsj
  • 31
  • 2
  • 2
    It is never necessary to include of the runtime API definition headers into a `.cu` file compiled with nvcc. The compiler will automatically include everything to supported kernel code and the host runtime APIs. Doing so can lead to unexpected behaviour unless you know *exactly* what you are doing. – talonmies May 07 '16 at 16:30