C++ and CUDA using CUDA with C++

Hi ,
I am new to CUDA. I tried to compile my C++ with .cu extension, as I had defined a function in the file to run on the tesla machine.But the classes and their objects are not recognized by the complier.
So,do I have to change the extension of my file from .cu to .cpp and then compile the file.Or there is some other way around the problem.

What do you mean “the classes and their objects are not recognized by the compiler” ?
Does your C++ compiler try to compile a .cu file that has CUDA extensions in it? Or does your development suite not recognize the .cu extension? Or do you get linker errors of the type external symbol not defined?

I am working on linux.I made the Makefile.After typing make to run the code( file with .cu extension).I get errors like

“proj3.cu”, line 271: error: identifier “p4” is undefined

  xsr =mod ((p3.x_axis-p4.x_axis)/Nx);

                       ^

Here my class is points and my objects are p3,p4

If you’re using C++ features from inside CUDA kernels, stop, you can’t do that (except for some simple templates). If you’re using C++ in host code, that’s fine; just add --host-compilation=c++ to your nvcc command.

I’ve just tried this and have problems:

I’ve included and added a

std::cout<<"c++";

into host code.

Then I’ve added --host-compilation=c++ to the compiler’s command line. I’m getting errors like this:

Compiling...

sample.cu

"D:\mvs8\VC\INCLUDE\xlocinfo", line 77: error: support for exception handling

          is disabled

              throw runtime_error("bad locale name");

              ^

What should I do?

My entire command line looks like this:

nvcc.exe -ccbin "D:\mvs8\VC\bin"  -c -D_CONSOLE -Xcompiler "/EHsc /W3 /nologo /Wp64 /O2 /Zi   /MT " -I"D:\CUDA\include" -I"D:\NVIDIA CUDA SDK\common\inc" -o Release\sample.obj sample.cu

I presume VS2005 appends additional commands to the end of this line.

Hi,

thanks for the reply.Yes,I was using C++ features from inside CUDA kernel and thereby committing a big mistake.

For the solution to the problem, do I need to first change the extension from .cu to .cpp.

2)remove the function that need to be implemented on the device from .cpp file to other file with .cu

3)then in the make file add the ccp file

Are the above steps correct to implement the solution given by tmurray or do I need to add something more?

The file extension makes absolutely no difference anywhere. It’s just convention.

Indeed, NVIDIA claims that “Full C++ is supported for the host code”. However, when trying to compile a program consisting of the single line “include”, I get compiler errors like:

xlocale(490): error: excepted an identifier

Should this work, or is it just not true that c++ is supported for host code? I used --host-compilation=c++, which seems to be default for CUDA 2.0.

are you using Visual Studio 2005 with SP1? if not, it won’t work–upgrade to SP1 and it will.

just tried this to be sure you’re wrong:

#include <iostream>

int main (int argc, char** argv)

{

 Â return 0;

}
C:\>nvcc --host-compilation c++ -o iostreamTest iostreamTest.cu

iostreamTest.cu

tmpxft_00001580_00000000-3_iostreamTest.cudafe1.gpu

tmpxft_00001580_00000000-8_iostreamTest.cudafe2.gpu

tmpxft_00001580_00000000-3_iostreamTest.cudafe1.cpp

tmpxft_00001580_00000000-12_iostreamTest.ii

C:\>

and if I just do include , it returns the error I’d expect:

C:\>nvcc --host-compilation c++ -o iostreamTest iostreamTest.cu

iostreamTest.cu

tmpxft_00000adc_00000000-3_iostreamTest.cudafe1.gpu

tmpxft_00000adc_00000000-8_iostreamTest.cudafe2.gpu

tmpxft_00000adc_00000000-3_iostreamTest.cudafe1.cpp

tmpxft_00000adc_00000000-12_iostreamTest.ii

LINK : fatal error LNK1561: entry point must be defined

I am using Visual Studio 2005 Express Ediiton with SP1.

Compilation with cl gives the following output:

C:\>cl -c simplecpp.cpp /EHsc

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86

Copyright (C) Microsoft Corporation.  All rights reserved.

simplecpp.cpp

Compilation with nvcc fails with the following output:

C:\Documents and Settings\sunnegardh>nvcc --host-compilation c++ -c simplecpp.cu

simplecpp.cu

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\crtdbg.h(1142): warning: o

mission of exception specification is incompatible with previous function "opera

tor delete[](void *)"

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\new(80): here

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\xutility(2364): error: name followed by "::" must be a class or namespace name

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\xutility(2438): error: name followed by "::" must be a class or namespace name

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\xutility(2662): error: name followed by "::" must be a class or namespace name

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\xutility(2662): error: expected an identifier

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\xutility(2663): error: name followed by "::" must be a class or namespace name

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\xutility(2664): error: identifier "_Result" is undefined

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\xutility(2749): error: name followed by "::" must be a class or namespace name

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\xutility(2749): error: expected an identifier

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\xutility(2750): error: name followed by "::" must be a class or namespace name

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\xutility(2751): error: identifier "_Result" is undefined

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\xutility(2857): error: name followed by "::" must be a class or namespace name

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\xutility(2919): error: name followed by "::" must be a class or namespace name

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\xutility(3065): error: name followed by "::" must be a class or namespace name

C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\xlocale(490): error: expected an identifier

14 errors detected in the compilation of "tmpxft_000006d8_00000000-6_simplecpp.cpp1.ii".

Should it work also with the express edition?

Tried this also with Visual Studio 2005 Professional, SP 1, getting exactly the same error messages. After installing SP1, the number of error messages has just grown (before it was only complaining on line 490 in xlocale).

OK, found a solution to my problem. Seems like nvcc on my Windows system requires the case sensitive path for VS to be entered for advanced c++ features to work.

In other words,

nvcc -ccbin “C:\Program Files\Microsoft Visual Studio 8\VC\bin” -c simplecpp.cu

works, but

nvcc -ccbin “c:\program files\microsoft visual studio 8\vc\bin” -c simplecpp.cu

fails. Does anyone have a clue how this might happen?