24

I am using CUDA 4.1 and GCC 4.5 ... (FINALLY! CUDA supports GCC 4.5, but still waiting for GCC 4.6). Anyways, is it possible to use C++11 with the CUDA 4.1?

I tried passing:

--compiler-options "-std=c++0x"

to nvcc and it throws a bunch of errors at me:

/usr/include/c++/4.5/exception_ptr.h(100): error: copy constructor for class "std::__exception_ptr::exception_ptr" may not have a parameter of type "std::__exception_ptr::exception_ptr"

/usr/include/c++/4.5/exception_ptr.h(100): error: expected a ")"

/usr/include/c++/4.5/exception_ptr.h(110): error: expected a ")"

/usr/include/c++/4.5/exception_ptr.h(132): error: identifier "type_info" is undefined

/usr/include/c++/4.5/exception_ptr.h(101): error: identifier "__o" is undefined

/usr/include/c++/4.5/exception_ptr.h(112): error: expected a ">"

/usr/include/c++/4.5/exception_ptr.h(112): error: identifier "__o" is undefined

/usr/include/c++/4.5/nested_exception.h(62): error: expected a ";"

/usr/include/c++/4.5/nested_exception.h(64): error: expected a ";"

/usr/include/c++/4.5/nested_exception.h(77): error: member function "std::nested_exception::~nested_exception" may not be redeclared outside its class

/usr/include/c++/4.5/nested_exception.h(77): error: function "std::<error>" may not be initialized

/usr/include/c++/4.5/nested_exception.h(77): error: expected an expression

/usr/include/c++/4.5/nested_exception.h(82): error: expected a ")"

/usr/include/c++/4.5/nested_exception.h(110): error: expected a ")"

/usr/include/c++/4.5/nested_exception.h(115): error: expected a ")"

/usr/include/c++/4.5/nested_exception.h(122): error: expected a ")"

/usr/include/c++/4.5/nested_exception.h(127): error: expected a ")"

/usr/include/c++/4.5/nested_exception.h(127): error: function template "std::__throw_with_nested" has already been defined

/usr/include/c++/4.5/bits/cpp_type_traits.h(180): error: identifier "char16_t" is undefined

/usr/include/c++/4.5/bits/cpp_type_traits.h(187): error: identifier "char32_t" is undefined

/usr/include/c++/4.5/bits/cpp_type_traits.h(187): error: class "std::__is_integer<<error-type>>" has already been defined

21 errors detected in the compilation of "/tmp/tmpxft_00000ef2_00000000-4_test_cuda.cpp1.ii".

Example test.cu

#include <cuda.h>

__host__ void test() {
  // nothing in method
}

Compiles fine with:

nvcc -c -o test.o test.cu

But not with C++0x

nvcc -c -o test.o test.cu --compiler-options "-std=c++0x"
user988098
  • 261
  • 1
  • 3
  • 11

2 Answers2

26

No, as of this answer, nvcc does not support c++11 constructs, even if they are supported by the host compiler.

The reason it isn't as simple as passing -std=c++0x to the host compiler is that nvcc has to parse the entirety of the code in order to split it into its __host__and __device__ halves. This preprocess happens before the host compiler is presented with any code at all, so nvcc's parser needs to be able to grok c++11 for it to work.

Jared Hoberock
  • 11,118
  • 3
  • 40
  • 76
  • 1
    thanks. do you have any suggestions on "proper" way to split up C++0x and CUDA code then? – user988098 Jan 30 '12 at 04:10
  • 2
    Unfortunately, for now you need to quarantine everything CUDA into .cu files which only use c++03 constructs which ```nvcc``` can understand. Everything else goes into a .cpp file for your host compiler. – Jared Hoberock Jan 30 '12 at 06:02
  • @JaredHoberock hasn't it been changed in past two years with newer CUDA versions? – Farzad May 21 '14 at 19:15
  • @Farzad, apparently not, since exactly this same error brought me here today. – AatG May 23 '14 at 21:31
  • smart ptrs, tr1::unordered_map... I miss these basic features badly... Has anyone tried to compile .cu with some old Boost releases? – vitrums May 26 '14 at 00:50
11

Update on @Jared Hoberock's answer based on another message he posted on Thrust's Google Group: it seems that C++11 support has been added to CUDA 6.5 (although it is still experimental and undocumented).

Dummy example: test.cu

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

__host__ void test() {
  float a = 12.;
  double b = 3.;
  auto c = a * b;
  std::cout << c << std::endl;
}

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

Compilation & execution

$ nvcc -std=c++11 test.cu -o test
$ ./test
36

Without -std=c++11, I get the following (expected) error:

test.cu(7): error: explicit type is missing ("int" assumed)

Note: this example may fail to compile with GCC 5.1.

Update

CUDA 7.0 officially introduced C++11 support:

CUDA 7 adds C++11 feature support to nvcc, the CUDA C++ compiler. This means that you can use C++11 features not only in your host code compiled with nvcc, but also in device code. New C++ language features include auto, lambda functions, variadic templates, static_assert, rvalue references, range-based for loops, and more. To enable C++11 support, pass the flag --std=c++11 to nvcc (this option is not required for Microsoft Visual Studio).

BenC
  • 8,729
  • 3
  • 49
  • 68
  • In my case, when I try to compile this I get `nvcc warning : The -c++11 flag is not supported with the configured host compiler. Flag will be ignored.`. This happens with version 4.6 of g++, not with version 4.8. My Cuda version is 7.0 – user9869932 Jul 10 '15 at 15:08
  • 1
    @julianromera you need a host compiler that supports `-std=c++11`, and this is not the case of g++ 4.6. However, this version of g++ supports `-std=c++0x`, but in `nvcc`'s help, you can read `Allowed values for this option: 'c++11'.`, so I don't know if you will be able to use C++11 features unless you upgrade your compiler. – BenC Jul 11 '15 at 06:26