使用Automake 创建和使用静态库
1. 目录结构如下:
[c-sharp] view plaincopy
example
|——src 目录(存放源代码文件)
|——hello.c
|——lib 目录(存放用来生成库的文件)
|——test.c 用来生成静态库libhello.a
|——include 目录(存放程序中使用的头文件)
|——hello.h
2. 编写的各个目录下的源文件
[c-sharp] view plaincopy
hello.h 文件
extern void print(char *);
test.c 文件
#include<stdio.h>
void print(char *msg)
{
print(“%s/n”, msg);
}
hello.c 文件
#include “hello.h”
int main()
{
print(“Hello static library!”);//这里用到的是静态库中的函数
return 0;
}
- 编写lib/Makefile.am 文件
[c-sharp] view plain copy
noinst_LIBRARIES=libhello.a
libhello_a_SOURCES=test.c
AUTOMAKE_OPTIONS=foreign
第一行noinst 表示生成的是静态库,不需要make install ,直接制定它的位置和名字就
可以使用。
第二行表示用来生成静态库的源文件。如果要把静态库生成到其他地方,可以在=后面
加上路径(建议用绝对路径,并将所要用到的静态库生成在同一个文件夹下,如lib)。
第三行AUTOMAKE_OPTIONS 是Automake 的选项。Automake 主要是帮助开发 GNU 软
件的人员来维护软件,所以在执行Automake 时,会检查目录下是否存在标准 GNU 软件中
应具备的文件,例如 ‘NEWS’、’AUTHOR’、 ‘ChangeLog’ 等文件。设置为foreign 时,Automake
会改用一般软件的标准来检查。如果不加这句的话,需要在autoconf之前,先执行touch NEWS
README AUTHORS ChangeLog 来生成’NEWS’、’AUTHOR’、 ‘ChangeLog’ 等文件
- 编写src/Makefile.am 文件
[c-sharp] view plain copy
AUTOMAKE_OPTIONS=foreign
INCLUDES= -I../include
bin_PROGRAMS=hello
hello_SOURCES=hello.c
hello_LDADD=../lib/libhello.a
第二行指定头文件的位置,-I 是idirafter 的缩写。../include 指定头文件的位置,..是上
一级目录,也就是这里的example 目录。
第三行指定生成可执行文件名hello,在这里可执行文件生成在src 下,建议将可执行文
件生成到一个特定的文件夹下,让它和源代码分开,如/root/test 目录下。写法为:
[c-sharp] view plain copy
bin_PROGRAMS=/root/test/hello,后面的第四、五行也相对应地变为:
roottest_hello_SOURCES=hello.c
roottest_hello_LDADD=../lib/libhello.a
第四行指定生成可执行文件hello 的源代码文件,如果hello.c 在其他目录下,需要加上
完整的路径。
第五行指定需要使用静态库的位置。
- 生成静态库文件lib/libhello.a。
执行autoscan 生成configure.scan 文件,将它重命名为configure.in 并修改其内容。
[c-sharp] view plain copy
#configure.inProcess this file with autoconf to produce a configure script.
AC_PREREQ(2.59)
AC_INIT(libhello.a,1.1,[])
AM_INIT_AUTOMAKEChecks for programs.
AC_PROG_CCChecks for libraries.
AC_PROG_RANLIB//需要加入的内容,因为使用了静态库Checks for header files.
Checks for typedefs, structures, and compiler characteristics.
Checks for library functions.
AC_OUTPUT([Makefile])
AC_INIT(FILE)
该宏用来检查源代码所在路径,autoscan 会自动产生,一般无须修改它。
AM_INIT_AUTOMAKE(PACKAGE,VERSION)
这个是使用 Automake 所必备的宏,PACKAGE 是所要产生软件的名称,VERSION 是版
本编号。也可以把包和版本号等信息放在AC_INIT(FILE) 宏里。
AC_PROG_CC
检查系统可用的C 编译器,若源代码是用C 写的就需要这个宏。
AC_OUTPUT(FILE)
设置 configure 所要产生的文件,若是Makefile ,configure 便会把它检查出来的结果
填充到Makefile.in 文件后产生合适的 Makefile。 后面的FILE 是一个Makefile 的输出列表,
你可以选着将要输出的Makefile 的位置和个数。建议只在src 中输出Makefile。
在lib 目录下依次执行 aclocal 、autoconf、automake –add-missing、./configure、make,
此时在该目录下就可以看到生成的静态库文件libhello.a
在src 目录下,执行autoscan 生成configure.scan 文件,将它重命名为configure.in 并修
改其内容。
[c-sharp] view plain copy
#configure.inProcess this file with autoconf to produce a configure script.
AC_PREREQ(2.59)
AC_INIT(hello,1.1,[])
AM_INIT_AUTOMAKE
AC_CONFIG_SRCDIR([hello.c])Checks for programs.
AC_PROG_CC
Checks for libraries.
Checks for header files.
Checks for typedefs, structures, and compiler characteristics.
Checks for library functions.
AC_OUTPUT([Makefile])
在src 目录下依次执行 aclocal 、autoconf、automake –add-missing、./configure、make,
生成可执行文件hello执行make install 进行安装,最后输入hello 来运行程序,查看效果:
Hello static library!
执行成功!
使用gcc 创建和使用静态库编写mylib.h 文件
[c-sharp] view plain copy
#ifndef mylibh_
#define mylibh_
void welcome();
void outstring(const char * str);
#endif编写mylib.c 文件,用来生成静态库。
[c-sharp] view plain copy
#include <stdio.h>
void welcome()
{
printf(“welcome to libmylib/n”);
}
void outstring(const char * str)
{
if(str!=NULL)
printf(“%s”,str);
}编译源文件,产生目标代码
gcc –o mylib.o –c mylib.c将上面产生的目标文件加入到静态库中,并把静态库拷贝到系统默认的路径
[c-sharp] view plain copy
ar rcs libmylib.a mylib.o
cp libmylib.a /usr/lib/编写测试程序来使用刚才创建的静态库 libmylib.a
[c-sharp] view plain copy
#include “mylib.h”
#include <stdio.h>
Int main()
{
printf(“create and use library:/n”);
welcome();
outstring(“It’s a successful/n”);
}编译使用库函数的程序
[c-sharp] view plain copy
gcc –o test test.c -lmylib
运行./test 查看结果。