更改 boost::error_info 可见性

Change boost::error_info visibility

我正在尝试使用 -fvisibility=hidden 进行编译,但无法弄清楚如何更改 boost::error_info typedef 的可见性。有人可以启发我吗?无论我将 DVEM_EXPORT 放在 typedef 行中的什么位置,编译器都会拒绝它,除了编辑 boost header 以添加属性之外,别无他法,可以解决运行时问题。

#ifdef....
#define DVEM_EXPORT __attribute__((visibility("default")))
....
class DVEM_EXPORT UnsupportedDataTypeException : public dv::BaseException<> {};
struct DVEM_EXPORT errinfo_data_type_ {};
typedef boost::error_info<errinfo_data_type_, std::string> errinfo_data_type;

typedef 没有声明类型。

类型声明包含您的默认值 - 隐藏 - 因此该类型实例的任何别名(例如 errinfo_data_type)也被隐藏。

你说得对,修改Boost头是最直接的方法。但是你也可以非侵入性地做到这一点 只要 Boost 头文件不覆盖可见性¹

因此您可以简单地向前声明与 boost/exception/error_info.hpp 中相同的类型:

Live On Coliru

#define DVEM_EXPORT __attribute__((visibility("default")))

namespace boost { 
    template <class Tag,class T> class DVEM_EXPORT error_info; 
}

#include <boost/exception/all.hpp>
#include <stdexcept>

namespace dv {
    template <typename=void>
    struct DVEM_EXPORT BaseException : virtual boost::exception, virtual std::exception {
    };
}

#include <string>
class DVEM_EXPORT UnsupportedDataTypeException : public dv::BaseException<> {};
struct DVEM_EXPORT errinfo_data_type_ {};
typedef boost::error_info<errinfo_data_type_, std::string> errinfo_data_type;

DVEM_EXPORT void foo() {
    throw UnsupportedDataTypeException() << errinfo_data_type("bar");
}

¹ 它没有