14

in my test.cu file (cu file item type is CUDA C/C++)

__global__ void foo()
{
}

void CudaMain()
{

  foo<<<1,1>>>();
}

and in my test.cpp file

#include "mycuda.cu"

int main()
{

CudaMain();
return 0;

}

and compilator send me error "error c2059 syntax error ' <' " in test.cu file

Hristo Iliev
  • 72,659
  • 12
  • 135
  • 186
Alatriste
  • 527
  • 2
  • 6
  • 21
  • I think it's being parsed as operator << followed by <, try adding spaces like so: foo< < < 1,1 > > > – Borgleader Dec 11 '12 at 18:11
  • 3
    I have no clue how does CUDA work, but in this case, you're trying to compile (#include does, actually, insert the text from the .cu file in the .cpp) the 'test.cu' file with c++ compiler, which, apparently, cannot bear the `<<<` – nothrow Dec 11 '12 at 18:11
  • 1
    You should name your main test file `test_main.cu` (or something) and compile it with `nvcc`. – Hristo Iliev Dec 11 '12 at 18:14
  • I try foo< < < 1,1 > > > but not working – Alatriste Dec 11 '12 at 18:15
  • I rename file name from test.cu to test_main.cu but it's not working. how About CUDA rules in vs2010? – Alatriste Dec 11 '12 at 18:19
  • Before using CUDA, please read some documentation on both CUDA and C. You cannot compile CUDA-specific code with C++ compiler, it must be CUDA compiler (i.e. https://developer.nvidia.com/cuda-llvm-compiler). C also has declarations and definitions, and are not supposed to include one source file into another unless you know what you are doing... –  Dec 11 '12 at 18:22
  • I'm compiling cu file with NVCC compiler :)) I'm using CUDA Driver API but in Runtime API it's not woking. :) – Alatriste Dec 11 '12 at 18:26
  • 3
    Have you followed all the steps in the [getting started guide for windows](http://docs.nvidia.com/cuda/cuda-getting-started-guide-for-microsoft-windows/index.html)? You are including a .cu file into a .cpp file, but the ms vc build rules are compiling the .cpp file with cpp build rules, not .cu build rules. So it's tripping over the device code in a .cpp file. Rename your test.cpp to test.cu so that the device build (.cu) rules are used. – Robert Crovella Dec 11 '12 at 18:42

3 Answers3

4

Inclusion of CUDA source files in a C++ file doesn't work because this simply makes the CUDA source part of the C++ program code and regular C++ compilers do not understand CUDA syntax extensions. If you still want to keep your CUDA code separate from the non-CUDA C++ code, then you might want to look into separate compilation. CUDA source code can be compiled to regular object files, that can then be linked with other object files to produce an executable.

Modify the C++ code to read:

extern void CudaMain(void);

int main()
{
    CudaMain();
    return 0;
}

Compile the CUDA file with nvcc, the C++ code with your C++ compiler and then link the resulting object files with nvcc (you may also need to specify the standard C++ library in the link command):

$ nvcc -c -o test_cuda.o test.cu
$ g++ -c -o test_cpp.o test.cpp
$ nvcc -o test.exe test_cuda.o test_cpp.o -lstdc++

Edit: your question is about VS2010. May be you have to create custom build steps there.

Hristo Iliev
  • 72,659
  • 12
  • 135
  • 186
  • 1
    This would be applicable to the situation where c and c++ are being *linked* together. In this case, he is *including* the .cu file into the .cpp file, not linking them together. There are no `extern` function calls in this scenario. – Robert Crovella Dec 11 '12 at 18:45
  • Need I to copy cuda *.rules files into vs directory? – Alatriste Dec 11 '12 at 18:49
  • The cuda rules should already be set up properly if you followed the getting started instructions and installed cuda *after* installing ms vs. – Robert Crovella Dec 11 '12 at 18:53
  • Can somebody give me a simple runtime api kernel launching sample (+project file) .. please. – Alatriste Dec 11 '12 at 18:57
  • I'm doing all steps http://stackoverflow.com/questions/3778799/how-do-i-start-a-cuda-app-in-visual-studio-2010/7285235#7285235 – Alatriste Dec 11 '12 at 18:57
  • 2
    See my comment above on your question. Rename *all* your .cpp files to .cu – Robert Crovella Dec 11 '12 at 19:15
  • @RobertCrovella, my first suggestion was to rename the C++ file - see the 3rd comment to the OP. Still compiling CUDA and non-CUDA code separately is the better way to do it and such kind of source inclusion is a bad idea. – Hristo Iliev Dec 11 '12 at 20:15
  • I want to integrate CUDA into C++ application, can somebody give me a simple example about this? (project file) – Alatriste Dec 11 '12 at 20:23
  • @GiorgiGelava try this sample: [c++ Integration](http://docs.nvidia.com/cuda/cuda-samples/index.html#cpp-integration) – Robert Crovella Dec 11 '12 at 20:27
  • @HristoIliev Apologies I did not understand what you were trying to communicate. It's a sensible solution. – Robert Crovella Dec 11 '12 at 20:28
  • @RobertCrovella, no need to apologise. I had my answer completely stripped of its rationale. I've added one paragraph now to better communicate my idea. – Hristo Iliev Dec 11 '12 at 20:58
4

Based on the thread here: https://forums.developer.nvidia.com/t/cuda-build-error/52615/4

Your test file extension should be .cu as well, but if you're using MSCV rename does not enough you should create a new CUDA C/C++ source module in your VS project.

Also you should put spaces between the <> operators like.

foo< < <1,1> > >();

Because C++ cannot parse the <<<>>>.

marko1777
  • 51
  • 2
  • 7
2

I know this is an old question but I was searching around and it jogged my memory for a solution that hasn't been mentioned.

The nvcc help offers:

--x {c|c++|cu}                             (-x)
        Explicitly specify the language for the input files, rather than letting
        the compiler choose a default based on the file name suffix.
        Allowed values for this option:  'c','c++','cu'.

So although it's a bit of a blunt tool, you can do:

nvcc my_source.cpp -x cu ...

and it'll compile the .cpp as if it was named .cu (ie as CUDA).

Edd Inglis
  • 1,067
  • 10
  • 22