boost.dll 导入导出c++函数

boost

boost.org

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
    }
}

异常问题

  • 全局函数获取后,调用异常
  1. 添加namespace,boost导出全局c++函数调用有问题
  2. 或者别名声明的时候,原函数名标注全局
BOOST_DLL_ALIAS(::bearing_example_env,bearing_example_env)
  • get_alias、get
    shared_library的get(import_symbol同理)是获取export函数,get_alias获取alias别名函数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

怪力左手

囧rz

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值