DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

背景

GCC中已经指定链接库,然而编译时却提示动态库函数未定义!

测试出现的错误提示如下:

 
  1.  
    [GMPY@13:48 tmp]$gcc -o test -L. -lmylib test.c
  2.  
    /tmp/ccysQZI3.o:在函数‘main’中:
  3.  
    test.c:(.text+0x1a):对‘func_lib’未定义的引用
  4.  
    collect2: error: ld returned 1 exit status
 

而在测试用的动态库libmylib.so中是有定义函数func_lib

 
  1.  
    [GMPY@13:55 tmp]$cat mylib.c
  2.  
    #include <stdio.h>
  3.  
     
  4.  
    int func_lib(void)
  5.  
    {
  6.  
    printf("In share library\n");
  7.  
    return 0;
  8.  
    }
  9.  
    [GMPY@13:56 tmp]$gcc -fPIC -shared mylib.c -o libmylib.so
 

GCC的链接坑

此处的"坑"指对不熟悉GCC机制的童鞋而言,会出现无法理解的不符合预期的效果

在用gcc编译时,我们可以用-L指定链接库位置,用-l指定。

man gcc查询时,我发现这么一段描述:

 
  1.  
    -llibrary
  2.  
    -l library
  3.  
    ... ## 这里为了方便阅读,对原文进行了换行排版优化
  4.  
    It makes a difference where in the command you write this option;
  5.  
    the linker searches and processes libraries and object files in the order they are specified.
  6.  
    Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o.
  7.  
    If bar.o refers to functions in z, those functions may not be loaded.
  8.  
    ...
 

嗯,这段话什么意思呢? 如果-l链接库在源码之前,就会链接不到库!!

就像下面两个命令的差别:

 
  1.  
    异常:gcc -o test -L. -lmylib test.c
  2.  
    正常:gcc -o test -L. test.c -lmylib
 

竟然对执行时参数的位置都有要求,也是醉了

GCC的链接步骤

感谢 @firstrose 提供的原理说明链接

GCC是怎么样理解-l的呢?

 
  1.  
    A library is a collection (an archive) of object files. When you add a library using the -l option,
  2.  
    the linker does not unconditionally take all object files from the library. It only takes those object
  3.  
    files that are currently needed, i.e. files that resolve some currently unresolved (pending) symbols.
  4.  
    After that, the linker completely forgets about that library.
  5.  
     
  6.  
    The list of pending symbols is continuously maintained by the linker as the linker processes input
  7.  
    object files, one after another from left to right. As it processes each object file, some symbols get
  8.  
    resolved and removed from the list, other newly discovered unresolved symbols get added to the list.
  9.  
     
  10.  
    So, if you included some library by using -l, the linker uses that library to resolve as many currently
  11.  
    pending symbols as it can, and then completely forgets about that library. If it later suddenly
  12.  
    discovers that it now needs some additional object file(s) from that library, the linker will not "return"
  13.  
    to that library to retrieve those additional object files. It is already too late.
 

什么个意思呢?就是说,GCC链接器按下面的步骤链接文件:

  1. 从左往右链接源文件
  2. 在链接时,如果出现源文件调用了却没有定义的函数、变量等,则记录下来
  3. 如果遇到-l指定的库,则在库中尽可能检索所有记录下来的没有定义的函数、变量,只从库中提取用到的部分,其他完全抛弃
  4. 在全部链接完后,如果依然存在未定义的函数、变量,则报错

正因为GCC链接器的"始乱终弃",在检索-l的库后,就抛弃了这个库,后面还需要用时,自然就找不到了

GCC并不会回过头来检索之前链接过的库

从这样的链接步骤,我猜测还会有个特性:

由于GCC链接库会在首先指定的库中"贪婪匹配"所有未定义的函数、变量,因此,

即使两个库有相同的函数、变量的定义,只会使用最先找到的库中的定义

posted on 2023-01-04 13:53  DoubleLi  阅读(482)  评论(0编辑  收藏  举报