gcc 编译动态库和静态库

Linux C 编程入门之一:gcc 编译动态库和静态库

cheungmine

2012

参考:

C程序编译过程浅析

http://blog.csdn.net/koudaidai/article/details/8092647

1 准备工作

Windows7+Cygwin+gcc

在同一个目录下准备好下面3个文件,其中3-2,3-3用来生成动态库或静态库:

主调用程序源代码3-1:main.c

[cpp] view plain copy
 
 print?
  1. /** 
  2.  * main.c 
  3.  */  
  4. #include <stdio.h>  
  5. #include <math.h>  
  6.   
  7. #include "hello_fn.h"  
  8.   
  9. int  
  10. main ()  
  11. {  
  12.   hello("cheungmine");  
  13.   printf("sqrt(2.0) = %f ", sqrt(2.0));  
  14.   return 0;  
  15. }  

库源代码3-2:hello_fn.c

[cpp] view plain copy
 
 print?
  1. /** 
  2.  * hello_fn.c 
  3.  */  
  4. #include <stdio.h>  
  5.   
  6. void hello(const char *msg)  
  7. {  
  8.   printf("Hello %s! ", msg);  
  9. }  
  10.   
  11.   
  12.    

库头文件源代码3-3:hello_fn.h

[cpp] view plain copy
 
 print?
  1. /** 
  2.  * hello_fn.h 
  3.  */  
  4. void hello(const char *msg);  

2 编译库

2.1 首先编译源文件生成对象(obj)文件(main.o, hello_fn.o):

[plain] view plain copy
 
 print?
  1. $ gcc -W -Wall -ansi -pedantic -I. -c main.c  
  2. $ gcc -W -Wall -ansi -pedantic -I. -c hello_fn.c  

2.2 然后从对象文件编译动态库文件(libhello.so)和静态库(libhello.a)

[plain] view plain copy
 
 print?
  1. $ gcc -shared hello_fn.o -o libhello.so  
  2. 或者直接从源代码编译:  
  3. $ gcc -shared -I. hello_fn.c -o libhello.so  
  4. 编译静态库相对简单,就是相当于目标文件归档:  
  5. $ ar r libhello.a hello_fn.o  

3 编译使用库的主程序

3.1 如果不链接库的情况下编译一个主程序是:

[plain] view plain copy
 
 print?
  1. $ gcc main.o -o main  
  2. 或者  
  3. $ gcc -c main.c -o main  

但是由于我们在main.c的代码中写固定了调用库的代码(hello函数),所以,必须链接到这个库才行。

3.2 链接到动态库libhello.so

[plain] view plain copy
 
 print?
  1. $ gcc main.o -o main ./libhello.so  


这样在当前目录下就生成了:main.exe(我的cygwin环境,Linux环境下没有扩展名)

运行这个main.exe:

[plain] view plain copy
 
 print?
  1. $ ./main.exe  

删除libhello.so,再运行main.exe会报错误:error while loading shared libraries: libhello.so: cannot open shared object...

3.3 链接到静态库libhello.a

[plain] view plain copy
 
 print?
  1. $ gcc main.o -o main2 ./libhello.a  

删除libhello.a,运行main2.exe,一切正常。说明程序的确链接到静态库了。

4 查看程序依赖的库

[plain] view plain copy
 
 print?
  1. $ file main.exe main2.exe  
  2. $ ldd main.exe main2.exe  

如果我们的动态库libhello.so与主程序不在同一个目录下,怎么办?

复制libhello.so和libhello.a到另外一个目录,比如:/cygdrive/c/temp,那么编译主程序为:

[plain] view plain copy
 
 print?
  1. $ gcc main.o -o main /cygdrive/c/temp/libhello.so  
  2. 执行:  
  3. $ export PATH=/cygdrive/c/temp:$PATH  
  4. $ ./main.exe   

5 运行时加载动态库

修改main.c文件为如下清单:

[cpp] view plain copy
 
 print?
  1. /** 
  2.  * main.c 
  3.  */  
  4. #include <stdio.h>  
  5. #include <math.h>  
  6. #include <dlfcn.h>  
  7. #include "hello_fn.h"  
  8.   
  9. void dl_hello()  
  10. {  
  11.   void *dp;  
  12.   char *error;  
  13.   void (*fn_hello)(const char*);  
  14.   
  15.   dp = dlopen("libhello.so", RTLD_LAZY );  
  16.   if(!dp) {  
  17.     printf("%s ", dlerror());  
  18.     exit(1);  
  19.   }  
  20.   
  21.   fn_hello = dlsym(dp, "hello");  
  22.   if(!fn_hello) {  
  23.     printf("%s ", dlerror());  
  24.     exit(1);  
  25.   }  
  26.   
  27.   fn_hello("cheungmine: load library when running");  
  28.   
  29.   dlclose(dp);  
  30. }  
  31.       
  32. int  
  33. main ()  
  34. {  
  35. //  hello("cheungmine");  
  36.   dl_hello();  
  37.   printf("sqrt(2.0) = %f ", sqrt(2.0));  
  38.   return 0;  
  39. }  


然后重新编译main.exe和libhello.so如下:

[cpp] view plain copy
 
 print?
  1. 编译源文件  
  2. $ gcc -Wall -I. -c main.c  
  3. $ gcc -Wall -I. -c hello_fn.c  
  4. 编译动态库  
  5. $ gcc -shared hello_fn.o -o libhello.so  
  6. 链接主程序,但不链接到动态库。  
  7. $ gcc main.o -o main.exe  
  8. 执行下面的代码可以看到libhello.so并不在main.exe的依存之列:  
  9. $ ldd main.exe  
  10. 移动库到其他目录,通过修改环境变量,程序main.exe执行正确:   
  11. $ mv libhello.so /cygdrive/c/temp  
  12. $ export PATH=.:/cygdrive/c/temp:$PATH  
  13. $ ./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。

原文:

http://blog.csdn.net/ubuntu64fan/article/details/7684800

原文地址:https://www.cnblogs.com/jiftle/p/6868243.html