Chinaunix首页 | 论坛 | 博客
  • 博客访问: 275742
  • 博文数量: 150
  • 博客积分: 2396
  • 博客等级: 大尉
  • 技术积分: 1536
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-19 09:55
文章分类

全部博文(150)

文章存档

2021年(1)

2015年(9)

2014年(7)

2013年(50)

2012年(33)

2011年(1)

2010年(13)

2009年(36)

我的朋友

分类: LINUX

2012-09-19 13:15:02

一.写文件hello.c
#include
void hello()
{
 printf("hello,welcome to library world\n");
}
gcc -o hello.o -c hello.c
生成文件hello.o
二.gcc -shared -fPCI -o libhello.so hello.o

# -shared 表明是使用共享库
# -fpci或者-fPCI表明创建产生独立目标代码, 具体应用取决于平台
##this time you get libhello.so
三. 写文件hello_test.c
#include
int main(void)
{
printf("begin to use libary\n");
hello();
return 0;
}
编译gcc -o hello_test.o -o hello_test.c
生成文件hello_test.o
四. 链接静态库生成可执行文件
gcc -o hello_share hello_test.o -L. -lhello

五.运行./hello_share
出错解决:
cp libhello.so /usr/lib
export LD_LIBRARY_PATH=.
修改/etc/ld.so.conf文件,把库所在的路径加到文件末尾,并执行ldconfig刷新
 ./hello_share
注: rm /usr/lib/libhello.so
     ./hello_share 结果是出现找不到库文件的错误
 
 关于编译*.c 文件链接库文件的做法:
gcc -I./include/ -o time-test time-test.c -L./  -levent -lrt  上面是链接静态库,需要加-lrt 

gcc -I./include/ -o time-test time-test.c -L./  -levent 下面这个是链接动态库,然后还要指定动态库的路径
export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
 
 
阅读(627) | 评论(0) | 转发(0) |
0

上一篇:build mplayer

下一篇:C调用C++静态库

给主人留下些什么吧!~~