# C++ and CUDA using CUDA with C++

**URL:** <https://forums.developer.nvidia.com/t/c-and-cuda-using-cuda-with-c/4820>\
**Category:** CUDA Programming and Performance\
**Created:** [July 30, 2008, 1:37pm UTC](https://forums.developer.nvidia.com/t/c-and-cuda-using-cuda-with-c/4820 "2008-07-30T13:37:37Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![prateekswapnil](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@prateekswapnil](https://forums.developer.nvidia.com/u/prateekswapnil)\
**Post date:** [July 30, 2008, 1:37pm UTC](https://forums.developer.nvidia.com/t/c-and-cuda-using-cuda-with-c/4820/1 "2008-07-30T13:37:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jmoritz](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@jmoritz](https://forums.developer.nvidia.com/u/jmoritz)\
**Post date:** [July 30, 2008, 4:17pm UTC](https://forums.developer.nvidia.com/t/c-and-cuda-using-cuda-with-c/4820/2 "2008-07-30T16:17:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![prateekswapnil](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@prateekswapnil](https://forums.developer.nvidia.com/u/prateekswapnil)\
**Post date:** [July 30, 2008, 4:48pm UTC](https://forums.developer.nvidia.com/t/c-and-cuda-using-cuda-with-c/4820/3 "2008-07-30T16:48:20Z")

</div>

> [@](#):
>
> 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?
> 
> [snapback]418226[/snapback]

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

---

<div class="post-metadata">

**Author:** ![tmurray](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@tmurray](https://forums.developer.nvidia.com/u/tmurray)\
**Post date:** [July 30, 2008, 6:35pm UTC](https://forums.developer.nvidia.com/t/c-and-cuda-using-cuda-with-c/4820/4 "2008-07-30T18:35:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![\_Big\_Mac](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@\_Big\_Mac](https://forums.developer.nvidia.com/u/_Big_Mac)\
**Post date:** [July 31, 2008, 1:55pm UTC](https://forums.developer.nvidia.com/t/c-and-cuda-using-cuda-with-c/4820/5 "2008-07-31T13:55:58Z")

</div>

I’ve just tried this and have problems:

I’ve included and added a

```auto
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:

```auto
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![prateekswapnil](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@prateekswapnil](https://forums.developer.nvidia.com/u/prateekswapnil)\
**Post date:** [July 31, 2008, 2:48pm UTC](https://forums.developer.nvidia.com/t/c-and-cuda-using-cuda-with-c/4820/6 "2008-07-31T14:48:51Z")

</div>

> [@](#):
>
> 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.
> 
> [snapback]418310[/snapback]

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?

---

<div class="post-metadata">

**Author:** ![Tigga](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Tigga](https://forums.developer.nvidia.com/u/Tigga)\
**Post date:** [July 31, 2008, 4:16pm UTC](https://forums.developer.nvidia.com/t/c-and-cuda-using-cuda-with-c/4820/7 "2008-07-31T16:16:18Z")

</div>

> [@](#):
>
> 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?
> 
> [snapback]418794[/snapback]

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

---

<div class="post-metadata">

**Author:** ![sunne](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@sunne](https://forums.developer.nvidia.com/u/sunne)\
**Post date:** [August 27, 2008, 6:31am UTC](https://forums.developer.nvidia.com/t/c-and-cuda-using-cuda-with-c/4820/8 "2008-08-27T06:31:56Z")

</div>

> [@](#):
>
> 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.
> 
> [snapback]418310[/snapback]

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.

---

<div class="post-metadata">

**Author:** ![tmurray](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@tmurray](https://forums.developer.nvidia.com/u/tmurray)\
**Post date:** [August 27, 2008, 7:30am UTC](https://forums.developer.nvidia.com/t/c-and-cuda-using-cuda-with-c/4820/9 "2008-08-27T07:30:30Z")

</div>

> [@](#):
>
> 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.
> 
> [snapback]431667[/snapback]

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:

```auto
#include <iostream>

int main (int argc, char** argv)

{

 Â return 0;

}

```

```auto
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:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![sunne](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@sunne](https://forums.developer.nvidia.com/u/sunne)\
**Post date:** [August 27, 2008, 9:05am UTC](https://forums.developer.nvidia.com/t/c-and-cuda-using-cuda-with-c/4820/10 "2008-08-27T09:05:17Z")

</div>

> [@](#):
>
> are you using Visual Studio 2005 with SP1? if not, it won’t work–upgrade to SP1 and it will.
> 
> [snapback]431681[/snapback]

I am using Visual Studio 2005 Express Ediiton with SP1.

Compilation with cl gives the following output:

```auto
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:

```auto
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?

---

<div class="post-metadata">

**Author:** ![sunne](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@sunne](https://forums.developer.nvidia.com/u/sunne)\
**Post date:** [August 27, 2008, 9:39am UTC](https://forums.developer.nvidia.com/t/c-and-cuda-using-cuda-with-c/4820/11 "2008-08-27T09:39:42Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![sunne](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@sunne](https://forums.developer.nvidia.com/u/sunne)\
**Post date:** [August 27, 2008, 11:21am UTC](https://forums.developer.nvidia.com/t/c-and-cuda-using-cuda-with-c/4820/12 "2008-08-27T11:21:14Z")

</div>

> [@](#):
>
> 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).
> 
> [snapback]431712[/snapback]

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?
