一html
在应用程序须要链接外部库的状况下,linux默认对库的链接是使用动态库,在找不到动态库的状况下再选择静态库。使用方式为:mysql
gcc test.cpp -L. -ltestliblinux
若是当前目录有两个库libtestlib.so libtestlib.a 则确定是链接libtestlib.so。若是要指定为链接静态库则使用:sql
gcc test.cpp -L. -static -ltestlibwindows
使用静态库进行链接。函数
当对动态库与静态库混合链接的时候,使用-static会致使全部的库都使用静态链接的方式。这时须要做用-Wl的方式:ui
gcc test.cpp -L. -Wl,-Bstatic -ltestlib -Wl,-Bdynamic -ltestdll this
另外还要注意系统的运行库使用动态链接的方式,因此当动态库在静态库前面链接时,必须在命令行最后使用动态链接的命令才能正常链接.net
,如:命令行
gcc test.cpp -L. -Wl,-Bdynamic -ltestdll -Wl,-Bstatic -ltestlib -Wl,-Bdynamic
最后的-Wl,-Bdynamic表示将缺省库连接模式恢复成动态连接。
二:查看静态库导出函数
注意:参数信息只能存在于 .h 头文件中
windows下
dumpbin /exports libxxx.a
linux 下
nm -g --defined-only libxxx.a
三
场景是这样的。我在写一个Nginx模块,该模块使用了MySQL的C客户端接口库libmysqlclient,固然mysqlclient还引用了其余的库,好比libm, libz, libcrypto等等。对于使用mysqlclient的代码来讲,须要关心的只是mysqlclient引用到的动态库。大部分状况下,不是每台机器都安装有libmysqlclient,因此我想把这个库静态连接到Nginx模块中,但又不想把mysqlclient引用的其余库也静态的连接进来。
咱们知道gcc的-static选项可使连接器执行静态连接。但简单地使用-static显得有些’暴力’,由于他会把命令行中-static后面的全部-l指明的库都静态连接,更主要的是,有些库可能并无提供静态库(.a),而只提供了动态库(.so)。这样的话,使用-static就会形成连接错误。
以前的连接选项大体是这样的,
1 | CORE_LIBS="$CORE_LIBS -L/usr/lib64/mysql -lmysqlclient -lz -lcrypt -lnsl -lm -L/usr/lib64 -lssl -lcrypto" |
修改过是这样的,
12 | CORE_LIBS="$CORE_LIBS -L/usr/lib64/mysql -Wl,-Bstatic -lmysqlclient\ -Wl,-Bdynamic -lz -lcrypt -lnsl -lm -L/usr/lib64 -lssl -lcrypto" |
其中用到的两个选项:-Wl,-Bstatic和-Wl,-Bdynamic。这两个选项是gcc的特殊选项,它会将选项的参数传递给连接器,做为连接器的选项。好比-Wl,-Bstatic告诉连接器使用-Bstatic选项,该选项是告诉连接器,对接下来的-l选项使用静态连接;-Wl,-Bdynamic就是告诉连接器对接下来的-l选项使用动态连接。下面是man gcc对-Wl,option的描述,
-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.
下面是man ld分别对-Bstatic和-Bdynamic的描述,
-Bdynamic -dy -call_shared Link against dynamic libraries. You may use this option multiple times on the command line: it affects library searching for -l options which follow it. -Bstatic -dn -non_shared -static Do not link against shared libraries. You may use this option multiple times on the command line: it affects library searching for -l options which follow it. This option also implies --unresolved-symbols=report-all. This option can be used with -shared. Doing so means that a shared library is being created but that all of the library's external references must be resolved by pulling in entries from static libraries
http://blog.csdn.net/lapal/article/details/5482277
http://blog.chinaunix.net/uid-20737871-id-3083925.html