c++ - gcc7 的 NVCC 错误
问题描述
我有以下代码,只有 cuda9 + gcc7 有错误。Cuda9 + gcc6 没有编译错误。
这是我为该错误编写的最小复制器。我怀疑这是编译器错误,但我必须修复我的代码才能使用 gcc7。我想知道摆脱编译错误的解决方法。
Cuda 编译工具,9.2 版,V9.2.148
gcc 版本 7.3.0 (Ubuntu 7.3.0-21ubuntu1~16.04)
错误:
$ nvcc test.cu
test.h: In constructor 'TestOp::TestOp()':
test.h:6:54: error: 'Dummy' is not a member of 'TestOp'
//op.h
class OperatorBase {
public:
template <typename T>
inline bool Dummy(T default_value) {
return true;
}
};
template <class Context>
class Operator : public OperatorBase {
};
//test.cu
#include "test.h"
//test.h
#include "op.h"
template <class Context>
class TestOp : public Operator<Context> {
public:
TestOp()
: msg_(
OperatorBase::Dummy<bool>(true)) {}
private:
bool msg_;
};
解决方案
CUDA 9.2 nvcc C++ 前端正在对您的代码执行此操作:
class OperatorBase {
public:
template< class T> bool
Dummy(T default_value) {
return true;
}
};
template< class Context>
class Operator : public OperatorBase {
};
template< class Context>
class TestOp : public Operator< Context> {
public:
TestOp()
: msg_(
this->OperatorBase::template Dummy< bool> (true)) { }
private:
bool msg_;
};
似乎 g++-7(并且只有 g++-7 或更高版本)在编译该代码时出现名称查找失败。我对 C++ 了解得不够多,无法说出它为什么会失败以及它是否真的应该失败。我可以说这不是 CUDA 前端的新行为——我测试过的每个版本的 CUDA 9 和 CUDA 8 都会发出相同的代码。
您可以通过以不同的方式实现名称解析来避免这种情况:
template <class Context>
class TestOp : public Operator<Context> {
public:
TestOp()
: msg_(
//OperatorBase::Dummy<bool>(true)) {}
this->template Dummy<bool>(true)) {}
private:
bool msg_;
};
虽然这有点老派,但它可以与 CUDA 9.2 和 gcc-4.8、gcc-5.4 和 gcc-7 一起编译。如果过于冒犯您的敏感性,您可能可以尝试基于using
别名的第三种解决方案。this->
推荐阅读
- sql - 如何使用新列作为标志来显示空行?
- java - Java 信号量的确定性如何保证?
- java - 使用静态变量查找数组中的元素数
- python-3.x - 系统找不到指定的路径:Django 管理页面中更改字段时返回错误
- php - 编辑表格后保留图像。-Cakephp 3
- python - 如何将 base64 字符串中的图像作为参数传递给 Python 脚本?
- android - Android ffmpeg 命令未执行
- html - TinyMCE 从 iframe 在正文中添加 div
- asp.net - 向数据库添加新对象的问题
- python - 类型错误:get_file() 缺少 1 个必需的位置参数:'origin'