如何解决未定义符号: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 中打开项目时,它都是自动的,并且会在容器中构建示例:
-
克隆示例并在 vscode 中打开,例如与
./.devcontainer/devcontainer.json ./.devcontainer/Dockerfile ./.vscode/extensions.json
-
安装推荐的扩展
-
单击以在容器中重新打开(这可能需要几分钟时间)
-
在侧边栏中找到 CMake 并单击以“构建所有项目”
-
在构建文件夹中运行
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 举报,一经查实,本站将立刻删除。