前言
GNU autotools
是一系列编程工具,可以辅助产生 Makefile
,方便编译整个工程项目。它是自由软件基金会发起的GNU计划的其中一项,作为GNU建构系统的一部分。autotools
所产生的 Makefile
符合GNU编程标准。
使用过开源 C/C++
项目的同学们都知道,标准的编译过程已经变成了简单的三部曲:configure/make/make install
, 使用起来很方便,不像平时自己写代码,要手写一堆复杂的 Makefile
,而且换个编译环境,Makefile
还需要修改。
Makefile
带来的最大好处就是 “自动化编译”,一但编辑好文件,只需要一个 make
命令,整个工程完全自动编译,极大的提高了软件开发的效率。
如果工程项目比较小,那么可以直接手动编辑该文件;不过一但工程项目比较大时,手动维护将变得极其复杂,为此,就可以使用 Autotools
或者 CMake
生成 Makefile
文件。本文主要讲解 Autotools
生成Makefile
的方法。
一、Autotools构成
首先安装autoTools
工具集,Centos下可以使用如下命令在线安装:
yum install autoconf automake libtool
autotools包含以下工具:
aclocal: perl
脚本程序,根据已经安装的宏、用户定义宏和acinclude.m4
文件中的宏将configure.ac
文件所需要的宏集中定义到文件aclocal.m4
中;autoscan: autoconf
的辅助命令,扫描源代码以搜寻普通的可移植性问题,比如检查编译器、库、头文件等,生成configure.scan
,最终生成configure.ac
;autoheader: autoconf
的辅助命令,用于生成configure
的模板头文件config.h.in;autoconf:基于 configure.ac
生成configure
,需依赖m4
;automake:基于 Makefile.am
和configure.ac
生成Makefile
,需依赖perl
。libtool:创建和使用可移植的共享(动态链接)库。
二、构建流程
使用Autotools
生成Makefile
的流程总结如下:
在源代码的顶层目录下执行
autoscan
命令生成configure.scan
文件。将
configure.scan
文件改名为configure.ac(或configure.in)
,并对其默认配置进行修改。执行
aclocal
、autoconf
、autoheader
三个命令,分别生成aclocal.m4
、configure
、config.h.in
文件。在每个目录下创建一个名为
Makefile.am
的文件,并输入相应的内容。执行
automake --add-missing
,它根据Makefile.am
文件,生成Makefile.in
。(ps 此步骤只能在aclocal
、autoconf
之后)执行
./configure
脚本文件,它根据Makefile.in
文件,生成最终的Makefile
文件。生成
Makefile
之后,执行make
编译工程并且生成可执行程序。
根据上述流程,整理autotools
自动构建流程图如下:
看到这里,你可能只是对于使用 autotools
创建 Makefile
的步骤有了一个模糊的认识,正所谓:“实践才是检验真理的唯一标准”,接下来让我们从实际的例子入手带你领略一下autotools
的精妙之处。
三、Autotools编译各类型文件
首先,创建/tmp/myproject
目录,我们用它来存放各种编译场景下的main
程序及相关依赖文件。
mkdir -p /tmp/myproject
cd /tmp/myproject
mkdir src
mkdir include
创建C源文件供后续各编译场景下使用,具体如下:
main.c
源文件:
#include<stdio.h>
#include"include/math.h"
int main()
{
int a = 10;
int b = 20;
printf("max=%d\n", max(a, b));
printf("min=%d\n", min(a, b));
}
math.h
头文件:
#pragma once
#ifndef MATH_H
#define MATH_H
int max(int x, int y);
int min(int x, int y);
#endif // !MATH_H
math.c
源文件:
int max(int x, int y)
{
return x > y ? x : y;
}
int min(int x, int y)
{
return x < y ? x : y;
}
1、编译可执行程序(未依赖任何库文件)
(1)生成 configure.scan
我们使用 autoscan
命令来帮助我们根据目录下的源代码生成一个configure.scan
文件,它可以作为进一步生成 configure
的 configure.ac
文件的模板。
[root@localhost myproject]# tree
.
├── include
│ └── math.h
└── src
├── main.c
└── math.c
2 directories, 3 files
[root@localhost myproject]# autoscan
[root@localhost myproject]# tree
.
├── autoscan.log
├── configure.scan
├── include
│ └── math.h
└── src
├── main.c
└── math.c
2 directories, 5 files
执行后在myproject
目录下会生成一个文件:configure.scan
,我们可以拿它作为configure.in
(早期使用.in后缀) 或者 configure.ac
的蓝本。
(2)修改configure.ac
当利用autoscan
工具生成confiugre.scan
文件时,configure.scan
其实是一个模板文件,我们需要将文件confiugre.scan
重命名为confiugre.ac
。confiugre.ac
调用一系列autoconf
宏来测试程序需要的或用到的特性是否存在,以及这些特性的功能。
下面我们就来目睹一下confiugre.scan
的庐山真面目:
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([src/main.c])
AC_CONFIG_HEADERS([config.h])
# 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
每个configure.scan
文件都是以AC_INIT
开头,以AC_OUTPUT
结束。我们不难从文件中看出confiugre.ac
文件的一般布局:
AC_INIT
测试程序
测试函数库
测试头文件
测试类型定义
测试结构
测试编译器特性
测试库函数
测试系统调用
AC_OUTPUT
上面的调用次序只是建议性质的,但我们还是强烈建议不要随意改变对宏调用的次序。
configure.ac 标签说明:
标签 | 说明 |
---|---|
AC_PREREQ | 确定autoconf版本,它是在AC_INIT前唯一使用的宏 |
AC_INIT | 定义软件名称,版本号,邮箱 |
AM_INIT_AUTOMAKE | 必须要的,参数为软件名和版本号 |
AC_CONFIG_SRCDIR | 该宏用来侦测所指定的源码文件是否存在,来确定源码有效性 |
AC_CONFIG_HEADER | 该宏用来生成config.h文件,以便autoheader命令使用 |
AC_PROG_CC | 指定编译器,默认GCC |
AC_CONFIG_FILE | 生成相应的Makefile文件,不同目录下通过空格分隔 |
AC_OUTPUT | 用来设定configure所要产生的文件,如果是makefile,config会把它检查出来的结果带入makefile.in文件,产生合适的makefile,如果只有一个Makefile, 也可改为AC_OUTPUT([Makefile]) |
现在开始重命名该文件:
[root@localhost myproject]# mv configure.scan configure.ac
同时修改重命名后的configure.ac
文件,修改后的结果如下:
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69])
#AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_INIT(main, 1.0, linux106587@126.com) #新增一句描述,这里main是我源程序main.c的名字
AM_INIT_AUTOMAKE(main, 1.0) #新增一句,后面aclocal用到的
AC_CONFIG_SRCDIR([src/main.c])
AC_CONFIG_HEADERS([config.h])
# 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_CONFIG_FILES([Makefile])
AC_OUTPUT
(3)生成 configure
执行命令aclocal
和autoconf
,分别会产生aclocal.m4
及configure
两个文件:
# 执行aclocal,生成文件如下所示
[root@localhost myproject]# aclocal
[root@localhost myproject]# tree
.
├── aclocal.m4
├── autom4te.cache
│ ├── output.0
│ ├── requests
│ └── traces.0
├── autoscan.log
├── configure.ac
├── include
│ └── math.h
└── src
├── main.c
└── math.c
3 directories, 9 files
# 执行autoconf,生成文件如下:
[root@localhost myproject]# autoconf
[root@localhost myproject]# tree
.
├── aclocal.m4
├── autom4te.cache
│ ├── output.0
│ ├── output.1
│ ├── requests
│ ├── traces.0
│ └── traces.1
├── autoscan.log
├── configure
├── configure.ac
├── include
│ └── math.h
└── src
├── main.c
└── math.c
3 directories, 12 files
(4)生成config.h.in
执行 autoheader
,扫描configure.ac
(configure.in
)、acconfig.h
(如果存在),生成config.h.in
宏定义文件,里面主要是根据configure.ac
中某些特定宏(如AC_DEFINE
)生成的#define
和#undefine
宏,configure
在将根据实际的探测结果决定这些宏是否定义。
[root@localhost myproject]# autoheader
[root@localhost myproject]# tree
.
├── aclocal.m4
├── autom4te.cache
│ ├── output.0
│ ├── output.1
│ ├── requests
│ ├── traces.0
│ └── traces.1
├── autoscan.log
├── config.h.in
├── configure
├── configure.ac
├── include
│ └── math.h
└── src
├── main.c
└── math.c
3 directories, 13 files
(5)创建 Makefile.am
Makefile.am
是一种比Makefile
更高层次的规则。只需指定要生成什么目标,它由什么源文件生成,要安装到什么目录等构成。
图一列出了可执行文件、静态库、头文件和数据文件,四种书写Makefile.am
文件的一般格式。
对于可执行文件和静态库类型,如果只想编译,不想安装到系统中,可以用 noinst_PROGRAMS
代替 bin_PROGRAMS
,noinst_LIBRARIES
代替 lib_LIBRARIES
。
Makefile.am
还提供了一些全局变量供所有的目标体使用:
变量 | 含义 |
---|---|
INCLUDES | 链接时所需要的头文件 |
LDADD | 链接时所需要的库文件 |
LDFLAGS | 链接时所需要的库文件选项标志 |
EXTRA_DIST | 源程序和一些默认的文件将自动打入.tar.gz包,其它文件若要进入.tar.gz包可以用这种办法,比如配置文件、数据文件等等 |
SUBDIRS | 在处理当前目录之前要递归处理指定的子目录 |
在 Makefile.am
中尽量使用相对路径,系统预定义了两个基本路径:
路径变量 | 含义 |
---|---|
$(top_srcdir) | 工程最顶层目录,用于引用源程序 |
$(top_builddir) | 定义了生成目标文件最上层目录,用于引用.o等编译出来的目标文件 |
新建Makefile.am
文件,命令:
[root@localhost myproject]# vim Makefile.am
[root@localhost myproject]# cat Makefile.am
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=main
main_SOURCES=src/main.c src/math.c
include_HEADERS=include/math.h
其中的AUTOMAKE_OPTIONS
为设置automake
的选项。由于GNU
对自己发布的软件有严格的规范,比如必须附带许可证声明文件COPYING
等,否则automake
执行时会报错。automake
提供了三种软件等级:foreign
、gnu
和gnits
,让用户选择采用,默认等级为gnu
。在本例使用foreign
等级,它只检测必须的文件。
执行automake --add-missing
会根据你写的Makefile.am
来自动生成Makefile.in
。
Makefile.am
中定义的宏和目标,会指导automake
生成指定的代码。例如,宏bin_PROGRAMS
将导致编译和连接的目标被生成。automake
会根据Makefile.am
文件产生一些文件,包含最重要的Makefile.in
。
[root@localhost myproject]# automake --add-missing
configure.ac:7: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated. For more info, see:
configure.ac:7: https://www.gnu.org/software/automake/manual/automake.html#Modernize-AM_005fINIT_005fAUTOMAKE-invocation
configure.ac:12: installing './compile'
configure.ac:7: installing './install-sh'
configure.ac:7: installing './missing'
Makefile.am:3: warning: source file 'src/main.c' is in a subdirectory,
Makefile.am:3: but option 'subdir-objects' is disabled
automake: warning: possible forward-incompatibility.
automake: At least a source file is in a subdirectory, but the 'subdir-objects'
automake: automake option hasn't been enabled. For now, the corresponding output
automake: object file(s) will be placed in the top-level directory. However,
automake: this behaviour will change in future Automake versions: they will
automake: unconditionally cause object files to be placed in the same subdirectory
automake: of the corresponding sources.
automake: You are advised to start using 'subdir-objects' option throughout your
automake: project, to avoid future incompatibilities.
Makefile.am:3: warning: source file 'src/math.c' is in a subdirectory,
Makefile.am:3: but option 'subdir-objects' is disabled
Makefile.am: installing './depcomp'
[root@localhost myproject]#
[root@localhost myproject]# tree
.
├── aclocal.m4
├── autom4te.cache
│ ├── output.0
│ ├── output.1
│ ├── requests
│ ├── traces.0
│ └── traces.1
├── autoscan.log
├── compile -> /usr/share/automake-1.16/compile
├── config.h.in
├── configure
├── configure.ac
├── depcomp -> /usr/share/automake-1.16/depcomp
├── include
│ └── math.h
├── install-sh -> /usr/share/automake-1.16/install-sh
├── Makefile.am
├── Makefile.in
├── missing -> /usr/share/automake-1.16/missing
└── src
├── main.c
└── math.c
3 directories, 19 files
(6)生成Makefile
执行 configure
脚本文件,它根据 Makefile.in
文件,生成最终的 Makefile
文件。
[root@localhost myproject]# ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking whether gcc understands -c and -o together... yes
checking whether make supports the include directive... yes (GNU style)
checking dependency style of gcc... gcc3
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: executing depfiles commands
[root@localhost myproject]# tree
.
├── aclocal.m4
├── autom4te.cache
│ ├── output.0
│ ├── output.1
│ ├── requests
│ ├── traces.0
│ └── traces.1
├── autoscan.log
├── compile -> /usr/share/automake-1.16/compile
├── config.h
├── config.h.in
├── config.log
├── config.status
├── configure
├── configure.ac
├── depcomp -> /usr/share/automake-1.16/depcomp
├── include
│ └── math.h
├── install-sh -> /usr/share/automake-1.16/install-sh
├── Makefile
├── Makefile.am
├── Makefile.in
├── missing -> /usr/share/automake-1.16/missing
├── src
│ ├── main.c
│ └── math.c
└── stamp-h1
3 directories, 24 files
(7)make编译工程
生成 Makefile
之后,执行make
编译工程并且生成可执行程序。
[root@localhost myproject]# make
make all-am
make[1]: Entering directory '/tmp/myproject'
gcc -DHAVE_CONFIG_H -I. -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o `test -f 'src/main.c' || echo './'`src/main.c
mv -f .deps/main.Tpo .deps/main.Po
gcc -DHAVE_CONFIG_H -I. -g -O2 -MT math.o -MD -MP -MF .deps/math.Tpo -c -o math.o `test -f 'src/math.c' || echo './'`src/math.c
mv -f .deps/math.Tpo .deps/math.Po
gcc -g -O2 -o main main.o math.o
make[1]: Leaving directory '/tmp/myproject'
[root@localhost myproject]#
[root@localhost myproject]# ls -l
total 352
-rw-r--r-- 1 root root 41888 Jul 28 13:21 aclocal.m4
drwxr-xr-x 2 root root 86 Jul 28 13:42 autom4te.cache
-rw-r--r-- 1 root root 0 Jul 28 13:05 autoscan.log
lrwxrwxrwx 1 root root 32 Jul 28 13:42 compile -> /usr/share/automake-1.16/compile
-rw-r--r-- 1 root root 761 Jul 28 13:44 config.h
-rw-r--r-- 1 root root 625 Jul 28 13:27 config.h.in
-rw-r--r-- 1 root root 9015 Jul 28 13:44 config.log
-rwxr-xr-x 1 root root 32345 Jul 28 13:44 config.status
-rwxr-xr-x 1 root root 145845 Jul 28 13:24 configure
-rw-r--r-- 1 root root 671 Jul 28 13:21 configure.ac
lrwxrwxrwx 1 root root 32 Jul 28 13:42 depcomp -> /usr/share/automake-1.16/depcomp
drwxr-xr-x 2 root root 20 Jul 28 10:59 include
lrwxrwxrwx 1 root root 35 Jul 28 13:42 install-sh -> /usr/share/automake-1.16/install-sh
-rwxr-xr-x 1 root root 21416 Jul 28 13:46 main
-rw-r--r-- 1 root root 6672 Jul 28 13:46 main.o
-rw-r--r-- 1 root root 28783 Jul 28 13:44 Makefile
-rw-r--r-- 1 root root 109 Jul 28 13:38 Makefile.am
-rw-r--r-- 1 root root 29860 Jul 28 13:42 Makefile.in
-rw-r--r-- 1 root root 2784 Jul 28 13:46 math.o
lrwxrwxrwx 1 root root 32 Jul 28 13:42 missing -> /usr/share/automake-1.16/missing
drwxr-xr-x 2 root root 34 Jul 28 11:17 src
-rw-r--r-- 1 root root 23 Jul 28 13:44 stamp-h1
(8)运行验证
运行生成的main
可执行程序,执行成功,结果如下:
[root@localhost myproject]# ./main
max=20
min=10
2、编译可执行程序(依赖静态库文件)
上一节中所有源文件都在同一个目录下,但在比较大的项目中,很少将所有文件放在一个目录下的。下面将针对这种情况做个简单介绍。
多级目录结构的软件,一般是单个程序、库文件或模块放在各自的目录中。automake
要求每个目录都有自己的Makefile.am
文件来编译各自目录 下的代码。在顶级的目录中,有一个Makefile.am
文件,该文件通过SUBDIRS
指明了这个目录下有多少个直接下级目录的代码需要编译。下级目录的Makefile.am
也指明自己需要编译的下级目录。通过这样的层层递归,从而完成多级目录结构的编译。
本节使用上述创建的C源文件及相关目录,并新建lib
目录,查看各目录下的结构:
[root@localhost myproject]# mkdir lib
[root@localhost myproject]# tree
.
├── include
│ └── math.h
├── lib
│ └── math.c
└── src
└── main.c
3 directories, 3 files
(1)生成configure.scan
在项目顶级目录执行autoscan
命令生成configure.scan
文件,执行命令后项目目录内生成如下文件:
[root@localhost myproject]# ls
autoscan.log configure.scan include lib src
(2)修改configure.ac
将生成的configure.scan
文件改名成configure.ac
(早期使用configure.in
现在常用configure.ac
),查看configure.ac
内容如下:
[root@localhost myproject]# mv configure.scan configure.ac
[root@localhost myproject]# cat configure.ac
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([src/main.c])
AC_CONFIG_HEADERS([config.h])
# 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
修改configure.ac
文件内容如下:
[root@localhost myproject]# vi configure.ac
[root@localhost myproject]# cat configure.ac
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69])
#AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_INIT(main, 1.0, linux106587@126.com)
AC_CONFIG_SRCDIR([src/main.c])
AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE(main, 1.0)
AC_PROG_RANLIB #使用了静态库编译,需要此宏的定义
# 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/Makefile lib/Makefile)
(3)生成 configure
然后执行命令aclocal
和autoconf
,分别会产生aclocal.m4
及configure
两个文件:
[root@localhost myproject]# ls
aclocal.m4 autom4te.cache autoscan.log configure.ac include lib src
[root@localhost myproject]# ls
aclocal.m4 autom4te.cache autoscan.log configure configure.ac include lib src
(4)生成config.h.in
执行 autoheader
,生成config.h.in
宏定义文件。
[root@localhost myproject]# ls
aclocal.m4 autom4te.cache autoscan.log config.h.in configure configure.ac include lib src
(5)创建 Makefile.am
顶级目录下创建Makefile.am
并编辑内容如下:
[root@localhost myproject]# vi Makefile.am
[root@localhost myproject]# cat Makefile.am
AUTOMAKE_OPTIONS=foreign
SUBDIRS= lib src #表示本目录的直接下级目录需要编译
#注意:顺序不能反,按照调用顺序来写。
src
目录下创建Makefile.am
文件
[root@localhost src]# cat Makefile.am
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=main #本目录的文件编译成可执行文件main
main_SOURCES=main.c
main_LDADD=$(top_srcdir)/lib/libmath.a
INCLUDES=-I$(top_srcdir)/include/
main_LDADD
指定main
程序需要的库文件INCLUDES
指定需要的头文件
lib
目录下创建Makefile.am
文件
[root@localhost lib]# cat Makefile.am
AUTOMAKE_OPTIONS=foreign
noinst_LIBRARIES=libmath.a
libmath_a_SOURCES=math.c
INCLUDES=-I$(top_srcdir)/include/
(6)生成Makefile.in
[root@localhost myproject]# automake --add-missing
configure.ac:9: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated. For more info, see:
configure.ac:9: https://www.gnu.org/software/automake/manual/automake.html#Modernize-AM_005fINIT_005fAUTOMAKE-invocation
configure.ac:13: installing './compile'
configure.ac:9: installing './install-sh'
configure.ac:9: installing './missing'
lib/Makefile.am:7: warning: 'INCLUDES' is the old name for 'AM_CPPFLAGS' (or '*_CPPFLAGS')
lib/Makefile.am: installing './depcomp'
src/Makefile.am:9: warning: 'INCLUDES' is the old name for 'AM_CPPFLAGS' (or '*_CPPFLAGS')
[root@localhost myproject]#
[root@localhost myproject]# ls
aclocal.m4 autoscan.log config.h.in configure.ac include lib Makefile.in src
autom4te.cache compile configure depcomp install-sh Makefile.am missing
(7)生成Makefile
[root@localhost myproject]# ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking for ranlib... ranlib
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking whether gcc understands -c and -o together... yes
checking whether make supports the include directive... yes (GNU style)
checking dependency style of gcc... gcc3
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating src/Makefile
config.status: creating lib/Makefile
config.status: creating config.h
config.status: executing depfiles commands
[root@localhost myproject]# ls
aclocal.m4 autoscan.log config.h config.log configure depcomp install-sh Makefile Makefile.in src
autom4te.cache compile config.h.in config.status configure.ac include lib Makefile.am missing stamp-h1
(8)编译运行
[root@localhost myproject]# make
make all-recursive
make[1]: Entering directory '/tmp/myproject'
Making all in lib
make[2]: Entering directory '/tmp/myproject/lib'
gcc -DHAVE_CONFIG_H -I. -I.. -I../include/ -g -O2 -MT math.o -MD -MP -MF .deps/math.Tpo -c -o math.o math.c
mv -f .deps/math.Tpo .deps/math.Po
rm -f libmath.a
ar cru libmath.a math.o
ranlib libmath.a
make[2]: Leaving directory '/tmp/myproject/lib'
Making all in src
make[2]: Entering directory '/tmp/myproject/src'
gcc -DHAVE_CONFIG_H -I. -I.. -I../include/ -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.c
mv -f .deps/main.Tpo .deps/main.Po
gcc -g -O2 -o main main.o ../lib/libmath.a
make[2]: Leaving directory '/tmp/myproject/src'
make[2]: Entering directory '/tmp/myproject'
make[2]: Leaving directory '/tmp/myproject'
make[1]: Leaving directory '/tmp/myproject'
[root@localhost myproject]# tree
.
├── aclocal.m4
├── autom4te.cache
│ ├── output.0
│ ├── output.1
│ ├── requests
│ ├── traces.0
│ └── traces.1
├── autoscan.log
├── compile -> /usr/share/automake-1.16/compile
├── config.h
├── config.h.in
├── config.log
├── config.status
├── configure
├── configure.ac
├── depcomp -> /usr/share/automake-1.16/depcomp
├── include
│ └── math.h
├── install-sh -> /usr/share/automake-1.16/install-sh
├── lib
│ ├── libmath.a
│ ├── Makefile
│ ├── Makefile.am
│ ├── Makefile.in
│ ├── math.c
│ └── math.o
├── Makefile
├── Makefile.am
├── Makefile.in
├── missing -> /usr/share/automake-1.16/missing
├── src
│ ├── main
│ ├── main.c
│ ├── main.o
│ ├── Makefile
│ ├── Makefile.am
│ └── Makefile.in
└── stamp-h1
4 directories, 34 files
[root@localhost src]# ./main
max=20
min=10
[root@localhost myproject]# make install
Making install in lib
make[1]: Entering directory '/tmp/myproject/lib'
make[2]: Entering directory '/tmp/myproject/lib'
make[2]: Nothing to be done for 'install-exec-am'.
make[2]: Nothing to be done for 'install-data-am'.
make[2]: Leaving directory '/tmp/myproject/lib'
make[1]: Leaving directory '/tmp/myproject/lib'
Making install in src
make[1]: Entering directory '/tmp/myproject/src'
make[2]: Entering directory '/tmp/myproject/src'
/usr/bin/mkdir -p '/usr/local/bin'
/usr/bin/install -c main '/usr/local/bin'
make[2]: Nothing to be done for 'install-data-am'.
make[2]: Leaving directory '/tmp/myproject/src'
make[1]: Leaving directory '/tmp/myproject/src'
make[1]: Entering directory '/tmp/myproject'
make[2]: Entering directory '/tmp/myproject'
make[2]: Nothing to be done for 'install-exec-am'.
make[2]: Nothing to be done for 'install-data-am'.
make[2]: Leaving directory '/tmp/myproject'
make[1]: Leaving directory '/tmp/myproject'
[root@localhost myproject]# which main
/usr/local/bin/main
[root@localhost ~]# main
max=20
min=10
3、编译可执行程序(依赖动态库文件)
本节使用上述创建的C源文件并新创建相关目录,查看各目录下的结构。
[root@localhost 85]# tree
.
├── include
│ └── math.h
├── lib
│ └── math.c
└── src
└── main.c
3 directories, 3 files
(1)生成configure.scan
在项目顶级目录执行autoscan
命令生成configure.scan
文件,执行命令后项目目录内生成如下文件:
[root@localhost 85]# autoscan
[root@localhost 85]# ls
autoscan.log configure.scan include lib src
(2)修改configure.ac
将生成的configure.scan
文件改名成configure.ac
(早期使用configure.in
现在常用configure.ac
), 修改configure.ac
文件内容如下:
[root@localhost 85]# mv configure.scan configure.ac
[root@localhost 85]# vi configure.ac
[root@localhost 85]# cat configure.ac
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69])
#AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_INIT(main, 1.0, linux106587@126.com)
AC_CONFIG_SRCDIR([src/main.c])
AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE(main, 1.0)
AC_PROG_LIBTOOL #使用了动态库编译,需要此宏的定义
# 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/Makefile lib/Makefile)
(3)生成 configure
然后执行命令aclocal
、autoheader
和autoconf
,分别会产生aclocal.m4
、config.h.in
及configure
等文件:
[root@localhost 85]# aclocal
[root@localhost 85]# ls
aclocal.m4 autom4te.cache autoscan.log configure.ac include lib src
[root@localhost 85]# autoheader
[root@localhost 85]# ls -l
total 376
-rw-r--r-- 1 root root 372261 Aug 5 04:17 aclocal.m4
drwxr-xr-x 2 root root 86 Aug 5 04:18 autom4te.cache
-rw-r--r-- 1 root root 0 Aug 5 04:15 autoscan.log
-rw-r--r-- 1 root root 1611 Aug 5 04:18 config.h.in
-rw-r--r-- 1 root root 656 Aug 5 04:17 configure.ac
drwxr-xr-x 2 root root 20 Aug 5 04:02 include
drwxr-xr-x 3 root root 52 Aug 5 04:14 lib
drwxr-xr-x 3 root root 52 Aug 5 04:15 src
[root@localhost 85]#
[root@localhost 85]# autoconf
[root@localhost 85]# ls -l
total 812
-rw-r--r-- 1 root root 372261 Aug 5 04:17 aclocal.m4
drwxr-xr-x 2 root root 86 Aug 5 04:18 autom4te.cache
-rw-r--r-- 1 root root 0 Aug 5 04:15 autoscan.log
-rw-r--r-- 1 root root 1611 Aug 5 04:18 config.h.in
-rwxr-xr-x 1 root root 446104 Aug 5 04:18 configure
-rw-r--r-- 1 root root 656 Aug 5 04:17 configure.ac
drwxr-xr-x 2 root root 20 Aug 5 04:02 include
drwxr-xr-x 3 root root 52 Aug 5 04:14 lib
drwxr-xr-x 3 root root 52 Aug 5 04:15 src
(4)libtoolize配置
执行libtoolize -f -c
命令,使用共享库必须要执行。
[root@localhost 85]# libtoolize -f -c
libtoolize: putting auxiliary files in '.'.
libtoolize: copying file './ltmain.sh'
libtoolize: Consider adding 'AC_CONFIG_MACRO_DIRS([m4])' to configure.ac,
libtoolize: and rerunning libtoolize and aclocal.
libtoolize: Consider adding '-I m4' to ACLOCAL_AMFLAGS in Makefile.am.
(5)创建 Makefile.am
顶级目录下创建Makefile.am
并编辑内容如下:
[root@localhost 85]# vi Makefile.am
[root@localhost 85]# cat Makefile.am
AUTOMAKE_OPTIONS=foreign
SUBDIRS=lib src #表示本目录的直接下级目录需要编译
#注意:顺序不能反,按照调用顺序来写。
src
目录下创建Makefile.am
文件
[root@localhost src]# cat Makefile.am
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=main #本目录的文件编译成可执行文件main
main_SOURCES=main.c
main_LDADD=$(top_srcdir)/lib/libmath.la
INCLUDES=-I$(top_srcdir)/include/
main_LDADD
指定main
程序需要的库文件INCLUDES
指定需要的头文件
lib
目录下创建Makefile.am
文件
[root@localhost lib]# cat Makefile.am
AUTOMAKE_OPTIONS=foreign
lib_LTLIBRARIES=libmath.la
libmath_la_SOURCES=math.c
INCLUDES=-I$(top_srcdir)/include/
(6)生成Makefile.in
[root@localhost 85]# automake --add-missing
configure.ac:9: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated. For more info, see:
configure.ac:9: https://www.gnu.org/software/automake/manual/automake.html#Modernize-AM_005fINIT_005fAUTOMAKE-invocation
configure.ac:10: installing './compile'
configure.ac:10: installing './config.guess'
configure.ac:10: installing './config.sub'
configure.ac:9: installing './install-sh'
configure.ac:9: installing './missing'
lib/Makefile.am:7: warning: 'INCLUDES' is the old name for 'AM_CPPFLAGS' (or '*_CPPFLAGS')
lib/Makefile.am: installing './depcomp'
src/Makefile.am:9: warning: 'INCLUDES' is the old name for 'AM_CPPFLAGS' (or '*_CPPFLAGS')
(7)生成Makefile
[root@localhost 85]# ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking how to print strings... printf
checking whether make supports the include directive... yes (GNU style)
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking whether gcc understands -c and -o together... yes
checking dependency style of gcc... gcc3
checking for a sed that does not truncate output... /usr/bin/sed
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for fgrep... /usr/bin/grep -F
checking for ld used by gcc... /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... yes
checking for BSD- or MS-compatible name lister (nm)... /usr/bin/nm -B
checking the name lister (/usr/bin/nm -B) interface... BSD nm
checking whether ln -s works... yes
checking the maximum length of command line arguments... 1572864
checking how to convert x86_64-pc-linux-gnu file names to x86_64-pc-linux-gnu format... func_convert_file_noop
checking how to convert x86_64-pc-linux-gnu file names to toolchain format... func_convert_file_noop
checking for /usr/bin/ld option to reload object files... -r
checking for objdump... objdump
checking how to recognize dependent libraries... pass_all
checking for dlltool... no
checking how to associate runtime and link libraries... printf %s\n
checking for ar... ar
checking for archiver @FILE support... @
checking for strip... strip
checking for ranlib... ranlib
checking command to parse /usr/bin/nm -B output from gcc object... ok
checking for sysroot... no
checking for a working dd... /usr/bin/dd
checking how to truncate binary pipes... /usr/bin/dd bs=4096 count=1
checking for mt... no
checking if : is a manifest tool... no
checking how to run the C preprocessor... gcc -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for dlfcn.h... yes
checking for objdir... .libs
checking if gcc supports -fno-rtti -fno-exceptions... no
checking for gcc option to produce PIC... -fPIC -DPIC
checking if gcc PIC flag -fPIC -DPIC works... yes
checking if gcc static flag -static works... no
checking if gcc supports -c -o file.o... yes
checking if gcc supports -c -o file.o... (cached) yes
checking whether the gcc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
checking whether -lc should be explicitly linked in... no
checking dynamic linker characteristics... GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... yes
checking for gcc... (cached) gcc
checking whether we are using the GNU C compiler... (cached) yes
checking whether gcc accepts -g... (cached) yes
checking for gcc option to accept ISO C89... (cached) none needed
checking whether gcc understands -c and -o together... (cached) yes
checking dependency style of gcc... (cached) gcc3
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating src/Makefile
config.status: creating lib/Makefile
config.status: creating config.h
config.status: executing depfiles commands
config.status: executing libtool commands
[root@localhost 85]#
[root@nj-localhost]# ls -l
total 1728
-rw-r--r-- 1 root root 372261 Aug 5 04:17 aclocal.m4
drwxr-xr-x 2 root root 86 Aug 5 04:19 autom4te.cache
-rw-r--r-- 1 root root 0 Aug 5 04:15 autoscan.log
lrwxrwxrwx 1 root root 32 Aug 5 04:19 compile -> /usr/share/automake-1.16/compile
lrwxrwxrwx 1 root root 37 Aug 5 04:19 config.guess -> /usr/share/automake-1.16/config.guess
-rw-r--r-- 1 root root 1790 Aug 5 04:19 config.h
-rw-r--r-- 1 root root 1611 Aug 5 04:18 config.h.in
-rw-r--r-- 1 root root 23404 Aug 5 04:19 config.log
-rwxr-xr-x 1 root root 57443 Aug 5 04:19 config.status
lrwxrwxrwx 1 root root 35 Aug 5 04:19 config.sub -> /usr/share/automake-1.16/config.sub
-rwxr-xr-x 1 root root 446104 Aug 5 04:18 configure
-rw-r--r-- 1 root root 656 Aug 5 04:17 configure.ac
lrwxrwxrwx 1 root root 32 Aug 5 04:19 depcomp -> /usr/share/automake-1.16/depcomp
drwxr-xr-x 2 root root 20 Aug 5 04:02 include
lrwxrwxrwx 1 root root 35 Aug 5 04:19 install-sh -> /usr/share/automake-1.16/install-sh
drwxr-xr-x 3 root root 87 Aug 5 04:19 lib
-rwxr-xr-x 1 root root 339146 Aug 5 04:19 libtool
-rw-r--r-- 1 root root 324152 Aug 5 04:18 ltmain.sh
-rw-r--r-- 1 root root 25193 Aug 5 04:19 Makefile
-rw-r--r-- 1 root root 150 Aug 5 04:15 Makefile.am
-rw-r--r-- 1 root root 25308 Aug 5 04:19 Makefile.in
lrwxrwxrwx 1 root root 32 Aug 5 04:19 missing -> /usr/share/automake-1.16/missing
drwxr-xr-x 3 root root 87 Aug 5 04:19 src
-rw-r--r-- 1 root root 23 Aug 5 04:19 stamp-h1
(8)编译安装
[root@localhost 85]# make
make all-recursive
make[1]: Entering directory '/tmp/85'
Making all in lib
make[2]: Entering directory '/tmp/85/lib'
/bin/sh ../libtool --tag=CC --mode=compile gcc -DHAVE_CONFIG_H -I. -I.. -I../include/ -g -O2 -MT math.lo -MD -MP -MF .deps/math.Tpo -c -o math.lo math.c
libtool: compile: gcc -DHAVE_CONFIG_H -I. -I.. -I../include/ -g -O2 -MT math.lo -MD -MP -MF .deps/math.Tpo -c math.c -fPIC -DPIC -o .libs/math.o
libtool: compile: gcc -DHAVE_CONFIG_H -I. -I.. -I../include/ -g -O2 -MT math.lo -MD -MP -MF .deps/math.Tpo -c math.c -o math.o >/dev/null 2>&1
mv -f .deps/math.Tpo .deps/math.Plo
/bin/sh ../libtool --tag=CC --mode=link gcc -g -O2 -o libmath.la -rpath /usr/local/lib math.lo
libtool: link: gcc -shared -fPIC -DPIC .libs/math.o -g -O2 -Wl,-soname -Wl,libmath.so.0 -o .libs/libmath.so.0.0.0
libtool: link: (cd ".libs" && rm -f "libmath.so.0" && ln -s "libmath.so.0.0.0" "libmath.so.0")
libtool: link: (cd ".libs" && rm -f "libmath.so" && ln -s "libmath.so.0.0.0" "libmath.so")
libtool: link: ar cru .libs/libmath.a math.o
libtool: link: ranlib .libs/libmath.a
libtool: link: ( cd ".libs" && rm -f "libmath.la" && ln -s "../libmath.la" "libmath.la" )
make[2]: Leaving directory '/tmp/85/lib'
Making all in src
make[2]: Entering directory '/tmp/85/src'
gcc -DHAVE_CONFIG_H -I. -I.. -I../include/ -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.c
mv -f .deps/main.Tpo .deps/main.Po
/bin/sh ../libtool --tag=CC --mode=link gcc -g -O2 -o main main.o ../lib/libmath.la
libtool: link: gcc -g -O2 -o .libs/main main.o ../lib/.libs/libmath.so -Wl,-rpath -Wl,/usr/local/lib
make[2]: Leaving directory '/tmp/85/src'
make[2]: Entering directory '/tmp/85'
make[2]: Leaving directory '/tmp/85'
make[1]: Leaving directory '/tmp/85'
[root@localhost 85]#
[root@localhost 85]# tree
.
├── aclocal.m4
├── autom4te.cache
│ ├── output.0
│ ├── output.1
│ ├── requests
│ ├── traces.0
│ └── traces.1
├── autoscan.log
├── compile -> /usr/share/automake-1.16/compile
├── config.guess -> /usr/share/automake-1.16/config.guess
├── config.h
├── config.h.in
├── config.log
├── config.status
├── config.sub -> /usr/share/automake-1.16/config.sub
├── configure
├── configure.ac
├── depcomp -> /usr/share/automake-1.16/depcomp
├── include
│ └── math.h
├── install-sh -> /usr/share/automake-1.16/install-sh
├── lib
│ ├── libmath.la
│ ├── Makefile
│ ├── Makefile.am
│ ├── Makefile.in
│ ├── math.c
│ ├── math.lo
│ └── math.o
├── libtool
├── ltmain.sh
├── Makefile
├── Makefile.am
├── Makefile.in
├── missing -> /usr/share/automake-1.16/missing
├── src
│ ├── main
│ ├── main.c
│ ├── main.o
│ ├── Makefile
│ ├── Makefile.am
│ └── Makefile.in
└── stamp-h1
4 directories, 39 files
[root@localhost src]# ./main
max=20
min=10
[root@localhost 85]# make install
Making install in lib
make[1]: Entering directory '/tmp/85/lib'
make[2]: Entering directory '/tmp/85/lib'
/usr/bin/mkdir -p '/usr/local/lib'
/bin/sh ../libtool --mode=install /usr/bin/install -c libmath.la '/usr/local/lib'
libtool: install: /usr/bin/install -c .libs/libmath.so.0.0.0 /usr/local/lib/libmath.so.0.0.0
libtool: install: (cd /usr/local/lib && { ln -s -f libmath.so.0.0.0 libmath.so.0 || { rm -f libmath.so.0 && ln -s libmath.so.0.0.0 libmath.so.0; }; })
libtool: install: (cd /usr/local/lib && { ln -s -f libmath.so.0.0.0 libmath.so || { rm -f libmath.so && ln -s libmath.so.0.0.0 libmath.so; }; })
libtool: install: /usr/bin/install -c .libs/libmath.lai /usr/local/lib/libmath.la
libtool: install: /usr/bin/install -c .libs/libmath.a /usr/local/lib/libmath.a
libtool: install: chmod 644 /usr/local/lib/libmath.a
libtool: install: ranlib /usr/local/lib/libmath.a
libtool: finish: PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/netronome/bin:/root/bin:/sbin" ldconfig -n /usr/local/lib
----------------------------------------------------------------------
Libraries have been installed in:
/usr/local/lib
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the '-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the 'LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the 'LD_RUN_PATH' environment variable
during linking
- use the '-Wl,-rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to '/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
make[2]: Nothing to be done for 'install-data-am'.
make[2]: Leaving directory '/tmp/85/lib'
make[1]: Leaving directory '/tmp/85/lib'
Making install in src
make[1]: Entering directory '/tmp/85/src'
make[2]: Entering directory '/tmp/85/src'
/usr/bin/mkdir -p '/usr/local/bin'
/bin/sh ../libtool --mode=install /usr/bin/install -c main '/usr/local/bin'
libtool: install: /usr/bin/install -c .libs/main /usr/local/bin/main
make[2]: Nothing to be done for 'install-data-am'.
make[2]: Leaving directory '/tmp/85/src'
make[1]: Leaving directory '/tmp/85/src'
make[1]: Entering directory '/tmp/85'
make[2]: Entering directory '/tmp/85'
make[2]: Nothing to be done for 'install-exec-am'.
make[2]: Nothing to be done for 'install-data-am'.
make[2]: Leaving directory '/tmp/85'
make[1]: Leaving directory '/tmp/85'
[root@localhost 85]#
[root@localhost 85]# which main
/usr/local/bin/main
[root@localhost 85]# ls -l /usr/local/bin/main
-rwxr-xr-x 1 root root 20968 Aug 5 10:33 /usr/local/bin/main
[root@localhost 85]# ls -l /usr/local/lib/ | grep libmath.la
-rwxr-xr-x 1 root root 916 Aug 5 10:33 libmath.la
如果没有指定安装路径,make install
会将可执行程序以及库文件安装到默认路径下,可执行文件默认放在/usr/local/bin/
,库文件默认放在/usr/local/lib
,配置文件默认放在/usr/local/etc
,其它的资源文件放在/usr/local/share
,比较分散。如果不想安装在默认路径,而想自行指定安装路径,则可以通过执行configure
时指定--prefix
,这里--prefix
选项表示配置安装的路径。为了便于集中管理某个软件的各种文件,可以配置如:./configure --prefix=/usr/local
,则会把所有资源文件放在/usr/local
的路径中,就不会分散了。
[root@localhost 85]# make clean
Making clean in lib
make[1]: Entering directory '/tmp/85/lib'
test -z "libmath.la" || rm -f libmath.la
rm -f ./so_locations
rm -rf .libs _libs
rm -f *.o
rm -f *.lo
make[1]: Leaving directory '/tmp/85/lib'
Making clean in src
make[1]: Entering directory '/tmp/85/src'
rm -f main
rm -rf .libs _libs
rm -f *.o
rm -f *.lo
make[1]: Leaving directory '/tmp/85/src'
make[1]: Entering directory '/tmp/85'
rm -rf .libs _libs
rm -f *.lo
make[1]: Leaving directory '/tmp/85'
[root@localhost 85]# tree
.
├── aclocal.m4
├── autom4te.cache
│ ├── output.0
│ ├── output.1
│ ├── requests
│ ├── traces.0
│ └── traces.1
├── autoscan.log
├── compile -> /usr/share/automake-1.16/compile
├── config.guess -> /usr/share/automake-1.16/config.guess
├── config.h
├── config.h.in
├── config.log
├── config.status
├── config.sub -> /usr/share/automake-1.16/config.sub
├── configure
├── configure.ac
├── depcomp -> /usr/share/automake-1.16/depcomp
├── include
│ └── math.h
├── install-sh -> /usr/share/automake-1.16/install-sh
├── lib
│ ├── Makefile
│ ├── Makefile.am
│ ├── Makefile.in
│ └── math.c
├── libtool
├── ltmain.sh
├── Makefile
├── Makefile.am
├── Makefile.in
├── missing -> /usr/share/automake-1.16/missing
├── src
│ ├── main.c
│ ├── Makefile
│ ├── Makefile.am
│ └── Makefile.in
└── stamp-h1
4 directories, 34 files
[root@localhost 85]# ./configure --prefix=$PWD/_install
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking how to print strings... printf
checking whether make supports the include directive... yes (GNU style)
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking whether gcc understands -c and -o together... yes
checking dependency style of gcc... gcc3
checking for a sed that does not truncate output... /usr/bin/sed
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for fgrep... /usr/bin/grep -F
checking for ld used by gcc... /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... yes
checking for BSD- or MS-compatible name lister (nm)... /usr/bin/nm -B
checking the name lister (/usr/bin/nm -B) interface... BSD nm
checking whether ln -s works... yes
checking the maximum length of command line arguments... 1572864
checking how to convert x86_64-pc-linux-gnu file names to x86_64-pc-linux-gnu format... func_convert_file_noop
checking how to convert x86_64-pc-linux-gnu file names to toolchain format... func_convert_file_noop
checking for /usr/bin/ld option to reload object files... -r
checking for objdump... objdump
checking how to recognize dependent libraries... pass_all
checking for dlltool... no
checking how to associate runtime and link libraries... printf %s\n
checking for ar... ar
checking for archiver @FILE support... @
checking for strip... strip
checking for ranlib... ranlib
checking command to parse /usr/bin/nm -B output from gcc object... ok
checking for sysroot... no
checking for a working dd... /usr/bin/dd
checking how to truncate binary pipes... /usr/bin/dd bs=4096 count=1
checking for mt... no
checking if : is a manifest tool... no
checking how to run the C preprocessor... gcc -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for dlfcn.h... yes
checking for objdir... .libs
checking if gcc supports -fno-rtti -fno-exceptions... no
checking for gcc option to produce PIC... -fPIC -DPIC
checking if gcc PIC flag -fPIC -DPIC works... yes
checking if gcc static flag -static works... no
checking if gcc supports -c -o file.o... yes
checking if gcc supports -c -o file.o... (cached) yes
checking whether the gcc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
checking whether -lc should be explicitly linked in... no
checking dynamic linker characteristics... GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... yes
checking for gcc... (cached) gcc
checking whether we are using the GNU C compiler... (cached) yes
checking whether gcc accepts -g... (cached) yes
checking for gcc option to accept ISO C89... (cached) none needed
checking whether gcc understands -c and -o together... (cached) yes
checking dependency style of gcc... (cached) gcc3
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating src/Makefile
config.status: creating lib/Makefile
config.status: creating config.h
config.status: config.h is unchanged
config.status: executing depfiles commands
config.status: executing libtool commands
[root@localhost 85]# ls -l
total 1728
-rw-r--r-- 1 root root 372261 Aug 5 04:17 aclocal.m4
drwxr-xr-x 2 root root 86 Aug 5 04:19 autom4te.cache
-rw-r--r-- 1 root root 0 Aug 5 04:15 autoscan.log
lrwxrwxrwx 1 root root 32 Aug 5 04:19 compile -> /usr/share/automake-1.16/compile
lrwxrwxrwx 1 root root 37 Aug 5 04:19 config.guess -> /usr/share/automake-1.16/config.guess
-rw-r--r-- 1 root root 1790 Aug 5 04:19 config.h
-rw-r--r-- 1 root root 1611 Aug 5 04:18 config.h.in
-rw-r--r-- 1 root root 23522 Aug 5 04:22 config.log
-rwxr-xr-x 1 root root 57504 Aug 5 04:22 config.status
lrwxrwxrwx 1 root root 35 Aug 5 04:19 config.sub -> /usr/share/automake-1.16/config.sub
-rwxr-xr-x 1 root root 446104 Aug 5 04:18 configure
-rw-r--r-- 1 root root 656 Aug 5 04:17 configure.ac
lrwxrwxrwx 1 root root 32 Aug 5 04:19 depcomp -> /usr/share/automake-1.16/depcomp
drwxr-xr-x 2 root root 20 Aug 5 04:02 include
lrwxrwxrwx 1 root root 35 Aug 5 04:19 install-sh -> /usr/share/automake-1.16/install-sh
drwxr-xr-x 3 root root 87 Aug 5 04:22 lib
-rwxr-xr-x 1 root root 339146 Aug 5 04:22 libtool
-rw-r--r-- 1 root root 324152 Aug 5 04:18 ltmain.sh
-rw-r--r-- 1 root root 25199 Aug 5 04:22 Makefile
-rw-r--r-- 1 root root 150 Aug 5 04:15 Makefile.am
-rw-r--r-- 1 root root 25308 Aug 5 04:19 Makefile.in
lrwxrwxrwx 1 root root 32 Aug 5 04:19 missing -> /usr/share/automake-1.16/missing
drwxr-xr-x 3 root root 87 Aug 5 04:22 src
-rw-r--r-- 1 root root 23 Aug 5 04:22 stamp-h1
[root@localhost 85]# make
make all-recursive
make[1]: Entering directory '/tmp/85'
Making all in lib
make[2]: Entering directory '/tmp/85/lib'
/bin/sh ../libtool --tag=CC --mode=compile gcc -DHAVE_CONFIG_H -I. -I.. -I../include/ -g -O2 -MT math.lo -MD -MP -MF .deps/math.Tpo -c -o math.lo math.c
libtool: compile: gcc -DHAVE_CONFIG_H -I. -I.. -I../include/ -g -O2 -MT math.lo -MD -MP -MF .deps/math.Tpo -c math.c -fPIC -DPIC -o .libs/math.o
libtool: compile: gcc -DHAVE_CONFIG_H -I. -I.. -I../include/ -g -O2 -MT math.lo -MD -MP -MF .deps/math.Tpo -c math.c -o math.o >/dev/null 2>&1
mv -f .deps/math.Tpo .deps/math.Plo
/bin/sh ../libtool --tag=CC --mode=link gcc -g -O2 -o libmath.la -rpath /tmp/85/_install/lib math.lo
libtool: link: gcc -shared -fPIC -DPIC .libs/math.o -g -O2 -Wl,-soname -Wl,libmath.so.0 -o .libs/libmath.so.0.0.0
libtool: link: (cd ".libs" && rm -f "libmath.so.0" && ln -s "libmath.so.0.0.0" "libmath.so.0")
libtool: link: (cd ".libs" && rm -f "libmath.so" && ln -s "libmath.so.0.0.0" "libmath.so")
libtool: link: ar cru .libs/libmath.a math.o
libtool: link: ranlib .libs/libmath.a
libtool: link: ( cd ".libs" && rm -f "libmath.la" && ln -s "../libmath.la" "libmath.la" )
make[2]: Leaving directory '/tmp/85/lib'
Making all in src
make[2]: Entering directory '/tmp/85/src'
gcc -DHAVE_CONFIG_H -I. -I.. -I../include/ -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.c
mv -f .deps/main.Tpo .deps/main.Po
/bin/sh ../libtool --tag=CC --mode=link gcc -g -O2 -o main main.o ../lib/libmath.la
libtool: link: gcc -g -O2 -o .libs/main main.o ../lib/.libs/libmath.so -Wl,-rpath -Wl,/tmp/85/_install/lib
make[2]: Leaving directory '/tmp/85/src'
make[2]: Entering directory '/tmp/85'
make[2]: Leaving directory '/tmp/85'
make[1]: Leaving directory '/tmp/85'
[root@localhost 85]# ls -l
total 1728
-rw-r--r-- 1 root root 372261 Aug 5 04:17 aclocal.m4
drwxr-xr-x 2 root root 86 Aug 5 04:19 autom4te.cache
-rw-r--r-- 1 root root 0 Aug 5 04:15 autoscan.log
lrwxrwxrwx 1 root root 32 Aug 5 04:19 compile -> /usr/share/automake-1.16/compile
lrwxrwxrwx 1 root root 37 Aug 5 04:19 config.guess -> /usr/share/automake-1.16/config.guess
-rw-r--r-- 1 root root 1790 Aug 5 04:19 config.h
-rw-r--r-- 1 root root 1611 Aug 5 04:18 config.h.in
-rw-r--r-- 1 root root 23522 Aug 5 04:22 config.log
-rwxr-xr-x 1 root root 57504 Aug 5 04:22 config.status
lrwxrwxrwx 1 root root 35 Aug 5 04:19 config.sub -> /usr/share/automake-1.16/config.sub
-rwxr-xr-x 1 root root 446104 Aug 5 04:18 configure
-rw-r--r-- 1 root root 656 Aug 5 04:17 configure.ac
lrwxrwxrwx 1 root root 32 Aug 5 04:19 depcomp -> /usr/share/automake-1.16/depcomp
drwxr-xr-x 2 root root 20 Aug 5 04:02 include
lrwxrwxrwx 1 root root 35 Aug 5 04:19 install-sh -> /usr/share/automake-1.16/install-sh
drwxr-xr-x 4 root root 147 Aug 5 04:22 lib
-rwxr-xr-x 1 root root 339146 Aug 5 04:22 libtool
-rw-r--r-- 1 root root 324152 Aug 5 04:18 ltmain.sh
-rw-r--r-- 1 root root 25199 Aug 5 04:22 Makefile
-rw-r--r-- 1 root root 150 Aug 5 04:15 Makefile.am
-rw-r--r-- 1 root root 25308 Aug 5 04:19 Makefile.in
lrwxrwxrwx 1 root root 32 Aug 5 04:19 missing -> /usr/share/automake-1.16/missing
drwxr-xr-x 4 root root 126 Aug 5 04:22 src
-rw-r--r-- 1 root root 23 Aug 5 04:22 stamp-h1
[root@localhost 85]# make install
Making install in lib
make[1]: Entering directory '/tmp/85/lib'
make[2]: Entering directory '/tmp/85/lib'
/usr/bin/mkdir -p '/tmp/85/_install/lib'
/bin/sh ../libtool --mode=install /usr/bin/install -c libmath.la '/tmp/85/_install/lib'
libtool: install: /usr/bin/install -c .libs/libmath.so.0.0.0 /tmp/85/_install/lib/libmath.so.0.0.0
libtool: install: (cd /tmp/85/_install/lib && { ln -s -f libmath.so.0.0.0 libmath.so.0 || { rm -f libmath.so.0 && ln -s libmath.so.0.0.0 libmath.so.0; }; })
libtool: install: (cd /tmp/85/_install/lib && { ln -s -f libmath.so.0.0.0 libmath.so || { rm -f libmath.so && ln -s libmath.so.0.0.0 libmath.so; }; })
libtool: install: /usr/bin/install -c .libs/libmath.lai /tmp/85/_install/lib/libmath.la
libtool: install: /usr/bin/install -c .libs/libmath.a /tmp/85/_install/lib/libmath.a
libtool: install: chmod 644 /tmp/85/_install/lib/libmath.a
libtool: install: ranlib /tmp/85/_install/lib/libmath.a
libtool: finish: PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/netronome/bin:/root/bin:/sbin" ldconfig -n /tmp/85/_install/lib
----------------------------------------------------------------------
Libraries have been installed in:
/tmp/85/_install/lib
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the '-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the 'LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the 'LD_RUN_PATH' environment variable
during linking
- use the '-Wl,-rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to '/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
make[2]: Nothing to be done for 'install-data-am'.
make[2]: Leaving directory '/tmp/85/lib'
make[1]: Leaving directory '/tmp/85/lib'
Making install in src
make[1]: Entering directory '/tmp/85/src'
make[2]: Entering directory '/tmp/85/src'
/usr/bin/mkdir -p '/tmp/85/_install/bin'
/bin/sh ../libtool --mode=install /usr/bin/install -c main '/tmp/85/_install/bin'
libtool: install: /usr/bin/install -c .libs/main /tmp/85/_install/bin/main
make[2]: Nothing to be done for 'install-data-am'.
make[2]: Leaving directory '/tmp/85/src'
make[1]: Leaving directory '/tmp/85/src'
make[1]: Entering directory '/tmp/85'
make[2]: Entering directory '/tmp/85'
make[2]: Nothing to be done for 'install-exec-am'.
make[2]: Nothing to be done for 'install-data-am'.
make[2]: Leaving directory '/tmp/85'
make[1]: Leaving directory '/tmp/85'
[root@localhost 85]# which main
/usr/local/bin/main
[root@localhost 85]# ls -l /usr/local/bin/main
-rwxr-xr-x 1 root root 21424 Aug 3 04:16 /usr/local/bin/main
[root@localhost 85]# ls
aclocal.m4 compile config.h.in config.sub depcomp install-sh ltmain.sh Makefile.in stamp-h1
autom4te.cache config.guess config.log configure include lib Makefile missing
autoscan.log config.h config.status configure.ac _install libtool Makefile.am src
[root@localhost 85]# cd _install/
[root@localhost _install]# tree .
.
├── bin
│ └── main
└── lib
├── libmath.a
├── libmath.la
├── libmath.so -> libmath.so.0.0.0
├── libmath.so.0 -> libmath.so.0.0.0
└── libmath.so.0.0.0
2 directories, 6 files
[root@localhost 85]# ls
aclocal.m4 compile config.h.in config.sub depcomp install-sh ltmain.sh Makefile.in stamp-h1
autom4te.cache config.guess config.log configure include lib Makefile missing
autoscan.log config.h config.status configure.ac _install libtool Makefile.am src
这里我们通过--prefix
选项将可执行文件和库文件安装到自定义路径_install
。通过make install
编译安装过后,我们发现在_install
路径下确实含有编译生成的可执行程序以及动态库文件。
另外,在确保可执行程序运行正常后,我们也可以通过命令make dist
将项目源文件整体打包发布,极其方便。
[root@localhost 85]# make dist
make dist-gzip am__post_remove_distdir='@:'
make[1]: Entering directory '/tmp/85'
make distdir-am
make[2]: Entering directory '/tmp/85'
if test -d "main-1.0"; then find "main-1.0" -type d ! -perm -200 -exec chmod u+w {} ';' && rm -rf "main-1.0" || { sleep 5 && rm -rf "main-1.0"; }; else :; fi
test -d "main-1.0" || mkdir "main-1.0"
(cd lib && make top_distdir=../main-1.0 distdir=../main-1.0/lib \
am__remove_distdir=: am__skip_length_check=: am__skip_mode_fix=: distdir)
make[3]: Entering directory '/tmp/85/lib'
make distdir-am
make[4]: Entering directory '/tmp/85/lib'
make[4]: Leaving directory '/tmp/85/lib'
make[3]: Leaving directory '/tmp/85/lib'
(cd src && make top_distdir=../main-1.0 distdir=../main-1.0/src \
am__remove_distdir=: am__skip_length_check=: am__skip_mode_fix=: distdir)
make[3]: Entering directory '/tmp/85/src'
make distdir-am
make[4]: Entering directory '/tmp/85/src'
make[4]: Leaving directory '/tmp/85/src'
make[3]: Leaving directory '/tmp/85/src'
test -n "" \
|| find "main-1.0" -type d ! -perm -755 \
-exec chmod u+rwx,go+rx {} \; -o \
! -type d ! -perm -444 -links 1 -exec chmod a+r {} \; -o \
! -type d ! -perm -400 -exec chmod a+r {} \; -o \
! -type d ! -perm -444 -exec /bin/sh /tmp/85/install-sh -c -m a+r {} {} \; \
|| chmod -R a+r "main-1.0"
make[2]: Leaving directory '/tmp/85'
tardir=main-1.0 && ${TAR-tar} chof - "$tardir" | eval GZIP= gzip --best -c >main-1.0.tar.gz
make[1]: Leaving directory '/tmp/85'
if test -d "main-1.0"; then find "main-1.0" -type d ! -perm -200 -exec chmod u+w {} ';' && rm -rf "main-1.0" || { sleep 5 && rm -rf "main-1.0"; }; else :; fi
[root@localhost 85]# ls -l
total 1936
-rw-r--r-- 1 root root 372261 Aug 5 04:17 aclocal.m4
drwxr-xr-x 2 root root 86 Aug 5 04:19 autom4te.cache
-rw-r--r-- 1 root root 0 Aug 5 04:15 autoscan.log
lrwxrwxrwx 1 root root 32 Aug 5 04:19 compile -> /usr/share/automake-1.16/compile
lrwxrwxrwx 1 root root 37 Aug 5 04:19 config.guess -> /usr/share/automake-1.16/config.guess
-rw-r--r-- 1 root root 1790 Aug 5 04:19 config.h
-rw-r--r-- 1 root root 1611 Aug 5 04:18 config.h.in
-rw-r--r-- 1 root root 23522 Aug 5 10:48 config.log
-rwxr-xr-x 1 root root 57504 Aug 5 10:48 config.status
lrwxrwxrwx 1 root root 35 Aug 5 04:19 config.sub -> /usr/share/automake-1.16/config.sub
-rwxr-xr-x 1 root root 446104 Aug 5 04:18 configure
-rw-r--r-- 1 root root 656 Aug 5 04:17 configure.ac
lrwxrwxrwx 1 root root 32 Aug 5 04:19 depcomp -> /usr/share/automake-1.16/depcomp
drwxr-xr-x 2 root root 20 Aug 5 04:02 include
drwxr-xr-x 4 root root 28 Aug 5 04:25 _install
lrwxrwxrwx 1 root root 35 Aug 5 04:19 install-sh -> /usr/share/automake-1.16/install-sh
drwxr-xr-x 4 root root 147 Aug 5 10:49 lib
-rwxr-xr-x 1 root root 339146 Aug 5 10:48 libtool
-rw-r--r-- 1 root root 324152 Aug 5 04:18 ltmain.sh
-rw-r--r-- 1 root root 331481 Aug 5 13:03 main-1.0.tar.gz
-rw-r--r-- 1 root root 25199 Aug 5 10:48 Makefile
-rw-r--r-- 1 root root 150 Aug 5 04:15 Makefile.am
-rw-r--r-- 1 root root 25308 Aug 5 04:19 Makefile.in
lrwxrwxrwx 1 root root 32 Aug 5 04:19 missing -> /usr/share/automake-1.16/missing
drwxr-xr-x 4 root root 126 Aug 5 10:49 src
-rw-r--r-- 1 root root 23 Aug 5 10:48 stamp-h1
[root@nj-localhost]# cp main-1.0.tar.gz ../
[root@nj-raclocalhostcd ..
[root@nj-rack02localhostr -zxvf main-1.0.tar.gz
main-1.0/
main-1.0/Makefile.am
main-1.0/configure
main-1.0/configure.ac
main-1.0/aclocal.m4
main-1.0/Makefile.in
main-1.0/config.h.in
main-1.0/compile
main-1.0/config.guess
main-1.0/config.sub
main-1.0/install-sh
main-1.0/ltmain.sh
main-1.0/missing
main-1.0/lib/
main-1.0/lib/Makefile.am
main-1.0/lib/Makefile.in
main-1.0/lib/math.c
main-1.0/depcomp
main-1.0/src/
main-1.0/src/Makefile.am
main-1.0/src/Makefile.in
main-1.0/src/main.c
[root@localhostlocalhost
total 384
drwxr-xr-x 2 root root 20 Jul 28 09:01 717
drwxr-xr-x 2 root root 23 Jul 28 09:01 722
drwxr-xr-x 5 root root 4096 Jul 28 08:49 724
drwxr-xr-x 2 root root 6 Jul 17 13:36 77
drwxr-xr-x 8 root root 4096 Aug 5 13:03 85
drwxr-xr-x 6 root root 137 Aug 1 14:57 example
-rw-r--r-- 1 root root 12510 Jul 28 03:53 gcc_micro
drwxr-xr-x 4 root root 4096 Aug 5 13:03 main-1.0
-rw-r--r-- 1 root root 331481 Aug 5 13:03 main-1.0.tar.gz
-rw-r--r-- 1 root root 252 Jul 24 13:43 Makefile
drwxr-xr-x 7 root root 4096 Aug 5 04:02 myproject
drwx------ 3 root root 17 Jun 8 15:34 systemd-private-ef1fd2fdd12d442c8b6a8d3d0cdc1c54-chronyd.service-BS2qcS
-rwxr-xr-x 1 root root 18096 Jul 27 02:48 test
-rw-r--r-- 1 root root 162 Jul 27 02:48 test.c
[root@nj-localhostlocalhost1.0/
[root@nj-raclocalhostlocalhostocal.m4 config.guess config.sub configure.ac install-sh ltmain.sh Makefile.in src
compile config.h.in configure depcomp lib Makefile.am missing
[root@nj-rack02localhostlocalhost─ aclocal.m4
├── compile
├── config.guess
├── config.h.in
├── config.sub
├── configure
├── configure.ac
├── depcomp
├── install-sh
├── lib
│ ├── Makefile.am
│ ├── Makefile.in
│ └── math.c
├── ltmain.sh
├── Makefile.am
├── Makefile.in
├── missing
└── src
├── main.c
├── Makefile.am
└── Makefile.in
2 directories, 19 files
四、总结
本文从Autotools
工具的构成、构建工程项目流程、以及实战构建各类程序三个方面,简单的介绍了Autotools
工具的原理和使用方法,为我们在日常学习和工作中构建复杂的工程项目提供一定的参考,姑且作为抛砖引玉之作。但是由于篇幅所限,无法在一篇文章中将Autotools
工具的方方面面都详细的总结出来,后续小编会陆续推出针对Autotools
工具的一些细节内容总结的推文,敬请期待。
end
一口Linux
关注,回复【1024】海量Linux资料赠送
精彩文章合集
文章推荐