【问题标题】:Error while using CUDA and C++11使用 CUDA 和 C++11 时出错
【发布时间】:2012-01-29 21:52:45
【问题描述】:

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

我尝试过:

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

【问题讨论】:

  • 只是编译器选项导致了问题,还是您使用了特定的 c++0x 功能?
  • 我的 test_cuda.cu 没有代码。它只有一个功能,例如 test_cuda() {}
  • 不。没有使用 extern "C" .. 编译 C++ 代码。
  • 您必须在答案中添加一些代码。仅通过查看错误日志很难知道发生了什么问题。
  • 使用 CUDA 6 检查我的解决方案:stackoverflow.com/questions/25941117/c11-standard-with-cuda-6-0

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


【解决方案1】:

不,截至本答案,nvcc 不支持 c++11 构造,即使它们受主机编译器支持。

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

【讨论】:

  • 谢谢。您对拆分 C++0x 和 CUDA 代码的“正确”方式有什么建议吗?
  • 不幸的是,现在您需要将所有 CUDA 隔离到 .cu 文件中,这些文件只使用 nvcc 可以理解的 c++03 结构。其他所有内容都进入主机编译器的 .cpp 文件。
  • @JaredHoberock 在过去两年中没有使用更新的 CUDA 版本进行更改吗?
  • @Farzad,显然不是,因为今天正是同样的错误将我带到了这里。
  • smart ptrs, tr1::unordered_map...我非常怀念这些基本功能...有没有人尝试使用一些旧的 Boost 版本编译 .cu?
【解决方案2】:

根据@Jared Hoberock 在Thrust's Google Group 上发布的另一条消息更新@Jared Hoberock 的答案:似乎 C++11 支持已添加到 CUDA 6.5 中(尽管它仍处于试验阶段且未记录)。

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

编译与执行

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

【讨论】:

  • 就我而言,当我尝试编译它时,我得到nvcc warning : The -c++11 flag is not supported with the configured host compiler. Flag will be ignored.。这发生在 g++ 的 4.6 版中,而不是 4.8 版。我的 Cuda 版本是 7.0
  • @julianromera 你需要一个支持-std=c++11 的宿主编译器,而g++ 4.6 不是这样。不过这个版本的g++是支持-std=c++0x的,不过在nvcc的帮助下可以看Allowed values for this option: 'c++11'.,所以不知道能不能用C++11的特性除非你升级编译器.
猜你喜欢
  • 2016-01-24
  • 2015-11-28
  • 2017-08-31
  • 2021-11-17
  • 1970-01-01
  • 2019-10-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多