gcc生成.a.so静态库和动态库文件

类别:编程语言 点击:0 评论:0 推荐:

因为我想把字体文件作成一个库,以便使用。查了一些资料,现总结下生成.a库文件的方法。
/***************************************************/
/* gdfontwu.c */
#include "gdfontwu.h"

char gdFontWuData[] = {

/* Char 0 */
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0,
  0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0,
  0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0,
  0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0,
  0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0,
  0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0,
  0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0,
  0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0,
  0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0,
  0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0,
  0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0,
  0, 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 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0,
  0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,


};

gdFont gdFontWuRep = {
  123,
  48,
  18,
  30,
  gdFontWuData
};

BGD_EXPORT_DATA_IMPL gdFontPtr gdFontWu = &gdFontWuRep;

BGD_DECLARE(gdFontPtr) gdFontGetWu (void)
{
  return gdFontWu;
}
/***************************************************/
/*gdfontliu.c */

#include "gdfontliu.h"

char gdFontLiuData[] = {

/* Char 0 */

......... 如上

};

gdFont gdFontLiuRep = {
  123,
  48,
  18,
  30,
  gdFontLiuData
};

BGD_EXPORT_DATA_IMPL gdFontPtr gdFontLiu = &gdFontLiuRep;

BGD_DECLARE(gdFontPtr) gdFontGetLiu (void)
{
  return gdFontLiu;
}
/***************************************************/
/*gdfontliu.c */

#include "gdfontbin.h"

char gdFontBinData[] = {

........... 如上

};

gdFont gdFontBinRep = {
  123,
  48,
  18,
  30,
  gdFontBinData
};

BGD_EXPORT_DATA_IMPL gdFontPtr gdFontBin = &gdFontBinRep;

BGD_DECLARE(gdFontPtr) gdFontGetBin (void)
{
  return gdFontBin;
}

/****************************************************/
还需要头文件声明导出的函数:

/* gdfontwu.h */
#include "gd.h"
   BGD_EXPORT_DATA_PROT gdFontPtr gdFontWu;
   BGD_DECLARE(gdFontPtr) gdFontGetWu(void);

/* gdfontliu.h */
#include "gd.h"
   BGD_EXPORT_DATA_PROT gdFontPtr gdFontLiu;
   BGD_DECLARE(gdFontPtr) gdFontGetLiu(void);

/* gdfontbin.h */
#include "gd.h"
   BGD_EXPORT_DATA_PROT gdFontPtr gdFontBin;
   BGD_DECLARE(gdFontPtr) gdFontGetBin(void);

/****************************************************/

o 生成静态库的方法

$ gcc -c gdfontwu.c
$ gcc -c gdfontliu.c
$ gcc -c gdfontbin.c
$ ar rc libgdfont.a gdfontwu.o gdfontliu.o gdfontbin.o
(ranlib libgdfont.a 可生成索引)

用 nm libgdfont.a 来看里面的目标文件和导出函数(带 T 标记)。


o 生成动态库的方法

$ gcc -c gdfontwu.c
$ gcc -c gdfontliu.c
$ gcc -c gdfontbin.c
$ gcc -o libgdfont.so -shared -fPIC gdfontwu.o gdfontliu.o gdfontbin.o

-fPIC:表示编译为位置独立的代码,不用此选项的话编译后的代码
是位置相关的所以动态载入时是通过代码拷贝的方式来满足不同进程
需要,而不能达到真正代码段共享的目的。可用 nm libgdfont.so 来看
里面导出的函数(带 T 标记)。

用动态库的好处是:更新了动态库之后链结它的程序不用重新编译。


o 用法

在 websrv.c 中:

/* websrv.c */
#include "gdfontwu.h"
#include "gdfontliu.h"
#include "gdfontbin.h"

int getpng()
{
    ...
    gdImageChar(..., gdFontWu, ...);
    gdImageChar(..., gdFontLiu, ...);
    gdImageChar(..., gdFontBin, ...);
    ...
    return 0;
}

> 静态库:
$ gcc -c websrv.c
$ gcc -o websrv websrv.o libgdfont.a


> 动态库:
$ gcc -o websrv websrv.o -L. -lgdfont
$ LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH ./websrv

LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH ldd main
可以看到 websrv 程序所链结的动态库。把 libgdfont.so 放到
/etc/ld.so.conf 中列出的目录下就可以不用先指定环境变
,注意先 ldconfig 刷新系统动态库的缓存

 还有一种 ld -rpath dir 这样的方法可避免设定环境变量

本文地址:http://com.8s8s.com/it/it23544.htm