gcc 编译动态库和静态库
来源:互联网 发布:编程培训多少钱 编辑:程序博客网 时间:2024/05/01 10:55
Linux C 编程入门之一:gcc 编译动态库和静态库
cheungmine
2012
1 准备工作
Windows7+Cygwin+gcc
在同一个目录下准备好下面3个文件,其中3-2,3-3用来生成动态库或静态库:
主调用程序源代码3-1:main.c
/** * main.c */#include <stdio.h>#include <math.h>#include "hello_fn.h"intmain (){ hello("cheungmine"); printf("sqrt(2.0) = %f\n", sqrt(2.0)); return 0;}库源代码3-2:hello_fn.c
/** * hello_fn.c */#include <stdio.h>void hello(const char *msg){ printf("Hello %s!\n", msg);}
库头文件源代码3-3:hello_fn.h
/** * hello_fn.h */void hello(const char *msg);
2 编译库
2.1 首先编译源文件生成对象(obj)文件(main.o, hello_fn.o):
$ gcc -W -Wall -ansi -pedantic -I. -c main.c$ gcc -W -Wall -ansi -pedantic -I. -c hello_fn.c
2.2 然后从对象文件编译动态库文件(libhello.so)和静态库(libhello.a)
$ gcc -shared hello_fn.o -o libhello.so或者直接从源代码编译:$ gcc -shared -I. hello_fn.c -o libhello.so编译静态库相对简单,就是相当于目标文件归档:$ ar r libhello.a hello_fn.o
3 编译使用库的主程序
3.1 如果不链接库的情况下编译一个主程序是:
$ gcc main.o -o main或者$ gcc -c main.c -o main但是由于我们在main.c的代码中写固定了调用库的代码(hello函数),所以,必须链接到这个库才行。
3.2 链接到动态库libhello.so
$ gcc main.o -o main ./libhello.so
这样在当前目录下就生成了:main.exe(我的cygwin环境,Linux环境下没有扩展名)
运行这个main.exe:
$ ./main.exe删除libhello.so,再运行main.exe会报错误:error while loading shared libraries: libhello.so: cannot open shared object...
3.3 链接到静态库libhello.a
$ gcc main.o -o main2 ./libhello.a删除libhello.a,运行main2.exe,一切正常。说明程序的确链接到静态库了。
4 查看程序依赖的库
$ file main.exe main2.exe$ ldd main.exe main2.exe如果我们的动态库libhello.so与主程序不在同一个目录下,怎么办?
复制libhello.so和libhello.a到另外一个目录,比如:/cygdrive/c/temp,那么编译主程序为:
$ gcc main.o -o main /cygdrive/c/temp/libhello.so执行:$ export PATH=/cygdrive/c/temp:$PATH$ ./main.exe
5 运行时加载动态库
修改main.c文件为如下清单:
/** * main.c */#include <stdio.h>#include <math.h>#include <dlfcn.h>#include "hello_fn.h"void dl_hello(){ void *dp; char *error; void (*fn_hello)(const char*); dp = dlopen("libhello.so", RTLD_LAZY ); if(!dp) { printf("%s\n", dlerror()); exit(1); } fn_hello = dlsym(dp, "hello"); if(!fn_hello) { printf("%s\n", dlerror()); exit(1); } fn_hello("cheungmine: load library when running"); dlclose(dp);} intmain (){// hello("cheungmine"); dl_hello(); printf("sqrt(2.0) = %f\n", sqrt(2.0)); return 0;}
然后重新编译main.exe和libhello.so如下:
编译源文件$ gcc -Wall -I. -c main.c$ gcc -Wall -I. -c hello_fn.c编译动态库$ gcc -shared hello_fn.o -o libhello.so链接主程序,但不链接到动态库。$ gcc main.o -o main.exe执行下面的代码可以看到libhello.so并不在main.exe的依存之列:$ ldd main.exe移动库到其他目录,通过修改环境变量,程序main.exe执行正确: $ mv libhello.so /cygdrive/c/temp$ export PATH=.:/cygdrive/c/temp:$PATH$ ./main.exe
6 总结
通过上面的练习,基本清楚了如何用gcc编译程序,包括静态链接库和动态链接库。通过下面的表格可以看到
Linux和Windows的区别:
Windows Unix/Linux
----------------------------------------------------------------------
静态链接库 hello.lib libhello.a
动态链接库 hello.dll libhello.so
延迟加载 LoadLibrary dlopen
GetProcAddress dlsym
FreeLibrary dlclose
本文全部内容在cygwin上运行的,和真实的Linux环境还是存在差异的。gcc版本3.4.4。
- GCC 编译动态库和静态库
- GCC 编译动态库和静态库
- GCC 编译动态库和静态库
- gcc 编译动态库和静态库
- gcc编译静态库和动态库
- gcc编译静态库和动态库
- gcc编译静态库和动态库
- gcc 编译动态库和静态库
- GCC 编译动态库和静态库
- gcc编译静态库和动态库
- GCC编译静态库和动态库
- GCC 编译动态库和静态库
- gcc编译静态库和动态库
- gcc编译静态库和动态库
- gcc编译动态库和静态库
- gcc 编译动态库和静态库
- gcc编译动态和静态链接库
- gcc编译动态和静态链接库
- javascript中的 非运算! 的问题
- MongoDB管理与开发精要《红丸出品》6 Capped Collection
- 你我对视,看谁比得过谁
- Ubuntu11.10下安装sqldeveloper
- MongoDB管理与开发精要《红丸出品》7 GridFS
- gcc 编译动态库和静态库
- MongoDB管理与开发精要《红丸出品》8 MapReduce
- 敏捷开发松结对编程系列之十:代码审查最佳实践
- 手把手教你如何配置和编译ogre 1.7.0 + cegui 0.7.1
- 解决winpcap无法卸载问题
- 在指定路径创建空数据库
- 虽然在修行初期的飞鸽传书
- 如异界守塔的伪原创工具
- C# 获取空闲端口及查看已用端口