http://blog.csdn.net/ubuntu64fan/article/details/7684800
Linux C 编程入门之一:gcc 编译动态库和静态库
cheungmine
2012
1 准备工作
Windows7+Cygwin+gcc
在同一个目录下准备好下面3个文件,其中3-2,3-3用来生成动态库或静态库:
主调用程序源代码3-1:main.c
#include <stdio.h> #include <math.h> #include "hello_fn.h" int main () { hello("cheungmine" ); printf("sqrt(2.0) = %f\n" , sqrt(2.0)); return 0; }
库源代码3-2:hello_fn.c
#include <stdio.h> void hello( const char *msg) { printf("Hello %s!\n" , msg); }
库头文件源代码3-3: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:
删除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文件为如下清单:
#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); } int main () { 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。