0

Let's say I have a C++ library code, with some definitions wrapped with extern "C" { ... }.

I also have a C library code which uses that C++ library.

What I want to do is to create a single .so file, so that only one call to dlopen should be enough to start using this library.

Here's what I do now: I'm first compiling my C++ library to a .so file with -shared -rdynamic -fPIC. Then I'm compiling my C library to a .so file with same parameters. After that, I have to load C++ library with dload before loading the C library. Otherwise loading fails with a undefined symbol error.

What I want to do is to compile this two libraries into one .so file so that only one call to dload should be enough to use it.

How can I do that?

Thanks in advance.

EDIT: Compiling to .o files and then combining doesn't work for me. Here's what I do:

  • I compile each file to object file with -fPIC parameter
  • I link them with clang [list of object files] -shared -rdynamic -fPIC -o libmylib.so
  • When I try to load, I get undefined symbol: __gxx_personality_v0 error.

EDIT2: Ahh, I forgoto to link it against libstdc++, it works now. Thanks.

sinan
  • 6,809
  • 6
  • 38
  • 67
  • 1
    Just stick all the object files as arguments to the linking line? – Mats Petersson Jun 04 '13 at 07:38
  • @MatsPetersson, for some reason, that doesn't work. should I use any additional parameters while compiling to object files? – sinan Jun 04 '13 at 08:40
  • @sinan Are these unresolved symbols? If yes, you need to link them in a specific order. http://stackoverflow.com/q/45135/23643 – Alex B Jun 04 '13 at 09:26

1 Answers1

1
  1. The easiest way is to combine them into a single .so file by either combining all object files, or building two static .a libraries and then linking them into a single shared library.
  2. If you really want to keep two .so files, link the first library to the second, as you would with executable. E.g. if libfoo depends on libbar, compile libfoo with -lbar. You'd have to have the dependent libraries in your default library path or in LD_LIBRARY_PATH environment variable.
Alex B
  • 82,554
  • 44
  • 203
  • 280
  • Thanks for your answer. "You'd have to have the dependent libraries in your default library path or in LD_LIBRARY_PATH environment variable" I don't want that, I want to completely remove all .so except only one of them and still be able to load it. As for your first suggestion, how can I combine all object files? Which parameters should I use to compile to object files? – sinan Jun 04 '13 at 08:39
  • @sinan How do you link two libraries that you already have? Take all files and put them in one command line. – Alex B Jun 04 '13 at 09:24