I have problem running the very first example of Boost::DLL tutorial. At some point I just copied code from the git repo to be sure I didn't do any mistake. Didn't help. Both executable and shared lib compile and link properly. However, I still get the same error when running the program:
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)
The funny thing is I don't even have to try to import plugin to get this error... My question is what is happening and why?
My abstract base (in file 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();
};
My executable code (file 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";
}
Now, the important part, the library code (file 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; }
}
Additional info:
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
g++ -shared -fPIC -fvisibility="hidden" -o libmy_plugin_sum.so plugin.cpp
It is enough to remove the following two lines of library code:
extern "C" BOOST_SYMBOL_EXPORT my_plugin_sum plugin;
my_plugin_sum plugin;
and error disappears.
Part of result of nm -C -D libmy_plugin_sum.so
(run on version that generates error):
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
List of objects of library version that doesn't cause any problems doesn't contain any my_plugin_api
entry at all.
EDIT:
When looking at non-demangled symbols (nm -D libmy_plugin_sum.so
) I get this:
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
Answer is very simple. I was just blind :) .
In the abstract base class I declared destructor as virtual. That's ok. I knew it cannot be pure virtual function. However if it's not pure virtual, it needs to be defined.
So, in my abstract base class my_plugin_api
instead of only declaring:
~my_plugin_api(); //declaration only!!!
I should have written definition:
~my_plugin_api() {} //definition!!!