Search code examples
c++visual-studio-2010cudanvcc

Unable to compile CUDA C sources. Simple version is provided


Here is the problem... For school project I need to write parallel application using CUDA C. Even the most simple example will not compile. I'm using Windows7 and MS visual studio. The code is taken from the book: CUDA by example. An introduction to general purpose GPU computing.

#include<iostream>
#include<cuda.h>

using namespace std;

__global__ void kernel(void){
}

int main(){
kernel<<<1, 1>>>();
cout << "Hello world" << endl;
return 0;
}

Here are the errors:

1>c:\users\administrator\documents\visualstudio2010\projects\test\test\test.cpp(6): error C2144: syntax error : 'void' should be preceded by ';'
1>c:\users\administrator\documents\visualstudio2010\projects\test\test\test.cpp(6): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\administrator\documents\visualstudio2010\projects\test\test\test.cpp(10): error C2059: syntax error : '<'

Do I need to set nvcc.exe as default compiler instead of cl.exe? If that is the case, how to do it? Any help is much appreciated!


Solution

  • CUDA code needs to be written in a .cu file and compiled with the NVCC compiler. You are seeing the above errors because you have written your code in a .c or .cpp file and are trying to compile it with a C++ compiler (the Visual C++ compiler).

    You have chosen the right book to learn CUDA from. However, you are not following all the steps given in the book. Please have a look at the details of compilation in the book :-)