---
title: "gcc 编译动态库和静态库 1"
author: "samson7b"
date: "2012-07-09T10:27:45Z"
tags: []
categories: ["编程语言"]
slug: "gcc-1"
draft: false
lastmod: "2012-07-09T10:27:45Z"
id: 1582314
url: "https://samson7b.iteye.com/blog/1582314"
---

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

 

cheungmine

 

2012

1 准备工作

 

Windows7+Cygwin+gcc

 

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

 

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

 

\[cpp] view plaincopyprint?

 

    /\** 

     * main.c 

     \*/  

    #include &lt;stdio.h&gt;  

    #include &lt;math.h&gt;  

 

    #include "hello\_fn.h"  

 

    int  

    main ()  

    {  

      hello("cheungmine");  

      printf("sqrt(2.0) = %f\\n", sqrt(2.0));  

      return 0;  

    }  

 

库源代码3-2：hello\_fn.c

\[cpp] view plaincopyprint?

 

    /\** 

     * hello\_fn.c 

     \*/  

    #include &lt;stdio.h&gt;  

 

    void hello(const char \*msg)  

    {  

      printf("Hello %s!\\n", msg);  

    }  

 

 

 

 

库头文件源代码3-3：hello\_fn.h

 

\[cpp] view plaincopyprint?

 

    /\** 

     * hello\_fn.h 

     \*/  

    void hello(const char \*msg);  

 

2 编译库

2.1 首先编译源文件生成对象(obj)文件(main.o, hello\_fn.o)：

 

\[plain] view plaincopyprint?

 

    $ 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)

 

\[plain] view plaincopyprint?

 

    $ 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 如果不链接库的情况下编译一个主程序是：

 

\[plain] view plaincopyprint?

 

    $ gcc main.o -o main  

    或者  

    $ gcc -c main.c -o main  

 

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

3.2 链接到动态库libhello.so

 

\[plain] view plaincopyprint?

 

    $ gcc main.o -o main ./libhello.so  

 

 

这样在当前目录下就生成了：main.exe（我的cygwin环境，Linux环境下没有扩展名）

 

运行这个main.exe：

 

\[plain] view plaincopyprint?

 

    $ ./main.exe  

 

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

3.3 链接到静态库libhello.a

 

\[plain] view plaincopyprint?

 

    $ gcc main.o -o main2 ./libhello.a  

 

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

4 查看程序依赖的库

 

\[plain] view plaincopyprint?

 

    $ file main.exe main2.exe  

    $ ldd main.exe main2.exe  

 

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

 

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

 

\[plain] view plaincopyprint?

 

    $ gcc main.o -o main /cygdrive/c/temp/libhello.so  

    执行：  

    $ export PATH=/cygdrive/c/temp:$PATH  

    $ ./main.exe   

 

5 运行时加载动态库

 

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

 

\[cpp] view plaincopyprint?

 

    /\** 

     * main.c 

     \*/  

    #include &lt;stdio.h&gt;  

    #include &lt;math.h&gt;  

    #include &lt;dlfcn.h&gt;  

    #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 ()  

    {  

    //  hello("cheungmine");  

      dl\_hello();  

      printf("sqrt(2.0) = %f\\n", sqrt(2.0));  

      return 0;  

    }  

 

 

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

 

\[cpp] view plaincopyprint?

 

    编译源文件  

    $ 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