0

I have a C++ shared library with functions like

extern "C" void myfunc(const std::string &foo, std::map<std::string>&bar)

I have a C++ program which uses dlopen() to LAZY_LOAD load the library and then dlsym() to load myfunc. However, I am confused by the fact that the linker was told to use C style linking with C++ STL style arguments. How does that work? Won't that corrupt the stack?

Paul Grinberg
  • 1,184
  • 14
  • 37
  • AIUI, the only effect this has -- at least on Linux -- is to prevent name-mangling by the compiler/linker. This allows the symbol to be found by the linker even if the library and program are compiled with a different compiler, though in that case I'd be a bit worried that their `std::string` and `std::map` implementations are incompatible. tl;dr the arguments are likely passed just fine, but the two things might disagree about what a string/map looks like. – cdhowie Jul 27 '20 at 22:27
  • @cdhowie - in my case, both the shared library and the executable are compiled with the same compiler. It is on Linux. – Paul Grinberg Jul 27 '20 at 22:36
  • In that case, the name mangling should be compatible and you don't need to declare the function with C linkage. – cdhowie Jul 27 '20 at 23:11
  • @cdhowie That would require passing the mangled name to `dlsym`, no? – Paul Sanders Jul 28 '20 at 00:04
  • @PaulSanders That is true. Of course, the mangled name can be determined rather easily. ([Example with g++](https://stackoverflow.com/q/12400105/501250)) – cdhowie Jul 28 '20 at 01:25

0 Answers0