使用CUDA和C ++ 11时出错

时间:2012-01-29 21:52:45

标签: c++ gcc cuda c++11

我正在使用CUDA 4.1和GCC 4.5 ......(最终!CUDA支持GCC 4.5,但仍在等待GCC 4.6)。无论如何,是否可以将C ++ 11与CUDA 4.1一起使用?

我试过传递:

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

到nvcc,它给我带来了一堆错误:

/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".

示例test.cu

#include <cuda.h>

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

编译好:

nvcc -c -o test.o test.cu

但不是C ++ 0x

nvcc -c -o test.o test.cu --compiler-options "-std=c++0x"

2 个答案:

答案 0 :(得分:26)

不,从这个答案来看,nvcc不支持c ++ 11结构,即使它们受到主机编译器的支持。

它不像将-std=c++0x传递给主机编译器那么简单的原因是nvcc必须解析整个代码才能将其拆分为__host____device__个一半。这个预处理在主机编译器被提供任何代码之前发生,因此nvcc的解析器需要能够使用c ++ 11来使它工作。

答案 1 :(得分:11)

根据他在Thrust's Google Group上发布的另一条消息更新了@Jared Hoberock的答案:CUDA 6.5似乎已经添加了C ++ 11支持(虽然它仍然是实验性的和未记录的)。

虚拟示例: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;
}

编译&amp;执行

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

如果没有-std=c++11,我会收到以下(预期)错误:

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

注意:此示例可能无法使用GCC 5.1进行编译。

更新

CUDA 7.0 officially引入了C ++ 11支持:

  

CUDA 7为cvA C ++编译器nvcc增加了C ++ 11特性支持。这意味着您不仅可以在使用nvcc编译的主机代码中使用C ++ 11功能,还可以在设备代码中使用C ++ 11功能。新的C ++语言功能包括auto,lambda函数,可变参数模板,static_assert,rvalue引用,基于范围的for循环等。要启用C ++ 11支持,请将标志--std = c ++ 11传递给nvcc(Microsoft Visual Studio不需要此选项)。