未定义符号:Boost::DLL 教程中的 _ZTI13my_plugin_api

如何解决未定义符号:Boost::DLL 教程中的 _ZTI13my_plugin_api

我在运行 Boost::DLL tutorial 的第一个示例时遇到问题。在某些时候,我只是从 git repo 复制代码以确保我没有犯任何错误。没有帮助。可执行和共享库都正确编译和链接。但是,我在运行程序时仍然出现相同的错误:

terminate called after throwing an instance of 'boost::wrapexcept<boost::system::system_error>'
  what():  boost::dll::shared_library::load() failed (dlerror system message: ./libmy_plugin_sum.so: undefined symbol: _ZTI13my_plugin_api): Exec format error
Interrupt (memory dump)

有趣的是我什至不必尝试导入插件来获取此错误... 我的问题是发生了什么以及为什么?

我的抽象基础(在文件 my_plugin_api.hpp 中):

#include <boost/config.hpp>

#include <string>

class BOOST_SYMBOL_VISIBLE my_plugin_api {
public:
    virtual std::string name() const = 0;
    virtual float calculate(float x,float y) = 0;

    virtual ~my_plugin_api();
};

我的可执行代码(文件 main.cpp):

#include <boost/dll/import.hpp> // for import_alias
#include <iostream>
#include "my_plugin_api.hpp"

namespace dll = boost::dll;

boost::dll::fs::path get_path(char* str) {
    return boost::dll::fs::path(str);
}

int main(int argc,char* argv[]) {

    boost::dll::fs::path lib_path = get_path(argv[1]);             // argv[1] contains path to directory with our plugin library
    boost::shared_ptr<my_plugin_api> plugin;            // variable to hold a pointer to plugin variable
    std::cout << "Loading the plugin" << std::endl;
    
    auto multiply = dll::import<double(double,double)>(   //importing single function from library
        lib_path/"my_plugin_sum","multiply",dll::load_mode::append_decorations
    );

    std::cout << multiply(1.5,1.5) << "\n";
}

现在是重要的部分,库代码(文件 plugin.cpp):

#include <iostream>

#include <boost/config.hpp> // for BOOST_SYMBOL_EXPORT
#include <boost/dll/alias.hpp>

#include "my_plugin_api.hpp"

namespace my_namespace {

class my_plugin_sum : public my_plugin_api {
public:
    my_plugin_sum() {
        std::cout << "Constructing my_plugin_sum" << std::endl;
    }

    std::string name() const {
        return "sum";
    }

    float calculate(float x,float y) {
        return x + y;
    }
   
    ~my_plugin_sum() {
        std::cout << "Destructing my_plugin_sum ;o)" << std::endl;
    }
};

extern "C" BOOST_SYMBOL_EXPORT my_plugin_sum plugin;
my_plugin_sum plugin;

} // namespace my_namespace

extern "C" {
    BOOST_SYMBOL_EXPORT
    double multiply(double x,double y) { return x*y; }
}

其他信息

  • 系统版本:Ubuntu 20.10 (x64)
  • 编译器版本:g++ (Ubuntu 10.2.0-13ubuntu1) 10.2.0
  • 增强版:1.75.0
  • 可执行文件编译为:g++ main.cpp -o boost-plugin -ldl -lboost_filesystem
  • SO 库编译为:g++ -shared -fPIC -fvisibility="hidden" -o libmy_plugin_sum.so plugin.cpp

去掉下面两行库代码就够了:

extern "C" BOOST_SYMBOL_EXPORT my_plugin_sum plugin;
my_plugin_sum plugin;

错误消失。

nm -C -D libmy_plugin_sum.so 的部分结果(在产生错误的版本上运行):

0000000000011510 u guard variable for boost::system::detail::to_std_category(boost::system::error_category const&)::map_
000000000000a9dc W my_plugin_api::my_plugin_api()
000000000000a9dc W my_plugin_api::my_plugin_api()
                 U my_plugin_api::~my_plugin_api()
000000000000a87c W boost::system::system_error::~system_error()
.
.
.
00000000000ad12 W std::operator==(std::_Rb_tree_iterator<std::pair<boost::system::error_category const* const,std::unique_ptr<boost::system::detail::std_category,std::default_delete<boost::system::detail::std_category> > > > const&,std::_Rb_tree_iterator<std::pair<boost::system::error_category const* const,std::default_delete<boost::system::detail::std_category> > > > const&)
                 U std::basic_ostream<char,std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char,std::char_traits<char> >&,char const*)
                 U typeinfo for my_plugin_api
0000000000010ce0 V typeinfo for boost::system::system_error
0000000000010d40 V typeinfo for boost::system::error_category

不会引起任何问题的库版本对象列表根本不包含任何 my_plugin_api 条目。

编辑:

在查看未编码的符号 (nm -D libmy_plugin_sum.so) 时,我明白了:

0000000000011510 u _ZGVZN5boost6system6detail15to_std_categoryERKNS0_14error_categoryEE4map_
000000000000a9e2 W _ZN13my_plugin_apiC1Ev
000000000000a9e2 W _ZN13my_plugin_apiC2Ev
                 U _ZN13my_plugin_apiD2Ev
000000000000a882 W _ZN5boost6system12system_errorD0Ev

...

                 U _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@@GLIBCXX_3.4
                 U _ZTI13my_plugin_api
0000000000010ce0 V _ZTIN5boost6system12system_errorE

...

000000000000d2a0 V _ZTSN5boost6system6detail22generic_error_categoryE
                 U _ZTV13my_plugin_api
                 U _ZTVN10__cxxabiv117__class_type_infoE@@CXXABI_1.3

解决方法

编辑:我没有复制你的文字信息。

为了帮助您排除任何不一致之处,我提供了一个分步示例存储库,请参见下文

您是否忘记添加库目录作为命令行参数?我认为样本应该进行一些参数检查,至少像断言:

symbol

因此,在我的测试中,我只指定主应用程序 (int main(int argc,char *argv[]) { assert(argc > 1); boost::dll::fs::path lib_path(argv[1]); // argv[1] contains path to directory with our plugin library ) 和库 (sotest) 的构建输出所在的当前目录:

libmy_plugin_sum.so

./sotest .

我已经测试过它可以与实际的库名称一起使用

./sotest "$PWD"

和“Linux 分片库命名约定”:

lib_path / "libmy_plugin_sum.so",// path to the library and library name

更新:实时互动示例

我决定创建一个易于复制的示例:https://github.com/sehe/boost-dll-example

它包含

lib_path / "my_plugin_sum",// path to the library and library name

cpp/hpp 是你的,CMakeLists.txt 是

CMakeLists.txt
my_plugin_api.hpp
plugin_impl.cpp
test.cpp

还有一些 VsCode 设置:

project(stackoverflow)
cmake_minimum_required(VERSION 3.5)
set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)

FIND_PACKAGE(Boost 1.61.0 REQUIRED COMPONENTS filesystem)
LINK_LIBRARIES(Boost::filesystem dl)

ADD_LIBRARY(my_plugin_sum SHARED plugin_impl.cpp)
ADD_EXECUTABLE(sotest test.cpp)

这样当你在 VsCode 中打开项目时,它都是自动的,并且会在容器中构建示例:

enter image description here

  1. 克隆示例并在 vscode 中打开,例如与

    ./.devcontainer/devcontainer.json
    ./.devcontainer/Dockerfile
    ./.vscode/extensions.json
    
  2. 安装推荐的扩展

  3. 单击以在容器中重新打开(这可能需要几分钟时间)

  4. 在侧边栏中找到 CMake 并单击以“构建所有项目”

  5. 在构建文件夹中运行clone https://github.com/sehe/boost-dll-example code boost-dll-example ,如上

,

答案很简单。我只是瞎了:) .

在抽象基类中,我将析构函数声明为虚拟的。没关系。我知道它不能是纯虚函数。 但是,如果它不是纯虚拟的,则需要对其进行定义。

所以,在我的抽象基类 my_plugin_api 中,而不是仅仅声明:

~my_plugin_api(); //declaration only!!!

我应该有书面定义:

~my_plugin_api() {} //definition!!!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-