继承和 OpenCL 的问题

Issues with Inheritance and OpenCL

我正在尝试将一些代码从 Windows 移植到 linux。我已经走得很远了,但现在我陷入了继承错误。但我无法弄清楚什么不起作用。它似乎没有导入 header,但我不明白为什么,因为在我看来这应该有效。

这是错误输出:

/usr/bin/c++   -DHAVE_CLOGS -DQT_CORE_LIB -DQT_GUI_LIB -DQT_NO_DEBUG -DQT_WIDGETS_LIB -std=c++11 -I/code/cuda/JF-Cut/src/build -I/code/cuda/JF-Cut/src/QVisualizer -I/code/cuda/clogs-install/include -I/usr/local/cuda-7.5/targets/x86_64-linux/include -isystem /opt/Qt/5.5/gcc_64/include -isystem /opt/Qt/5.5/gcc_64/include/QtWidgets -isystem /opt/Qt/5.5/gcc_64/include/QtGui -isystem /opt/Qt/5.5/gcc_64/include/QtCore -isystem /opt/Qt/5.5/gcc_64/./mkspecs/linux-g++    -fPIC -o CMakeFiles/QGCWidget.dir/Graph_Cut/QGCWidget.cpp.o -c /code/cuda/JF-Cut/src/QVisualizer/Graph_Cut/QGCWidget.cpp
In file included from /code/cuda/JF-Cut/src/QVisualizer/Graph_Cut/QGCWidget.cpp:44:0:
/code/cuda/JF-Cut/src/QVisualizer/Graph_Cut/../infrastructures/QError.h:45:11: error: ‘cl::Error’ has not been declared
 using cl::Error;
           ^
/code/cuda/JF-Cut/src/QVisualizer/Graph_Cut/../infrastructures/QError.h:48:1: error: expected class-name before ‘{’ token
 {
 ^
/code/cuda/JF-Cut/src/QVisualizer/Graph_Cut/../infrastructures/QError.h: In member function ‘void QError::serialize(std::ostringstream&, cl_int)’:
/home/sansomk/code/cuda/JF-Cut/src/QVisualizer/Graph_Cut/../infrastructures/QError.h:68:13: error: ‘cl::Error’ has not been declared
         cl::Error::serialize(s, code);

这是 QError.h

的代码
#ifndef QERROR_H
#define QERROR_H

#ifndef __CL_ENABLE_EXCEPTIONS
#define __CL_ENABLE_EXCEPTIONS
#endif
// removed #include "../3rdParty/cl/cl_stacktrace.hpp"
#if defined(__APPLE__) || defined(__MACOSX)
#include <OpenCL/cl.hpp>
#else
#include <CL/cl.hpp>
#endif

#define Q_LOGIC_ERROR       -100
#define Q_INVALID_ARGUMENT  -101
#define Q_LENGTH_ERROR      -102
#define Q_OUT_OF_RANGE      -103
#define Q_FUTURE_ERROR      -104
#define Q_RUNTIME_ERROR     -110
#define Q_RANGE_ERROR       -111
#define Q_OVERFLOW_ERROR    -112
#define Q_UNDERFLOW_ERROR   -113
#define Q_SYSTEM_ERROR      -114

using cl::Error;

class QError : public cl::Error
{
protected:
    cl_int level_;
    void serialize(std::ostringstream& s, cl_int code)
    {
        std::string error;
        switch (code)
        {
        case Q_LOGIC_ERROR: error = "Q_LOGIC_ERROR"; break;
        case Q_INVALID_ARGUMENT: error = "Q_INVALID_ARGUMENT"; break;
        case Q_LENGTH_ERROR: error = "Q_LENGTH_ERROR"; break;
        case Q_OUT_OF_RANGE: error = "Q_OUT_OF_RANGE"; break;
        case Q_FUTURE_ERROR: error = "Q_FUTURE_ERROR"; break;
        case Q_RUNTIME_ERROR: error = "Q_RUNTIME_ERROR"; break;
        case Q_RANGE_ERROR: error = "Q_RANGE_ERROR"; break;
        case Q_OVERFLOW_ERROR: error = "Q_OVERFLOW_ERROR"; break;
        case Q_UNDERFLOW_ERROR: error = "Q_UNDERFLOW_ERROR"; break;
        case Q_SYSTEM_ERROR: error = "Q_SYSTEM_ERROR"; break;
        }
        if (!error.empty()) s << " > " << error << ", ";
        cl::Error::serialize(s, code);
    }
public:
    QError(cl_int level, cl_int err, const char * errStr = NULL) : level_(level), cl::Error(err, errStr) {}

    ~QError() throw() {}

    cl_int level(void) const { return level_; }

    virtual const char * what() throw ()
    {
        std::ostringstream s;
        serialize(s, err_);
        errStr_ = s.str();
        return errStr_.c_str();
    }
};

#endif  // QERROR_H

为了使用 cl::Error 你需要定义 __CL_ENABLE_EXCEPTIONS.

我看到你有它,但那个文件是 header 文件。如果您在编译单元 (.cpp) 中较早的其他地方包含 OpenCL headers 而未定义 __CL_ENABLE_EXCEPTIONS。然后后面的包含将简单地跳过(由于 header 文件中的 ifdefs 以避免同一 .h 文件的多个实例)。

对于这些类型的全局编译定义,您应该做的是在命令行中声明它们。

 g++ ... -D__CL_ENABLE_EXCEPTIONS

这样可以确保在编译的一开始就启用定义。