zoukankan      html  css  js  c++  java
  • 三种标准库链接方式

    Linux 应用程序因为 Linux 版本的众多与各自独立性,在工程制作与使用中必须熟练掌握如下两点才能有效地工作和理想地运行。
    1.Linux 下标准库链接的三种方式(全静态 , 半静态 (libgcc,libstdc++), 全动态)及其各自利弊。
    2.Linux 下如何巧妙构建 achrive(*.a),并且如何设置链接选项来解决 gcc 比较特别的链接库的顺序问题。
    全静态:
    1.-static -pthread -lrt -ldl
    2.不会发生应用程序在 不同 Linux 版本下的标准库不兼容问题。
    3.生成的文件比较大,应用程序功能受限(不能调用动态库等)
    全动态
    1.-pthread -lrt -ldl
    2.生成文件是三者中最小的
    3.比较容易发生应用程序在不同 Linux 版本下标准库依赖不兼容问题。
    半静态 (libgcc,libstdc++)
    1.-static-libgcc -L. -pthread -lrt -ldl
    2.灵活度大,能够针对不同的标准库采取不同的链接策略,从而避免不兼容问题发生。
    3.结合了全静态与全动态两种链接方式的优点。
    4.比较难识别哪些库容易发生不兼容问题,目前只有依靠经验积累。
    5.某些功能会因选择的标准库版本而丧失。

    Linux 静态库链接顺序问题及解决方法:
    正如 GCC 手册中提到的那样:
    It makes a difference where in the command you write this option; the linker
    searches and processes libraries and object files in the order they are specified.
    Thus, ‘ foo.o -lz bar.o ’ searches library ‘ z ’ after file ‘ foo.o ’ but before
    ‘ bar.o ’ . If ‘ bar.o ’ refers to functions in ‘ z ’ , those functions may not be loaded.
    为了解决这种库链接顺序问题,我们需要增加一些链接选项 :
    $(CXX) $(LINKFLAGS) $(OBJS) -Xlinker “-(” $(LIBS) -Xlinker “-)” -o $@
    通过将所有需要被链接的静态库放入 -Xlinker “-(” 与 -Xlinker “-)” 之间,可以是 g++ 链接过程中, 自动循环链接所有静态库,从而解决了原本的链接顺序问题。
    涉及链接选项:-Xlinker
    -Xlinker option
    Pass option as an option to the linker. You can use this to supply system-specific
    linker options which GCC does not know how to recognize.

    http://www.kxtry.com/archives/566

  • 相关阅读:
    php环境配置中各个模块在网站建设中的功能
    PHP+Apache+MySQL+phpMyAdmin在win7系统下的环境配置
    August 17th 2017 Week 33rd Thursday
    August 16th 2017 Week 33rd Wednesday
    August 15th 2017 Week 33rd Tuesday
    August 14th 2017 Week 33rd Monday
    August 13th 2017 Week 33rd Sunday
    August 12th 2017 Week 32nd Saturday
    August 11th 2017 Week 32nd Friday
    August 10th 2017 Week 32nd Thursday
  • 原文地址:https://www.cnblogs.com/findumars/p/5661850.html
Copyright © 2011-2022 走看看