问题
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.
回答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. - If you really want to keep two
.so
files, link the first library to the second, as you would with executable. E.g. iflibfoo
depends onlibbar
, compilelibfoo
with-lbar
. You'd have to have the dependent libraries in your default library path or inLD_LIBRARY_PATH
environment variable.
来源:https://stackoverflow.com/questions/16912185/generating-single-so-from-multiple-c-and-c-object-files