1

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:

  • System version: Ubuntu 20.10 (x64)
  • Compiler version: g++ (Ubuntu 10.2.0-13ubuntu1) 10.2.0
  • Boost version: 1.75.0
  • Executable compiled with: g++ main.cpp -o boost-plugin -ldl -lboost_filesystem
  • SO library compiled with: 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
Lehu
  • 761
  • 4
  • 14

2 Answers2

1

Edit: I didn't reproduce your literal message.

To help you rule out any inconsistencies, I provided a step-for-step example repo, see below

Did you forget to add the library directory as the command line argument? I think the sample should have done some argument checking, like at least an assert:

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

So, in my test, I just specify the current directory where the build output for the main application (sotest) and the library (libmy_plugin_sum.so) reside:

./sotest .

Or

./sotest "$PWD"

I've tested that it works both with the actual library name

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

and the "linux convention for shard library naming":

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

UPDATE: Live Interactive Example

I decided to create an easy-to-reproduce sample: https://github.com/sehe/boost-dll-example

It contains

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

The cpp/hpp are yours, the CMakeLists.txt is

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)

And there are some VsCode settings:

./.devcontainer/devcontainer.json
./.devcontainer/Dockerfile
./.vscode/extensions.json

So that when you open the project in VsCode it's all automatic and will build the example in a container:

enter image description here

  1. Clone the sample and open in vscode, e.g. with

    clone https://github.com/sehe/boost-dll-example
    code boost-dll-example
    
  2. Install recommended extensions

  3. Click to re-open in container (this may take a few minutes)

  4. Locate CMake in the side-bar and click to "Build All Projects"

  5. run sotest in the build folder, as above

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Had to reduce the GIF quality. [Original mp4](http://stackoverflow-sehe.s3.amazonaws.com/a49d6eaa-478c-4168-9f5a-039080d1c1d2/out.mp4) in case it is easier to use/read. – sehe Mar 03 '21 at 17:08
  • 1
    Thanks, I will try it to double check. However, I'm properly invoking program. If I don't give command line parameter, program fails at `filesystem::path` construction, not at `dll:import` . Additionally, if I did not specify correct path, it wouldn't work with the lines `extern "C" BOOST_SYMBOL_EXPORT my_plugin_sum plugin; my_plugin_sum plugin;` removed as it wouldn't be able to find library. – Lehu Mar 03 '21 at 17:21
  • One more thing: I am using cmake and vscode :) . I removed it as it is another layer that could cause issues. I wanted to reproduce error with setup as simple as possible for easier debugging. – Lehu Mar 03 '21 at 17:23
  • Oh, yeah. I don't use VsCode actually. Just thought it would be a nice way to package project + devcontainer. VsCode has it's sweet spot there – sehe Mar 03 '21 at 17:38
  • Ahh, I get it. Nevertheless: I think it's some problem with my setup/environment. I don't think boost shows not working code in their own tutorial. I'd like to understand why it's looking for mangled symbol (`_ZTI13my_plugin_api`) even if I'm not trying to import anything connected with it and why there is no such symbol in library... – Lehu Mar 03 '21 at 18:03
  • I'm interested in finding that, but it's objectively impossible without the code/build files. – sehe Mar 03 '21 at 22:00
  • I did found the source of the problem :) . I my virtual base I declared virtual destructor but did not define it: `virtual ~my_plugin_api();` instead of `virtual ~my_plugin_api() {}` . – Lehu Mar 06 '21 at 16:05
0

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!!!
Lehu
  • 761
  • 4
  • 14