4

If I want to dynamically link a shared library (.so) for a C++ application (which was built with g++) using LD_PRELOAD, does it matter if the .so is generated from a C source file (using gcc) or a C++ source file (using g++)? And why or why not?

Thanks for helping me understand this.

Naly Gneh
  • 63
  • 3

2 Answers2

5

Yes, a C++ executable can be linked (both statically and dynamically) to a C library.

This is completely deliberate. The C++ ABIs are designed to be backwards compatible.

You will have to ensure that the declarations of functions and so on of the library symbols, as written in your C++ program, are marked extern "C" to denote that you are crossing a language boundary. Typically, the library's own shipped header files will do this for you.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

It doesn't matter how an .so was generated. You should be able preload an .so that was generated from FORTRAN code too. The key issue is making sure that you use the symbols in the .so correctly.

When an .so is generated from C code, the names of the symbols are unmangled.

When an .so is generated from C++ code, the names of the symbols are mangled.

I am not sure whether the names of symbols in an .so generated from FORTRAN are mangled.

R Sahu
  • 204,454
  • 14
  • 159
  • 270