0

我的项目中的所有异常都有一个基类。它是这样实现的:

异常.hpp

class Exception : public std::exception
{
public:

    ELS_EXPORT_SYMBOL Exception(void) throw();
    ELS_EXPORT_SYMBOL explicit Exception(const std::string& what) throw();
    ELS_EXPORT_SYMBOL Exception(const char* format, ...) throw()
            ELS_PRINTF_FUNC(2, 3);
    ELS_EXPORT_SYMBOL Exception(const Exception& other) throw();
    ELS_EXPORT_SYMBOL Exception& operator =(const Exception& other)
            throw();
    ELS_EXPORT_SYMBOL virtual ~Exception(void) throw();

    ELS_EXPORT_SYMBOL virtual const char* what(void) const throw();

protected:

    ELS_EXPORT_SYMBOL void _M_setWhat(const char* format, ::va_list va)
        throw();

private:

    std::string _M_what;
};

#define ELS_EXC_VA_SET_WHAT(FORMAT)                                         \
    do                                                                      \
    {                                                                       \
        ::va_list va;                                                       \
        ::va_start(va, FORMAT);                                             \
        this->_M_setWhat(FORMAT, va);                                       \
        ::va_end(va);                                                       \
    }                                                                       \
    while (false)

异常.cpp

Exception::Exception(void) throw()
    : std::exception(),
      _M_what("Exception")
{

}

Exception::Exception(const std::string& what) throw()
    : std::exception(),
      _M_what(what.empty() ? "Exception" : what)
{

}

Exception::Exception(const char* format, ...) throw()
    : std::exception(),
      _M_what()
{
    ELS_EXC_VA_SET_WHAT(format);
}

Exception::Exception(const Exception& other) throw()
    : std::exception(other),
      _M_what(other._M_what)
{

}

Exception& Exception::operator =(const Exception& other) throw()
{
    std::exception::operator =(other);
    this->_M_what = other._M_what;
    return *this;
}

Exception::~Exception(void) throw()
{

}

const char* Exception::what(void) const throw()
{
    return this->_M_what.c_str();
}

void Exception::_M_setWhat(const char* format, ::va_list va) throw()
{
    static const size_t BUFSIZE = 512;

    char buf[BUFSIZE];

    ::memset(buf, 0, BUFSIZE);
    ::vsnprintf(buf, BUFSIZE, format, va);
    this->_M_what = std::string(buf);
}

这些文件是使用以下选项编译的共享库的一部分:

-Wall -fPIC -O2 -D_GNU_SOURCE -fvisibility=hidden -rdynamic -Wl,-E

库本身编译没有任何问题,但是当我尝试编译二进制文件并将其链接到它时,我收到以下错误:

Main.cpp:(.text+0x297): undefined reference to `typeinfo for els::except::Exception'

我用谷歌搜索了一下,常见的答案是要么实现所有虚拟方法,要么使它们成为纯方法。我在这里看不到这个问题。我究竟做错了什么?

4

3 回答 3

1

好的,我明白了 - 我必须让整个班级可见 - 类 ELS_EXPORT_SYMBOL 异常。

于 2013-01-31T21:05:25.813 回答
0

当 .so 文件与 C++ 标准库静态链接时,使用 RTTI 在 Linux 上会出现问题。在一个 .so 中实例化的类在另一个 .so 中不可用于自省,因为它们具有不同的 RTTI 表(因为每个 .so 都有自己的 C++ 运行时副本,由于静态链接)。

您有 2 个选项: 1. 在所有程序部分使用 C++ 运行时的动态链接(我使用此选项) 2. 像您已经想通的那样显式公开类(当您有很多类时会变得烦人)

于 2013-01-31T22:56:12.193 回答
0

库本身编译没有任何问题,但是当我尝试编译二进制文件并将其链接到它时,我收到以下错误

什么是链接命令行?

您可能认为您过早地传入库,因此链接器在需要该定义之前已经对其进行了处理。

于 2013-01-31T20:53:22.337 回答