GCC动态库和静态库混合使用

本文详细介绍了在Linux环境下如何创建和使用静态库及动态库,并解释了混合使用两者的具体方法。通过实例演示了从创建库文件到在主程序中调用的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天,一个同事编译静态库,我也趁此机会在温习一下,先google一下,然后在自己实验。

 

首先,在网上抄个例子,内容如下:

  :建静态库
/*   hellos.h   */
#ifndef _HELLO_S_H
#define  _HELLO_S_H

void  printS( char *  str);

#endif

/*   hellos.c   */
#include 
" hellos.h "

void  printS( char *  str) {
  printf(
" print in static way: %s " , str);
}

输入命令:
gcc 
- - o hellos.o hellos.c
ar cqs libhellos.a hellos.o 
于是得到了 libhellos.a这么一个静态链接库

:主程序
/*   main.c   */
#include 
< stdio.h >
#include 
" hellos.h "

main() {
  
char *  text  =   " Hello World!\n " ;
  printS(text);
}
编译链接:
gcc 
- o hello main.c  - static   - L.  - lhellos 
然后运行hello可以看到输出
print 
in   static  way: Hello World !
删除 libhellos.a和 hellos.
* 后, 程序仍然正常运行。 
下面再来看 动态链接 
:建动态库
/*   hellod.h   */
#ifndef _HELLO_D_H
#define  _HELLO_D_H

void  printD( char *  str);

#endif

/*   hellod.c   */
#include 
" hellod.h "

void  printD( char *  str) {
  printf(
" print in dynamic way: %s " , str);
}

输入命令:
gcc 
- shared  - o libhellod.so hellod.c 
于是得到了 libhellod.so 这么一个动态链接库 

: 主程序
/*   main.c   */
#include 
< stdio.h >
#include 
" hellod.h "

main() {
  
char *  text  =   " Hello World!\n " ;
  printD(text);
}
编译链接:
gcc 
- o hello main.c  - L.  - lhellod 

原文中存在一定错误,我已经修订,并且最会运行的话,需要设置动态库执行路径

否则出现下述错误:

  ./hello: error while loading shared libraries: libmyhello.so: cannot open shared object file: No such file or directory

当设定了LD_LIBRARY_PATH或者放在/usr/lib和/lib后,又出现下面的错误:

   ./hello: error while loading shared libraries: /usr/lib/libhello.so: cannot restore segment prot after reloc: Permission denied

这是SELinux引起的,修改如下:

chcon -t texrel_shlib_t /usr/lib/libhello.so

之后,运行,OK。

下面说一下混合编程:

 

/*   main.c   */
#include 
< stdio.h >
#include 
" hellos.h "
#include 
" hellod.h "

main() {
  
char *  text  =   " Hello World!\n " ;
  printS(text);
  printD(text);
}

上面的文章又说错了,自己google一下,某人如下说:

 

在应用程序需要连接外部库的情况下,linux默认对库的连接是使用动态库,在找不到动态库的情况下再选择静态库。使用方式为:

gcc test.cpp -L. -ltestlib

如果当前目录有两个库libtestlib.so libtestlib.a 则肯定是连接libtestlib.so。如果要指定为连接静态库则使用:

gcc test.cpp -L. -static -ltestlib

使用静态库进行连接。

当对动态库与静态库混合连接的时候,使用-static会导致所有的库都使用静态连接的方式。这时需要作用-Wl的方式:

gcc test.cpp -L. -Wl,-Bstatic -ltestlib  -Wl,-Bdynamic -ltestlib

 【本段文字来自:http://blog.youkuaiyun.com/lapal/archive/2010/04/13/5482277.aspx

在转他说的:http://blog.youkuaiyun.com/tenfyguo/archive/2010/07/15/5737974.aspx

至此,解决该类问题。

 

附带几篇不错的文章:

 

http://blog.163.com/xychenbaihu@yeah/blog/static/13222965520101023104745738/

http://www.cublog.cn/u3/119070/showart_2379190.html

http://blog.youkuaiyun.com/tenfyguo/archive/2010/07/15/5737974.aspx

http://www.linuxeden.com/html/develop/20100326/94297.html

http://blog.youkuaiyun.com/JsuFcz/archive/2009/11/07/4784038.aspx『pS:这小子的文章前面的还好,后面可给我害惨了】

转载于:https://www.cnblogs.com/diyunpeng/archive/2011/06/16/2083085.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值