# CUDA with C++

**URL:** <https://forums.developer.nvidia.com/t/cuda-with-c/9765>\
**Category:** CUDA Programming and Performance\
**Created:** [May 28, 2009, 2:56pm UTC](https://forums.developer.nvidia.com/t/cuda-with-c/9765 "2009-05-28T14:56:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![caggles](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@caggles](https://forums.developer.nvidia.com/u/caggles)\
**Post date:** [May 28, 2009, 2:56pm UTC](https://forums.developer.nvidia.com/t/cuda-with-c/9765/1 "2009-05-28T14:56:50Z")

</div>

Hey all

I’m pretty sure I read somewhere that I can use CUDA with C++, but all the examples are in C, so could someone explain to me how I would take the example code and change it into a C++ program? Thanks :)

---

<div class="post-metadata">

**Author:** ![l-BigBoss](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@l-BigBoss](https://forums.developer.nvidia.com/u/l-BigBoss)\
**Post date:** [May 28, 2009, 3:07pm UTC](https://forums.developer.nvidia.com/t/cuda-with-c/9765/2 "2009-05-28T15:07:10Z")

</div>

C++ can be used in host code. Kernel must be in C.

---

<div class="post-metadata">

**Author:** ![Jamie\_K](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Jamie\_K](https://forums.developer.nvidia.com/u/Jamie_K)\
**Post date:** [May 28, 2009, 4:45pm UTC](https://forums.developer.nvidia.com/t/cuda-with-c/9765/3 "2009-05-28T16:45:52Z")

</div>

> [@](#):
>
> C++ can be used in host code. Kernel must be in C.

A few C++ features can be used in kernel code, but it must be predominantly C “style”. For example, you can’t use new or delete, or virtual functions, because memory allocation and function pointers are not available. But some kinds of templates are possible.

According to the documentation:

> [@](#):
>
> The front end of the compiler processes CUDA source files according to C++ syntax rules. Full C++ is supported for the host code. However, only the C subset of C++ is fully supported for the device code; C++ specific features such as classes, inheritance, or declaration of variables within basic blocks are not. As a consequence of the use of C++ syntax rules, void pointers (e.g. returned by malloc()) cannot be assigned to non-void pointers without a typecast.

---

<div class="post-metadata">

**Author:** ![jph4599](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@jph4599](https://forums.developer.nvidia.com/u/jph4599)\
**Post date:** [May 28, 2009, 5:00pm UTC](https://forums.developer.nvidia.com/t/cuda-with-c/9765/4 "2009-05-28T17:00:39Z")

</div>

If you would prefer to program in C++, you could try checking out some wrappers such as Thrust (formerly Komrade)

> [@](#):
>
> Thrust is a CUDA library of parallel algorithms with an interface resembling the C++ Standard Template Library (STL). Thrust provides a flexible high-level interface for GPU programming that greatly enhances developer productivity. Develop high-performance applications rapidly with Thrust!

Not sure if it has all of the capabilities you need, but it may be worth checking out if you really want to avoid C.

---

<div class="post-metadata">

**Author:** ![caggles](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@caggles](https://forums.developer.nvidia.com/u/caggles)\
**Post date:** [May 28, 2009, 6:31pm UTC](https://forums.developer.nvidia.com/t/cuda-with-c/9765/5 "2009-05-28T18:31:25Z")

</div>

I just need to use C++ for my front-end program. I can write the kernel in C, that’s fine. It’s just because I need to use another tool in conjunction with CUDA that requires C++. So I just call the kernel header written in C into my C++ program?

---

<div class="post-metadata">

**Author:** ![jph4599](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@jph4599](https://forums.developer.nvidia.com/u/jph4599)\
**Post date:** [May 28, 2009, 6:43pm UTC](https://forums.developer.nvidia.com/t/cuda-with-c/9765/6 "2009-05-28T18:43:08Z")

</div>

Yeah, you just need to mark your C functions extern “C”. Check out the cppIntegration SDK example.
