一样新的东西的由来,往往因为之前的东西用的很不爽,然后更具弊端创造出更加容易使用的东西,比如在大型的项目中,如Linux,他是通过Makefile的方式进行编译。但是,Makefile复杂的语法结构,难于让人领会。在一个很大的项目中,维护Makefile是一个非常头疼的烦恼事。于是一个新的产物Autotools就出来了,他用来生成复杂的Makefile,很大程度降低了我们的开发难度。
Autotools并不是单独一个工具,而是一系列工具:
autotools提供的这一系列工具,最终的目的就是生成Makefile,这个Makefile是自动生成的。
首先我们创建一个测试代码main.c,通过简单的例子来说明autotools的使用流程,源文件内容如下:
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("Rice Autotools!!!\n");
return 0;
}
rice@rice:~/rice-autotools$ autoscan
Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/\${ <-- HERE [^\}]*}/ at /usr/bin/autoscan line 361.
rice@rice:~/rice-autotools$ ls
autoscan.log configure.scan main.c
rice@rice:~/rice-autotools$ mv configure.scan configure.ac
rice@rice:~/rice-autotools$ ls
autoscan.log configure.scan main.c
rice@rice:~/rice-autotools$
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_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
我们将其修改为下面的内容:
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69])
AC_INIT(rice, 1.0, 980307037@qq.com)
AM_INIT_AUTOMAKE(rice, 1.0)
AC_CONFIG_SRCDIR([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.
# Checks for library functions.
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
其中configure.ac的标签说明:
标签 | 说明 |
---|---|
AC_PREREQ | 声明autoconf要求的版本号 |
AC_INIT | 定义软件名称、版本号、联系方式 |
AM_INIT_AUTOMAKE | 必须要的,参数为软件名称和版本号 |
AC_CONFIG_SCRDIR | 宏用来侦测所指定的源码文件是否存在, 来确定源码目录的有效性.。此处为当前目录下main.c。 |
AC_CONFIG_HEADER | 宏用于生成config.h文件,以便 autoheader 命令使用。 |
AC_PROG_CC | 指定编译器,默认GCC |
AC_CONFIG_FILES | 生成相应的Makefile文件,不同文件夹下的Makefile通过空格分隔。例如:AC_CONFIG_FILES([Makefile, src/Makefile]) |
AC_OUTPUT | 用来设定 configure 所要产生的文件,如果是makefile,configure 会把它检查出来的结果带入makefile.in文件产生合适的makefile。 |
rice@rice:~/rice-autotools$ aclocal
rice@rice:~/rice-autotools$ ls
aclocal.m4 autom4te.cache autoscan.log configure.ac main.c
rice@rice:~/rice-autotools$
rice@rice:~/rice-autotools$ autoconf
rice@rice:~/rice-autotools$ ls
aclocal.m4 autom4te.cache autoscan.log configure configure.ac main.c
rice@rice:~/rice-autotools$
rice@rice:~/rice-autotools$ autoheader
rice@rice:~/rice-autotools$ ls
aclocal.m4 autom4te.cache autoscan.log config.h.in configure configure.ac main.c
rice@rice:~/rice-autotools$
Makefile.am文件内容:
AUTOMARK_OPTIONS = foreign
bin_PROGRAMS = rice
rice_SOURCES = main.c
rice@rice:~/rice-autotools$ automake --add-missing
configure.ac:6: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated. For more info, see:
configure.ac:6: http://www.gnu.org/software/automake/manual/automake.html#Modernize-AM_005fINIT_005fAUTOMAKE-invocation
configure.ac:11: installing './compile'
configure.ac:6: installing './install-sh'
configure.ac:6: installing './missing'
Makefile.am: installing './INSTALL'
Makefile.am: error: required file './NEWS' not found
Makefile.am: error: required file './README' not found
Makefile.am: error: required file './AUTHORS' not found
Makefile.am: error: required file './ChangeLog' not found
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: installing './depcomp'
rice@rice:~/rice-autotools$ touch NEWS
rice@rice:~/rice-autotools$ touch README
rice@rice:~/rice-autotools$ touch AUTHORS
rice@rice:~/rice-autotools$ touch ChangeLog
rice@rice:~/rice-autotools$ ls
aclocal.m4 autom4te.cache ChangeLog config.h.in configure.ac depcomp install-sh Makefile.am NEWS
AUTHORS autoscan.log compile configure COPYING INSTALL main.c missing README
rice@rice:~/rice-autotools$ automake --add-missing
configure.ac:6: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated. For more info, see:
configure.ac:6: http://www.gnu.org/software/automake/manual/automake.html#Modernize-AM_005fINIT_005fAUTOMAKE-invocation
rice@rice:~/rice-autotools$
rice@rice:~/rice-autotools$ ./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
rice@rice:~/rice-autotools$ ls
aclocal.m4 autoscan.log config.h config.status COPYING install-sh Makefile.am NEWS
AUTHORS ChangeLog config.h.in configure depcomp main.c Makefile.in README
autom4te.cache compile config.log configure.ac INSTALL Makefile missing stamp-h1
rice@rice:~/rice-autotools$
rice@rice:~/rice-autotools$ make
make all-am
make[1]: Entering directory '/home/tym/rice-autotools/temp'
gcc -DHAVE_CONFIG_H -I. -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 rice main.o
make[1]: Leaving directory '/home/tym/rice-autotools/temp'
rice@rice:~/rice-autotools$ ./rice
Rice Autotools!!!
rice@rice:~/rice-autotools$
上述的内容是在ubuntu上进行操作的,接下来我们说说如何在arm架构的板子进行运行。由于我目前的系统构建环境是基于Yocto上的。所以我这里直接以Yocto进行说明。
在Yocto工程创建自己的recipe。这里我随便找了一个地方放我的recipe进行演示。创建files目录和rice-autotools_git.bb文件。然后将上面我们利用autotools工具得到的内容拷贝到files目录下。如下:
rice@rice:~/yocto/poky/meta-sdk/recipes-connectivity/rice-autotools$ tree
.
├── files
│ ├── AUTHORS
│ ├── ChangeLog
│ ├── configure.ac
│ ├── main.c
│ ├── Makefile.am
│ ├── NEWS
│ └── README
└── rice-autotools_git.bb
1 directory, 9 files
ice@rice:~/yocto/poky/meta-sdk/recipes-connectivity/rice-autotools$
为了方便我们操作,我们将files文件的内容进行压缩。如下:
rice@rice:~/yocto/poky/meta-sdk/recipes-connectivity/rice-autotools$ tar -cvzf rice-autotools.tgz .
./
./.deps/
./.deps/main.Po
./ChangeLog
./NEWS
./AUTHORS
./Makefile.am
./main.c
./README
./configure.ac
tar: .: file changed as we read it
rice@rice:~/yocto/poky/meta-sdk/recipes-connectivity/rice-autotools$ ls
AUTHORS ChangeLog configure.ac main.c Makefile.am NEWS README rice-autotools.tgz
rice@rice:~/yocto/poky/meta-sdk/recipes-connectivity/rice-autotools$
rice-autotools_git.bb文件:内容如下:
SMMARY = "Rice Autotools Test"
LICENSE = "CLOSED"
SRC_URI = "file://rice-autotools.tgz"
S = "${WORKDIR}"
do_install() {
install -d ${D}/usr/bin
install -m 0755 ${WORKDIR}/rice -D ${D}/usr/bin/
}
inherit autotools
然后我们在Yocto进行编译,然后将镜像烧录到板子上。运行结果:
/ # rice
Rice Autotools!!!
/ #
本文分享自 Rice 嵌入式开发技术分享 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!