我在运行Boost::DLL教程的第一个示例时遇到了问题。在某种程度上,我只是从git复制代码,以确保我没有做任何错误。帮不上忙。可执行和共享库编译和链接正常。但是,在运行程序时仍然会出现相同的错误:
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.01.75.0g++ main.cpp -o boost-plugin -ldl -lboost_filesystem编译的可执行文件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::unique_ptr<boost::system::detail::std_category, 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发布于 2021-03-06 16:13:55
答案很简单。我只是瞎了:)。
在抽象基类中,我将析构函数声明为虚拟的。没关系。我知道它不可能是纯粹的虚拟功能。然而,如果不是纯虚拟的,则需要定义它.
因此,在我的抽象基类my_plugin_api中,而不是仅仅声明:
~my_plugin_api(); //declaration only!!!我应该写一份定义:
~my_plugin_api() {} //definition!!!发布于 2021-03-03 14:52:37
编辑:我没有复制你的文字信息。 为了帮助您排除任何不一致之处,我提供了一个逐步回购示例,请参阅下面。
您忘记添加库目录作为命令行参数吗?我认为样本应该做一些论证检查,比如至少断言:
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"我已经测试过它是否与实际的库名一起工作
lib_path / "libmy_plugin_sum.so", // path to the library and library name以及“用于碎片库命名的linux约定”:
lib_path / "my_plugin_sum", // path to the library and library name更新:动态交互示例
我决定创建一个易于复制的示例:https://github.com/sehe/boost-dll-example。
它包含
CMakeLists.txt
my_plugin_api.hpp
plugin_impl.cpp
test.cppcpp/hpp是你的,CMakeLists.txt是
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设置:
./.devcontainer/devcontainer.json
./.devcontainer/Dockerfile
./.vscode/extensions.json因此,当您在VsCode中打开项目时,它都是自动的,并将在容器中构建该示例:

sotest,如上面所示https://stackoverflow.com/questions/66449467
复制相似问题