Generating single .so from multiple C++ and C object files

本秂侑毒 提交于 2019-12-25 00:22:15

问题


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:


  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.


来源:https://stackoverflow.com/questions/16912185/generating-single-so-from-multiple-c-and-c-object-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!