多目录项目用automake和autoconf自动生成Makefile方法

在Linux系统下编写代码,基本上都会遇到Makefile的使用,虽说它对于工程管理有很方便的作用,但对初学者来说要编写出自己的Makefile却不那么容易。网上虽然有很多相关的文章,但大多都用一个helloword.c作为示例,而一半工程项目都不可能只要一个.c文件,多目录项目的autotools使用与一个.c文件还是有些区别的。下面记录一下多目录文件自动生成Makefile的方法:

1. 建立多目录项目

为模拟多目录项目,我们建立如下的目录结构:

/* 项目目录结构 */
automake |-- include |-- src.h        // src目录下用的头文件|           |-- src1.h       // src1目录下用的头文件||-- src |-- add.c|       |-- sub.c||-- src1 |-- mul.c|-- div.c

目录结构的截图:

各文件的的代码内容:

/* src.h */
#ifndef __SRC_H__
#define __SRC_H__
int add(int num1, int num2);
int sub(int num1, int num2);
#endif/* src1.h */
#ifndef __SRC1_H__
#define __SRC1_H__
int mul(int num1, int num2);
int my_div(int num1, int num2);
#endif/* add.c */
#include <stdio.h>
#include <stdlib.h>
#include "src.h"
int add(int num1, int num2)
{printf("this is add func...\n");return (num1 + num2);
}/* sub.c */
#include <stdio.h>
#include <stdlib.h>
#include "src.h"
int sub(int num1, int num2)
{printf("this is sub func...\n");return (num1 - num2);
}/* mul.c */
#include <stdio.h>
#include <stdlib.h>
#include "src1.h"
int mul(int num1, int num2)
{printf("this is mul func...\n");return (num1 * num2);
}/* div.c */
#include <stdio.h>
#include <stdlib.h>
#include "src1.h"
int my_div(int num1, int num2)
{printf("this is div func...\n");return (num1 / num2);
}/* main.c */
#include <stdio.h>
#include <stdlib.h>
#include "src.h"
#include "src1.h"int main(int argc, char **argv)
{printf("main func start..\n");int num1 = 6;int num2 = 3;printf("%d\n", add(num1, num2));printf("%d\n", sub(num1, num2));printf("%d\n", mul(num1, num2));printf("%d\n", my_div(num1, num2));printf("main func end...\n");
}

2. 使用autotools工具生成Makefile文件

1、执行autoscan

configure.ac文件是automake的输入文件,有些文章说是configure.in文件,这可能是旧版tools的输入,新版会提示输入文件应为configure.ac。直接执行autoscan会生成configure.scan(如果提示未安装automake,根据提示安装即可),然后将configure.acan重命名为configure.ac即可。执行命令:

/automake$ autoscan
/automake$ ls
autoscan.log  configure.scan include  main.c  src  src1
/automake$ mv configure.scan configure.ac
/automake$ ls
autoscan.log  configure.ac  include  main.c  src  src1

2、修改configure.ac

使用autoscan命令生成的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([main.c])
AC_CONFIG_HEADER([config.h])# Checks for programs.
AC_PROG_CC# Checks for libraries.# Checks for header files.
AC_CHECK_HEADERS([stdlib.h])# Checks for typedefs, structures, and compiler characteristics.# Checks for library functions.AC_OUTPUT

将configure.ac修改为:

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.AC_PREREQ(2.69)
# FULL-PACKAGE-NAME 代表输出的程序名称
# VERSION 代表版本号
# BUG-REPORT-ADDRESS 代表bug的汇报地址,这里随便填个邮箱
# AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_INIT([Main], [0.0.1], [12345@qq.com])
AM_INIT_AUTOMAKE    # 这是automake必须的,需要自己加上
AC_CONFIG_SRCDIR([main.c])    # 这里main.c可以是项目中任意文件,用来确定该文件是否存在
AC_CONFIG_HEADER([config.h])# Checks for programs.
AC_PROG_CC# Checks for libraries.# Checks for header files.
AC_CHECK_HEADERS([stdlib.h])# Checks for typedefs, structures, and compiler characteristics.# Checks for library functions.
AC_CONFIG_FILES([Makefile])    # 这一行表示要产生Makefile这个文件,需自己加上AC_OUTPUT

3、执行aclocal

通过aclocal命令会生成aclocal.m4和autom4te.cache文件

/automake$ aclocal
/automake$ ls
aclocal.m4  autom4te.cache  autoscan.log  configure.ac  include  main.c  src  src1
/automake$

4、制作Makefile.am

Makefile.am文件本身没有,需要自己创建,使用touch命令创建,然后vim编辑输入以下内容。automake这个命令需要用到这个配置文件,用于指定文件路径,头文件目录,输出程序名和指定命令等

# 设置automake的选项,在执行sutomake时会检查目录下是否存在标准GNU软件中应具备的文件,如NEWS
# AUTHOR ChangLog等文件。设置为foreign时,automake会改用一般标准来检查,此时不需要这些文件。
# AUTOMAKE_OPTIONS = foreign# 输出的可执行文件名称
bin_PROGRAMS = Main# Main所需要的源码文件
Main_SOURCES = include/src.h\include/src1.h\src/add.c\src/sub.c\src1/mul.c\src1/div.c\main.c# 头文件目录
Main_CPPFLAGS = -I include/# 一般用于指明本项目自己生成的库,如果使用math.h等的话需要用
Main_LDADD = -lm# 一般使用-l等指明本项目依赖的第三方库
# Main_LDFLAGS = -l

说明:automake会根据configure.ac中的宏并在perl的帮助下把Makefile.am转成Makefile.in文件,Makefile.am文件定义所要产生的目标,下表是网上找到可执行文件,静态库,头文件和数据文件四种书写Makefile.am文件的一般规则:

文件类型 书写格式
可执行文件

bin_PROGRAMS = foo

foo_SOURCE = xxx.c

foo_LDADD = -lm

foo_LDFLAGS = -l

foo_DEPENDENCIES =

静态库

lib_LIBRARIES = libfoo.a

foo_a_SOURCES =

foo_a_LDADD =

foo_a_LIBADD =

foo_a_LDFLAGS =

头文件

include_HEADERS = foo.h

数据文件

data_DATA = data1 data2

下表是上表中全局变量的含义:

变量 含义
INCLUDES 链接时所需要的头文件
LDADD 链接时所需要的库文件
LDFLAGS

连接时所需要的库文件选项标志

EXTRA_DIST

源程序和一些默认的文件将自动打入.tar.gz包,其它

文件若要进入.tar.gz包可以用这种办法,比如配置文

件,数据文件等。

SUBDIRS 在处理本目录之前要递归处理哪些子目录

在编写Makefile.am文件时,尽可能的用相对路径,要不在不同的地方编译、安装可能目录就不同了:

路径变量 含义
$(top_srcdir) 工程最顶层目录,用于引用源程序
$(top_builddir)

定义了生成目标文件上最上层目录,

用于引用.o等编译出来的目标文件

5、执行autoheader

执行autoheader命令会自动生成config.h.in文件

/automake$ ls
Makefile.am  autom4te.cache  configure.ac  main.c  src1
aclocal.m4   autoscan.log    include       src
/automake$ autoheader
/automake$ ls
Makefile.am  autom4te.cache  config.h.in   include  src
aclocal.m4   autoscan.log    configure.ac  main.c   src1

6、创建automake必须的文件

标准automake必须的文件包括:

*  install-sh
* missing
* INSTALL
* NEWS
* README
* AUTHORS
* ChangeLog
* COPYING
* depcomp 

其中需要我们手动创建的文件有:NEWS README AUTHORS ChangeLog,其它文件automake会自动创建。

/automake$ ls
Makefile.am  aclocal.m4  autom4te.cache  autoscan.log  config.h.in  configure.ac  include  main.c  src  src1
/automake$ touch NEWS README AUTHORS ChangeLog
/automake$ ls
AUTHORS    Makefile.am  README      autom4te.cache  config.h.in   include  src
ChangeLog  NEWS         aclocal.m4  autoscan.log    configure.ac  main.c   src1

7、执行automake -a

/automake$ automake -a
configure.ac:15: installing './compile'
configure.ac:10: installing './install-sh'
configure.ac:10: installing './missing'
Makefile.am: installing './INSTALL'
Makefile.am: installing './COPYING' using GNU General Public License v3 file
Makefile.am:     Consider adding the COPYING file to the version control system
Makefile.am:     for your code, to avoid questions about which license your project uses
Makefile.am:2: warning: source file 'src/add.c' is in a subdirectory,
Makefile.am:2: 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:2: warning: source file 'src/sub.c' is in a subdirectory,
Makefile.am:2: but option 'subdir-objects' is disabled
Makefile.am:2: warning: source file 'src1/mul.c' is in a subdirectory,
Makefile.am:2: but option 'subdir-objects' is disabled
Makefile.am:2: warning: source file 'src1/div.c' is in a subdirectory,
Makefile.am:2: but option 'subdir-objects' is disabled
Makefile.am: installing './depcomp'
/automake$ ls
AUTHORS    INSTALL      NEWS        autom4te.cache  config.h.in   include     missing
COPYING    Makefile.am  README      autoscan.log    configure.ac  install-sh  src
ChangeLog  Makefile.in  aclocal.m4  compile         depcomp       main.c      src1

8、执行autoconf

autoconf会生成configure可执行文件

/automake$ autoconf
/automake$ ls
AUTHORS    INSTALL      NEWS        autom4te.cache  config.h.in   depcomp     main.c   src1
COPYING    Makefile.am  README      autoscan.log    configure     include     missing
ChangeLog  Makefile.in  aclocal.m4  compile         configure.ac  install-sh  src

9、执行configure

/automake$ ./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... /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 for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -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 stdlib.h... (cached) yes
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
/automake$ ls
AUTHORS    INSTALL      Makefile.in  aclocal.m4      compile      config.log     configure.ac  install-sh  src
COPYING    Makefile     NEWS         autom4te.cache  config.h     config.status  depcomp       main.c      src1
ChangeLog  Makefile.am  README       autoscan.log    config.h.in  configure      include       missing     stamp-h1

至此就已经完成Makefile文件的生成,接下来就是编译,运行,安装了

10、编译(make)

通过编译命令make即可对项目进行编译,编译成功之后会生成Main可执行文件,使用./Main直接看到运行解过:

/automake$ make
make  all-am
make[1]: Entering directory '/mnt/f/workspase/automake'
gcc -DHAVE_CONFIG_H -I.  -I include/   -g -O2 -MT Main-add.o -MD -MP -MF .deps/Main-add.Tpo -c -o Main-add.o `test -f 'src/add.c' || echo './'`src/add.c
mv -f .deps/Main-add.Tpo .deps/Main-add.Po
gcc -DHAVE_CONFIG_H -I.  -I include/   -g -O2 -MT Main-sub.o -MD -MP -MF .deps/Main-sub.Tpo -c -o Main-sub.o `test -f 'src/sub.c' || echo './'`src/sub.c
mv -f .deps/Main-sub.Tpo .deps/Main-sub.Po
gcc -DHAVE_CONFIG_H -I.  -I include/   -g -O2 -MT Main-mul.o -MD -MP -MF .deps/Main-mul.Tpo -c -o Main-mul.o `test -f 'src1/mul.c' || echo './'`src1/mul.c
mv -f .deps/Main-mul.Tpo .deps/Main-mul.Po
gcc -DHAVE_CONFIG_H -I.  -I include/   -g -O2 -MT Main-div.o -MD -MP -MF .deps/Main-div.Tpo -c -o Main-div.o `test -f 'src1/div.c' || echo './'`src1/div.c
mv -f .deps/Main-div.Tpo .deps/Main-div.Po
gcc -DHAVE_CONFIG_H -I.  -I include/   -g -O2 -MT Main-main.o -MD -MP -MF .deps/Main-main.Tpo -c -o Main-main.o `test -f 'main.c' || echo './'`main.c
mv -f .deps/Main-main.Tpo .deps/Main-main.Po
gcc  -g -O2   -o Main Main-add.o Main-sub.o Main-mul.o Main-div.o Main-main.o -lm
make[1]: Leaving directory '/mnt/f/workspase/automake'
/automake$ ls
AUTHORS    Main         Main-mul.o   Makefile.in  autom4te.cache  config.h.in    configure.ac  main.c   stamp-h1
COPYING    Main-add.o   Main-sub.o   NEWS         autoscan.log    config.log     depcomp       missing
ChangeLog  Main-div.o   Makefile     README       compile         config.status  include       src
INSTALL    Main-main.o  Makefile.am  aclocal.m4   config.h        configure      install-sh    src1
/automake$ ./Main
main func start..
this is add func...
9
this is sub func...
3
this is mul func...
18
this is div func...
2
main func end...

11、打包,安装

执行main install即可直接将程序安装到系统里,然后在终端直接输入Main即可运行程序了。注意:有时需要sudo权限才能安装。

/automake$ sudo make install
[sudo] password for xxx:
make[1]: Entering directory '/mnt/f/workspase/automake'/bin/mkdir -p '/usr/local/bin'/usr/bin/install -c Main '/usr/local/bin'
make[1]: Nothing to be done for 'install-data-am'.
make[1]: Leaving directory '/mnt/f/workspase/automake'
/automake$ Main
main func start..
this is add func...
9
this is sub func...
3
this is mul func...
18
this is div func...
2
main func end...
/automake$

打包给别人使用时,执行命令:make dist,它就会自动生成main-0.0.1.tar.gz

/automake$ make dist
make  dist-gzip am__post_remove_distdir='@:'
make[1]: Entering directory '/mnt/f/workspase/automake'
if test -d "main-0.0.1"; then find "main-0.0.1" -type d ! -perm -200 -exec chmod u+w {} ';' && rm -rf "main-0.0.1" || { sleep 5 && rm -rf "main-0.0.1"; }; else :; fi
test -d "main-0.0.1" || mkdir "main-0.0.1"
test -n "" \
|| find "main-0.0.1" -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/bash /mnt/f/workspase/automake/install-sh -c -m a+r {} {} \; \
|| chmod -R a+r "main-0.0.1"
tardir=main-0.0.1 && ${TAR-tar} chof - "$tardir" | eval GZIP= gzip --best -c >main-0.0.1.tar.gz
make[1]: Leaving directory '/mnt/f/workspase/automake'
if test -d "main-0.0.1"; then find "main-0.0.1" -type d ! -perm -200 -exec chmod u+w {} ';' && rm -rf "main-0.0.1" || { sleep 5 && rm -rf "main-0.0.1"; }; else :; fi
/automake$ ls
AUTHORS    Main-add.o   Makefile     aclocal.m4      config.h.in    depcomp            missing
COPYING    Main-div.o   Makefile.am  autom4te.cache  config.log     include            src
ChangeLog  Main-main.o  Makefile.in  autoscan.log    config.status  install-sh         src1
INSTALL    Main-mul.o   NEWS         compile         configure      main-0.0.1.tar.gz  stamp-h1
Main       Main-sub.o   README       config.h        configure.ac   main.c

别人拿到tar.gz压缩包后进行解压,解压后执行./configure,然后执行make即可生成Main可执行文件,

/automake/test$ ls
main-0.0.1.tar.gz
/automake/test$ tar -zxvf main-0.0.1.tar.gz
main-0.0.1/
main-0.0.1/aclocal.m4
main-0.0.1/AUTHORS
main-0.0.1/ChangeLog
main-0.0.1/compile
main-0.0.1/config.h.in
main-0.0.1/configure
main-0.0.1/configure.ac
main-0.0.1/COPYING
main-0.0.1/depcomp
main-0.0.1/include/
main-0.0.1/include/src.h
main-0.0.1/include/src1.h
main-0.0.1/INSTALL
main-0.0.1/install-sh
main-0.0.1/main.c
main-0.0.1/Makefile.am
main-0.0.1/Makefile.in
main-0.0.1/missing
main-0.0.1/NEWS
main-0.0.1/README
main-0.0.1/src/
main-0.0.1/src/add.c
main-0.0.1/src/sub.c
main-0.0.1/src1/
main-0.0.1/src1/div.c
main-0.0.1/src1/mul.c
/automake/test$ ls
main-0.0.1  main-0.0.1.tar.gz
/automake/test$ cd main-0.0.1/
/automake/test/main-0.0.1$ ls
AUTHORS  ChangeLog  Makefile.am  NEWS    aclocal.m4  config.h.in  configure.ac  include     main.c   src
COPYING  INSTALL    Makefile.in  README  compile     configure    depcomp       install-sh  missing  src1
/automake/test/main-0.0.1$ ./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... /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 for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -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 stdlib.h... (cached) yes
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
/automake/test/main-0.0.1$ make
make  all-am
make[1]: Entering directory '/mnt/f/workspase/automake/test/main-0.0.1'
gcc -DHAVE_CONFIG_H -I.  -I include/   -g -O2 -MT Main-add.o -MD -MP -MF .deps/Main-add.Tpo -c -o Main-add.o `test -f 'src/add.c' || echo './'`src/add.c
mv -f .deps/Main-add.Tpo .deps/Main-add.Po
gcc -DHAVE_CONFIG_H -I.  -I include/   -g -O2 -MT Main-sub.o -MD -MP -MF .deps/Main-sub.Tpo -c -o Main-sub.o `test -f 'src/sub.c' || echo './'`src/sub.c
mv -f .deps/Main-sub.Tpo .deps/Main-sub.Po
gcc -DHAVE_CONFIG_H -I.  -I include/   -g -O2 -MT Main-mul.o -MD -MP -MF .deps/Main-mul.Tpo -c -o Main-mul.o `test -f 'src1/mul.c' || echo './'`src1/mul.c
mv -f .deps/Main-mul.Tpo .deps/Main-mul.Po
gcc -DHAVE_CONFIG_H -I.  -I include/   -g -O2 -MT Main-div.o -MD -MP -MF .deps/Main-div.Tpo -c -o Main-div.o `test -f 'src1/div.c' || echo './'`src1/div.c
mv -f .deps/Main-div.Tpo .deps/Main-div.Po
gcc -DHAVE_CONFIG_H -I.  -I include/   -g -O2 -MT Main-main.o -MD -MP -MF .deps/Main-main.Tpo -c -o Main-main.o `test -f 'main.c' || echo './'`main.c
mv -f .deps/Main-main.Tpo .deps/Main-main.Po
gcc  -g -O2   -o Main Main-add.o Main-sub.o Main-mul.o Main-div.o Main-main.o -lm
make[1]: Leaving directory '/mnt/f/workspase/automake/test/main-0.0.1'
/automake/test/main-0.0.1$ ls
AUTHORS    Main         Main-mul.o   Makefile.in  compile      config.status  include     src
COPYING    Main-add.o   Main-sub.o   NEWS         config.h     configure      install-sh  src1
ChangeLog  Main-div.o   Makefile     README       config.h.in  configure.ac   main.c      stamp-h1
INSTALL    Main-main.o  Makefile.am  aclocal.m4   config.log   depcomp        missing
/automake/test/main-0.0.1$ ./Main
main func start..
this is add func...
9
this is sub func...
3
this is mul func...
18
this is div func...
2
main func end...
/automake/test/main-0.0.1$

3、编译静态库并连接

要使项目编译出静态库.a并在make项目时自动链接,需要修改的文件主要有:根目录下的configure.ac,Makefile.am以及子目录下的Makefile.am,修改完成后再执行几个命令即可实现。

1. 在子目录建立Makefile.am

在src目录下建立Makefile.am文件,内容为:

# src/Makefile.am
# 生成的库名称,添加noinst表示不安装到系统
noinst_LIBRARIES=libsrc.a
# lib_LIBRARIES=libsrc.a 则会把它安装到系统中
# 库所依赖的源文件
libsrc_a_SOURCES=../include/src.h\add.c\sub.c
# 库所依赖的头文件目录
libsrc_a_CPPFLAGS = -I ../include/

在src1目录下建立Makefile.am文件,内容为:

# src1/Makefile.am
# 生成的库名称,添加noinst表示不安装到系统
noinst_LIBRARIES=libsrc1.a
# lib_LIBRARIES=libsrc1.a 表示会安装到系统中
# 库所依赖的源文件
libsrc1_a_SOURCES=../include/src1.h\mul.c\div.c
# 库所依赖的头文件目录
libsrc1_a_CPPFLAGS = -I ../include/

2. 修改跟目录下的Makefile.am和configure.ac

# Makefile.am
# 先扫描子目录
SUBDIRS=src\src1
# 生成的目标文件名称
bin_PROGRAMS = Main
# 这里源文件可以不再包含src和src1路径下的.c源文件
Main_SOURCES = include/src.h\include/src1.h\main.c
Main_CPPFLAGS = -I include/
# 添加库路径,注意:似乎库名称必须要lib开头
Main_LDADD = src/libsrc.a\src1/libsrc1.a\-lm
# configure.ac
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.AC_PREREQ([2.69])
# FULL-PACKAGE-NAME 代表输出项目的名称
# VERSION 代表项目的版本号
# BUG-REPORT-ADDRESS  代表bug报告地址,我这边随便写个邮箱
# AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_INIT([Main], [0.0.1], [12345@qq.com])
AM_INIT_AUTOMAKE    # 这是自动生成Makefile必须的,需要自己加上
AC_PROG_RANLIB      # 生成静态库.a
#AC_PROG_LIBTOOL        # 生成动态库.o
AC_CONFIG_SRCDIR([main.c])  # 这里main.c可以项目中任意文件,用来确定项目中指定文件是否存在
AC_CONFIG_HEADERS([config.h])# Checks for programs.
AC_PROG_CC# Checks for libraries.# Checks for header files.
AC_CHECK_HEADERS([stdlib.h])# Checks for typedefs, structures, and compiler characteristics.# 注意,要使automake -a命令在对应文件夹下生成Makefile.in,必须将所在目录添加到AC_CONFIG_FILES里面
AC_CONFIG_FILES([Makefile src/Makefile src1/Makefile])  # 指定输出的文件为MakefileAC_OUTPUT

3. 执行命令:

  1. aclocal
  2. autoconf
  3. autoheader
  4. automake -a 或者automake --add-missing      # 这条命令用于在有Makefile.am的目录下生成Makefile.in
  5. ./configure     # 用于生成Makefile文件
  6. make     # 编译

至此即可将子目录打包为静态库使用,整体来说还是比较方便的,最后生成静态库不确定是否需要将所有命令重新执行,但为保险起见将它都执行了一遍。除了autoscan命令,这个命令用于扫描工作目录,并生成configure.scan文件。最终生成子目录的文件为:

4、总结一下autotools使用流程

  1. 执行autoscan命令,用于扫描工作目录,生成configure.scan文件
  2. 重命名configure.scan为configure.ac文件,并修改配置内容
  3. 执行aclocal命令,用于扫描configure.ac文件并生成aclocal.m4文件
  4. 执行autoconf命令,用于将configure.ac文件中的宏展开,并生成configure脚本
  5. 执行autoheader命令,生成config.h.in文件
  6. 新建Makefile.am文件,并修改配置内容
  7. 执行automake -a或automake --add-missing命令,生成Makefile.in文件
  8. 执行./configure命令,用于根据Makefile.in生成Makefile文件
  9. 执行make命令,生成可执行文件
  10. 执行make dist命令,将项目打包(该命令只保留跟项目有关的文件,不在makefile包含内的文件会被忽略),生成.tar.gz文件
  11. 执行make install命令,将项目安装到系统。

至此,autotools工具的简单应用就记录在这里,当然该工具还有很多其它功能,如指定第三方库,指定安装时的安装目录等。另外,如何将生成的库放到指定目录也没测试出可用方法,这些后面再琢磨,其它功能也可以再去百度。

Makefile文件自动生成,ubuntu系统autotools使用相关推荐

  1. linux makefile文件怎么生成,Makefile文件生成

    所必须的软件:autoconf (generate configuration script [configuration]) automake( aclocal:automatically gene ...

  2. linux设备文件生成,Linux设备文件自动生成(示例代码)

    第一种是使用mknod手工创建:# mknod 第二种是自动创建设备节点:利用udev(mdev)来实现设备文件的自动创建,首先应保证支持udev(mdev),由busybox配置. 具体udev相关 ...

  3. 使用maven根据JSON文件自动生成Java POJO类(Java Bean)源文件

    根据JSON文件自动生成Java POJO类(Java Bean)源文件 本文介绍使用程序jsonschema2pojo来自动生成Java的POJO类源文件,本文主要使用maven,其他构建工具请参考 ...

  4. PNG字幕文件自动生成工具

    最近在PR中做视频需要加字幕,虽然有很多字幕工具,但是个人不喜欢用. 用PS做大段的文字又太费时,所以写了一个字幕文件自动生成工具,其实就是自动批量输出透明的PNG文件.

  5. 文件夹文件自动生成目录的方法-保存到txt

    文件夹文件自动生成目录的方法-保存到txt 1.打开记事本: 2.复制以下内容: @echo off     dir /b /on >list.txt 3.另存为bat,类型ANSI

  6. 【Matlab专题】-01-Matlab使用脚本导入SWC Arxml文件自动生成Simulink模型以及生成C代码

    附:"Matlab导入SWC Arxml文件自动生成Simulink模型"脚本!!! 目录 1 在DaVinci Development创建SWC.Runnable.Port 2 ...

  7. 用ECS做HexMap:自动生成地图系统

    基于Unity2019最新ECS架构开发MMO游戏笔记16 自动生成地图系统 AutoCreateMapSystem 神奇的六边形 六边形实体 创建者和创建六边形单元系统 更新计划 作者的话 ECS系 ...

  8. 【Latex】Latex小白入门(2)——如何用.bib文件自动生成论文Reference

    写在前面: 在研究生阶段搞学术的童鞋们很有可能会接触到Latex这种论文格式编辑工具,一般在论文投稿的时候,大多数期刊和会议会给一个Latex模板,要求将你的论文用Latex编辑成.pdf版本.这里的 ...

  9. lisp自动生成界址点表_基于AutoCAD VBA增减挂钩报备坐标文件自动生成.doc

    基于AutoCAD VBA增减挂钩报备坐标文件自动生成 基于AutoCAD VBA增减挂钩报备坐标文件自动生成 摘要:生成增减挂钩报备坐标文件是一项非常繁琐的工作,会占用大量工作时间.如果利用VBA对 ...

最新文章

  1. Teched 2010
  2. eclipse开发项目关于内存是如何分配的
  3. 算法:删除数组中的重复项
  4. 多元线性回归分析问题
  5. 分布式机器学习框架:CXXNet
  6. C++(23)--多态性与虚函数
  7. Sentinel底层LongAdder的计数实现
  8. 【华为云技术分享】如何用交互式特征工程工具进行数据分析处理
  9. 使用echarts(一) 第一次使用echarts
  10. Python/Java程序员面试必备常用问题解析与答案
  11. Qt——P27 QListWidget控件
  12. java获取inputstream_Java:我怎样才能从inputStream获取编码?
  13. python三国演义人物 统计分析_使用python统计《三国演义》小说里人物出现次数前十名,并实现可视化。...
  14. vue父子传值,slot插槽的使用
  15. Python_03序列、函数
  16. NLPer福利-中文语言理解基准测【CLUEbenchmark】
  17. Visual Studio Code(VSCode) 编辑/编译/调试 C++ 代码
  18. Mockito的简单使用
  19. Vue自定义指令的妙用
  20. 递归经典问题:迷宫以及八皇后

热门文章

  1. 第9章 maven的插件和生命周期
  2. 在Word中为标题样式添加自动编号功能
  3. C++判断字符串是否所有字符全都不同
  4. 电脑不能正常连接网络常见问题解决方法
  5. bert 使用(3)
  6. 深入浅出-网络七层模型以及libcurl的使用博客地址
  7. 单片机8位抢答器实训机电报告_16路抢答器单片机实训报告.docx
  8. Windows电脑上最好的3个小说阅读器
  9. 适用于所有人的Nextjs-具有一些React的基础知识
  10. MAC下安装GDAL库