关于GCC中同时使用动态和静态库链接的操作参数和解释

时间:2022-10-14 04:55:30
在我们开发的一个系统中,由于动态链接其中的一个动态库时,编译时没有问题,而运行时不能进行,如果将该库静态连接时,运行却没有问题。具体什么原因,一直没有搞清楚,权且当作暂时的解决办法。
   如何同时 同时使用动态和静态库链接,同事周楠提供了一个参数的用法,在GCC指令参数中具体参数如下:
    -Wl,-Bstatic -L/usr/local/sqlite-arm-linux/.libs -lsqlite -Wl,-Bdynamic -L/usr/local/arm/3.3.2/lib
 
 具体用途解释:sqlite库静态连接,其它库动态连接。
-Wl,-Bstatic 与-Wl,-Bdynamic参数,从字面意义上可以理解,有静态和动态的意思,但是具体的真正规则在查找了GCC的原版手册上有说明。
 
原文:

Note - if the linker is being invoked indirectly, via a compiler driver (eg gcc) then all the linker command line options should be prefixed by -Wl, (or whatever is appropriate for the particular compiler driver) like this:

gcc -Wl,--startgroup foo.o bar.o -Wl,--endgroup
 

This is important, because otherwise the compiler driver program may silently drop the linker options, resulting in a bad link.

 

实际上主要针对隐式应用LINKER的参数,用“-Wl,”来标识,,“--startgroup foo.o bar.o -Wl,--endgroup”表示一组,,-Bstatic -Bdynamic 作为关键字与-WL,不可分,在GCC连接库时,默认链接是动态链接,现在用上面的指令限制在链接sqlite库时采用静态链接。

 

-Bstatic 还有三个写法: -dn和-non_shared 和-static

-Bdynamic 还有两个写法:-dy 和-call_shared

上面参数“-L/usr/local/sqlite-arm-linux/.libs ”放不放在-Wl,...之间无所谓,因为它只是提供了sqlite动静态库的位置。可以改成下面的参数形式,更直观。

 

-L/usr/local/sqlite-arm-linux/.libs -L/usr/local/arm/3.3.2/lib -Wl,-dn -lsqlite -Wl,-dy  

 

-Wl,-dn 和 -Wl,-dy成对出现才能起到标题所说的作用。  

 

关于-Wl,后面的参数还有很多,全部明白我也不能。

 

还有一个问题值得注意,在-Wl,后面不能有空格,否则会出错!

 

关于-Wl,option 说明还有一段说明

 

GCC命令参数的英文原文

 

-Wl,option

Pass option as an option to the linker. If option contains commas, it is split into multiple options at the commas.

传递参数option作为linker的一个参数,如果option包含逗号,将在逗号处分割成几个参数。

 

例如:

-Wl,-dn –lsqlite

-dn 开始静态链接

-lsqlite 静态链接sqlite库

静态链接完后,然后需要动态链接

-Wl,-dy

重新开始动态链接。


 -T script
           Use script as the linker script.  This option is supported by most systems using the GNU linker.  On some targets, such as bare-board targets
           without an operating system, the -T option may be required when linking to avoid references to undefined symbols.


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


           If you want to pass an option that takes a separate argument, you must use -Xlinker twice, once for the option and once for the argument.  For
           example, to pass -assert definitions, you must write -Xlinker -assert -Xlinker definitions.  It does not work to write -Xlinker "-assert
           definitions", because this passes the entire string as a single argument, which is not what the linker expects.


           When using the GNU linker, it is usually more convenient to pass arguments to linker options using the option=value syntax than as separate
           arguments.  For example, you can specify -Xlinker -Map=output.map rather than -Xlinker -Map -Xlinker output.map.  Other linkers may not
           support this syntax for command-line options.


       -Wl,option
           Pass option as an option to the linker.  If option contains commas, it is split into multiple options at the commas.  You can use this syntax
           to pass an argument to the option.  For example, -Wl,-Map,output.map passes -Map output.map to the linker.  When using the GNU linker, you can
           also get the same effect with -Wl,-Map=output.map.


           NOTE: In Ubuntu 8.10 and later versions, for LDFLAGS, the option -Wl,-z,relro is used.  To disable, use -Wl,-z,norelro.