apolukhin / Boost.DLL

Library for comfortable work with DLL and DSO
https://boost.org/libs/dll
109 stars 69 forks source link

symbol_location example mainly reports: "./a.out" #65

Open pkeir opened 1 year ago

pkeir commented 1 year ago

I've created a program to test boost::dll::symbol_location, based on the documentation page here. If I compile the code ($CXX -std=c++17 main.cpp) on my Ubuntu 22.04 system with Clang (clang++), the output is as expected (a.out/libstdc++.so/libc.so etc.); but using GCC (g++), the program outputs that all 5 symbols are located at a.out. On another machine (Debian Crostini), -dl is required; and both GCC and Clang indicate that all 5 symbols are located at a.out. How can I get consistent results?

#define BOOST_DLL_USE_STD_FS
#include <boost/dll/config.hpp>
#include <boost/dll/runtime_symbol_info.hpp>
#include <iostream>
#include <cassert>
#include <functional> // std::placeholders

int var;
void foo() {}

int main(int argc, char *argv[])
{
  namespace dll = boost::dll;
  boost::dll::fs::path p1, p2, p3, p4, p5;
  boost::dll::fs::error_code ec{};
  p1 = dll::symbol_location(var, ec);       // returns program location
  assert(!ec);
  p2 = dll::symbol_location(foo, ec);       // returns program location
  assert(!ec);

  // returns location of libstdc++: "/usr/lib/x86_64-linux-gnu/libstdc++.so.6"
  p3 = dll::symbol_location(std::cerr, ec);
  assert(!ec);

  // returns location of libstdc++: "/usr/lib/x86_64-linux-gnu/libstdc++.so.6"
  p4 = dll::symbol_location(std::placeholders::_1, ec);
  assert(!ec);

  // returns location of libc: "/lib/x86_64-linux-gnu/libc.so.6"
  p5 = dll::symbol_location(std::puts, ec);
  assert(!ec);

  for (const auto& p : {p1, p2, p3, p4, p5}) { std::cout << p << std::endl; }

  return 0;
}