生成多个C ++和C对象文件单一的.so [英] Generating single .so from multiple C++ and C object files

查看:212
本文介绍了生成多个C ++和C对象文件单一的.so的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一个C ++库code,一些定义包裹着的externC{...}

我也有一个C库code它使用C ++库。

我想要做的是建立一个单一的.so文件,以便只有一个调用的dlopen 应该足以开始使用这个库。

下面就是我现在做的事:我第一次编译我的C ++库与 -shared -rdynamic -fPIC A .so文件。然后我编译我的C库与相同参数的.so文件。在那之后,我有装载C库前加载C ++与 DLOAD 库。否则加载失败了未定义的符号错误。

我想要做的就是这两个库编译成一个.so文件,以便只有一个调用 DLOAD 应该是足够用了。

我怎么能这样做?

先谢谢了。

编辑:编译为.o文件,然后合并不会为我工作。这是我做的:


  • 我编译每一个文件对象与-fPIC参数文件

  • 我联系​​他们铛[目标文件列表] -shared -rdynamic -fPIC -o libmylib.so

  • 当我尝试加载时,我得到未定义的符号:__gxx_personality_v0 错误

EDIT2:啊,我forgoto把它和的libstdc ++ 链接,现在的工作。谢谢你。


解决方案

  1. 最简单的方法是要么将所有的目标文件,或建造两个静态 .A 的.so 文件code>库,然后将它们连接成一个单一的共享库。

  2. 如果你真的想保持两个的.so 文件,第一个库链接到第二个,就像使用可执行文件。例如。如果 libfoo的取决于 libbar的,编译 libfoo的 -lbar 。你必须有依赖库在默认库路径或 LD_LIBRARY_PATH 环境变量。

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.
  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.

这篇关于生成多个C ++和C对象文件单一的.so的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆