我正在尝试使用 -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 header 是最直接的方法。但您也可以以非侵入方式执行此操作,只要 Boost header 不覆盖可见性¹
因此您可以简单地向前声明与 boost/exception/error_info.hpp
中相同的类型:
#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");
}
¹ 它没有
关于c++ - 更改 boost::error_info 可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46596933/