Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2236048
  • 博文数量: 230
  • 博客积分: 9346
  • 博客等级: 中将
  • 技术积分: 3418
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-26 01:58
文章分类

全部博文(230)

文章存档

2015年(30)

2014年(7)

2013年(12)

2012年(2)

2011年(3)

2010年(42)

2009年(9)

2008年(15)

2007年(74)

2006年(36)

分类:

2007-06-08 16:18:52

Creating a shared and static library with the gnu compiler [gcc]
 
Here's a summary on how to create a shared and a static library with gcc. The goal is to show the basic steps. I do not want to go into the hairy details. It should be possible to use this page as a reference.
These examples were tested and run on cygwin/Windows.

The code for the library

This is the code that goes into the library. It exhibits one single function that takes two doubles and calculates their mean value and returns it.
calc_mean.c
 

//#include
##N(mean)
double mean(double a, double b) {
    return (a+b) / 2;
}

 
The header file
Of course, we need a header file.
calc_mean.h
double mean(double, double);

Creating the static library

A static library is basically a set of object files that were copied into a single file. This single file is the static library. The static file is created with the .
First, calc_mean.c is turned into an object file:
gcc  calc_mean.c  calc_mean.o
Then, the archiver (ar) is invoked to produce a static library (named libmean.a) out of the object file calc_mean.o.
  rcs libmean.a      calc_mean.o
Note: the library must start with the three letters lib and have the suffix .a.

Creating the shared library

As with static libraries, an object file is created. The option tells gcc to create position independant code which is necessary for shared libraries. Note also, that the object file created for the static library will be overwritten. That's not bad, however, because we have a static library that already contains the needed object file.
gcc   calc_mean.c -o calc_mean.o    
For some reason, gcc says:
cc1: warning: -fPIC ignored for target (all code is position independent)
It looks like -fPIC is not necessary on x86, but all manuals say, it's needed, so I use it too.
Now, the shared library is created
gcc  ,-soname,libmean.so.1  libmean.so.1.0.1  calc_mean.o
Note: the library must start with the three letter lib.

The programm using the library

This is the program that uses the calc_mean library. Once, we will link it against the static library and once against the shared library.
main.c

#include <stdio.h>
#include "calc_mean.h"

int main(int argc, char* argv[]) {
    double v1, v2, m;
    v1 = 5.2;
    v2 = 7.9;

    m = mean(v1, v2);
    printf("The mean of %3.2f and %3.2f is %3.2f\n", v1, v2, m);

    return 0;
}

Linking against static library

gcc  main.c . mean  statically_linked
Note: the first three letters (the lib) must not be specified, as well as the suffix (.a)

Linking against shared library

gcc main.c  dynamically_linked . mean
Note: the first three letters (the lib) must not be specified, as well as the suffix (.so)

Executing the dynamically linked programm

LD_LIBRARY_PATH=.
./dynamically_linked

Thanks

Thanks to Donn Morrison who helped me improve this page.
 
tips: compile a file with the library "readline" to static mode
      gcc -c file.c
      gcc -o file -static file.o /path/libreadline.a /path/libtermcap.a
阅读(3318) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~