将一个共享库静态链接到我的共享库 [英] Link one shared library static to my shared library

查看:137
本文介绍了将一个共享库静态链接到我的共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图创建一个链接到一个共享库的链接其他2个图书馆。 (我们称它们为 libfoo.so libbar.so

我的输出库必须是一个共享库,我想静态链接 libfoo.so 到结果库,但是 libbar.so 应该链接作为一个动态库。 ( libbar.so )应该在每台机器上都可用,其中 libfoo.so 不可用,我不希望)



我如何归档这个?



我的当前版本指令如下所示:

  c ++ -Wall -shared -c -o src / lib.o src / lib.cpp 
c ++ -Wall -shared -o lib.ndll src / lib.o -lfoo -lbar

我的防守:我不是AC / C ++专家,所以很抱歉如果这个问题看起来很愚蠢。

解决方案

Linux C / C ++库类型。


  • 静态库( *。a )是目标代码它们与应用程序链接并成为其一部分。他们创建并可以使用 ar(1) 命令(即 ar -t libfoo.a 将列出库/档案中的文件)。


  • 可以通过两种方式使用动态链接的共享库( *。so )。


    1. 共享对象库可以在运行时动态链接,但可以静态识别。这些库在编译/链接阶段必须可用。共享对象不包含在二进制可执行文件中,但与执行绑定。

    2. 使用动态链接加载程序系统函数,可以动态加载/卸载和链接共享对象库。







libfoo.so 链接到您的二进制文件中,您将需要一个相应的静态库,通常称为 libfoo.a 。您可以在创建程序可执行文件时通过调用它作为编译和链接过程的一部分来使用静态库。



结果会将您的构建命令更改为以下内容:

  g ++ -Wall -fPIC -c -o src / lib.o src / lib.cpp 
g ++ -shared -Wl,-soname,mylib.so.1 -o mylib.so.1 src / lib.o -L / path / to / library-directory -lbar libfoo.a


I am struggeling a little bit with some options for linking on a project I am currently working on:

I am trying to create a shared library which is linked against 2 other libraries. (Lets call them libfoo.so and libbar.so)
My output library has to be a shared library and I want to static link libfoo.so to the resulting library, but libbar.so should linked as a dynamic library. (libbar.so should be available on every machine, where libfoo.so is not available and I do not want the user install it / ship it with my binaries.)

How could I archive this?

My current build instruction look like this:

c++ -Wall -shared -c -o src/lib.o src/lib.cpp
c++ -Wall -shared -o lib.ndll src/lib.o -lfoo -lbar

I my defense: I am not a c/c++ expert, so sorry if this question seems to be stupid.

解决方案

There are two Linux C/C++ library types.

  • Static libraries (*.a) are archives of object code which are linked with and becomes part of the application. They are created with and can be manipulated using the ar(1) command (i.e. ar -t libfoo.a will list the files in the library/archive).

  • Dynamically linked shared object libraries (*.so) can be used in two ways.

    1. The shared object libraries can be dynamically linked at run time but statically aware. The libraries must be available during compile/link phase. The shared objects are not included into the binary executable but are tied to the execution.
    2. The shared object libraries can be dynamically loaded/unloaded and linked during execution using the dynamic linking loader system functions.


In order to statically link libfoo.so into your binary, you will need a corresponding static library which is typically called libfoo.a. You can use a static library by invoking it as part of the compilation and linking process when creating a program executable.

The result would be changing your build commands to something like the following:

g++ -Wall -fPIC -c -o src/lib.o src/lib.cpp
g++ -shared -Wl,-soname,mylib.so.1 -o mylib.so.1 src/lib.o -L/path/to/library-directory -lbar libfoo.a 

这篇关于将一个共享库静态链接到我的共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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