首页 > 解决方案 > 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_;
};

标签: c++cudanvcc

解决方案


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


推荐阅读