在动态库中包含静态库 [英] Include static lib in dynamic lib

查看:919
本文介绍了在动态库中包含静态库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题:


  • 几个目标文件a1.o,a2.o,a3.o,...

  • 存档libxxxx.a。
    存档libxxxx.a只包含目标文件:b1.o,b2.o等...


    我会像所有对象文件一样创建一个共享库(.so)。



    问题:如果我使用:

      g ++ -shared libxxxx.a a1.o a2.o ... -o libnew.so 

    g ++确实知道我想链接到静态库libxxxx.a,并且不包含档案的所有符号。



    一个简单的解决方法是首先使用 ar -x 扩展存档,然后创建这个lib,但它并不真正优雅。



    必须有一个简单的命令来强制g ++将整个档案包含在.so中,但我找不到它。



    感谢您的帮助。

    解决方案


    - 整个存档

    对于在命令行中提到的每个存档打包归档选项时,请在
    链接中的归档文件中包含每个对象文件,而不是在归档文件中搜索所需的对象文件。这通常用于将存档文件转换为共享
    库,强制每个对象都包含在生成的共享库中。这个选项可能会被多次使用。

    从gcc使用这个选项时需要注意两点:首先,gcc不知道这个选项,所以你必须使用 - WL,-whole归档。
    其次,不要忘记在归档列表后面使用-Wl,-no-whole-archive,因为gcc会将自己的归档列表添加到
    链接中,并且您可能不希望使用此标志

    示例:

      g ++ -shared -o yourlib ao b.o. co -Wl,-whole-archive libstatic.a -Wl,-no-whole-archive 

    注意在你的例子中你首先要放置静态库,然后是对象文件 - 在这种情况下,除非使用 - whole-archive 链接器选项。如果你想只包含静态库中所需的符号,你需要在目标文件后面加上

      g ++ -o your_app ao bo co -lyour_static_lib 


    I have the following problem :

    • several object files a1.o, a2.o, a3.o, ...
    • an archive libxxxx.a. The archive libxxxx.a only contains object files : b1.o, b2.o etc...

    I would like to create a shared library (.so) from all the object files.

    Problem : If I use :

    g++ -shared libxxxx.a a1.o a2.o ... -o libnew.so
    

    g++ does understand that I want to link with the static library libxxxx.a and does not include all the symbols of the archive.

    A simple workaround is to first expand the archive using ar -x and then create the lib but it's not really "elegant".

    There must be a simple command to force g++ to include the whole archive in the .so but I can't find it.

    Thanks for your help.

    解决方案

    From man ld:

    --whole-archive
    For each archive mentioned on the command line after the --whole-archive option, include every object file in the archive in the link, rather than searching the archive for the required object files. This is normally used to turn an archive file into a shared library, forcing every object to be included in the resulting shared library. This option may be used more than once.

    Two notes when using this option from gcc: First, gcc doesn't know about this option, so you have to use -Wl,-whole-archive. Second, don't forget to use -Wl,-no-whole-archive after your list of archives, because gcc will add its own list of archives to your link and you may not want this flag to affect those as well.

    Example:

    g++ -shared -o yourlib a.o. b.o. c.o -Wl,-whole-archive libstatic.a -Wl,-no-whole-archive
    

    Note also that in your example you first put static library, then the object files - in this case the symbols used in the object files and defined in static library will not be found unless you use --whole-archive linker option. If you want to include just the needed symbols from the static library, you need to put it after the object files.

    g++ -o your_app a.o b.o c.o -lyour_static_lib
    

    这篇关于在动态库中包含静态库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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