boost
export
- BOOST_SYMBOL_EXPORT、BOOST_SYMBOL_VISIBLE
- BOOST_DLL_ALIAS,重命名
- BOOST_DLL_ALIAS_SECTIONED,把函数放在同一个section下
BOOST_SYMBOL_EXPORT std::vector<std::string> bearing_example_env(const std::vector<double>& data);
BOOST_DLL_ALIAS(
bearing_example_env,
bearing_example_env
)
BOOST_DLL_ALIAS_SECTIONED(bearing_example_env,bearing_example_env1,test)
三种方式导出的函数符号如下
使用boost.dll 可以导出c++函数而不用加
extern “c”
固定函数名,而且支持函数对象和成员的导入导出
import
- boost::dll::shared_library 基本的函数导入导出,用于导入别名alias
boost::dll::fs::error_code ec;
//append_decorations自动添加dll、so后缀,以及linux的lib前缀,都没有则用原名
boost::dll::shared_library lib("your_dll_name.dll",boost::dll::load_mode::append_decorations,ec);
AddFunction add = lib.get<AddFunction>("add_function_name");
- boost::dll::experimental::smart_libary,用于导入Mangling名称修饰函数,类成员等
namespace space {
class BOOST_SYMBOL_EXPORT my_plugin
{
std::string _name;
public:
std::string name() const;
float calculate(float x, float y);
int calculate(int, x, int y);
static std::size_t size();
my_plugin(const std::string & name);
my_plugin();
~my_plugin_api();
static int value;
};
}
boost::dll::fs::error_code ec;
boost::dll::experimental::smart_library lib("bearing.dll",ec);
auto size_f = lib.get_function<std::size_t()>("space::my_plugin::size"); //get the size function
- 遍历BOOST_DLL_ALIAS_SECTIONED
for (std::size_t i = 0; i < libs_count; ++i) {
// Class `library_info` can extract information from a library
boost::dll::library_info inf(libraries[i]);
//boost导出的函数在boostdll section中查看
std::vector<std::string> sections = inf.sections();
std::vector<std::string> exports = inf.symbols("boostdll");
// Loading library and importing symbols from it
boost::dll::shared_library lib(libraries[i]);
for (std::size_t j = 0; j < exports.size(); ++j) {
std::cout << "\nFunction '" << exports[j] << "' prints:\n\t";
lib.get_alias<void(const std::string&)>(exports[j]) // importing function
(username); // calling function
}
}
异常问题
- 全局函数获取后,调用异常
- 添加namespace,boost导出全局c++函数调用有问题
- 或者别名声明的时候,原函数名标注全局
BOOST_DLL_ALIAS(::bearing_example_env,bearing_example_env)
- get_alias、get
shared_library的get(import_symbol同理)是获取export函数,get_alias获取alias别名函数