Author:istone
E-mail:liul.stone@gmail.com
date:2015-09-19 15:16:38 One use Autotools toolset 1.1 what is Autotools
We all know the power of Make project manager. But writing makefile is not an easy thing to do, especially for a larger project. So, is there an easy way to generate makefile while at the same time allowing users to enjoy make the advantages of it. This autotools series of tools is designed for this purpose, it can easily generate makefile by simply entering a simple target file, dependent files, file directory, and so on. In addition, these tools can also complete the collection of system configuration information so that you can easily handle a variety of portability issues. It is based on this, now Linux software development generally use Autotools to generate makefile. 1.2 autotools Use process
As noted above, Autotools is a series of tools that contain aclocal, AutoScan, autoconf, Autoheader, and Automake tools, Using Autotools is primarily the use of the scripting files of each tool to generate the final makefile files. The overall process is as follows:
Figure 1-1 Autotools Generate makefile Flowchart
Then use/*sayhello.c*/as an example: 0, install Autotools
Install the Autotools toolset, CentOS can be installed using yum install autoconf automake online. 1, create a directory
Create a testautotools directory that will serve as a place to store SayHello programs and their associated files. 2. Create source file
Create the SAYHELLO.C source and sayHello.h headers in the Testautotools directory with the editor as follows:
[Root@localhost testautotools]# cat sayhello.c
#include <stdio.h>
#include "sayHello.h"
int main ()
{
printf ("program say:%s\n", STR);
return 0;
}
[Root@localhost testautotools]# cat sayHello.h
#ifndef sayhello_h
#define SAYHELLO_H
#define STR "Hello Autotools "
#endif
[root@localhost testautotools]# ls
sayhello.c sayHello.h
Next we will use Autotools to generate makefile documents for us: 3, AutoScan
[Root@localhost testautotools]# AutoScan
[root@localhost testautotools]# ls
autoscan.log Configure.scan sayhello.c sayHello.h
[root@localhost testautotools]#
It examines the source file in the given directory and its subtree, which is checked by default in the current directory and its subdirectory tree. The execution of the AutoScan produces a Configure.scan file that we can use as the blueprint for the configure.in document. However, the above results show that AutoScan will first read the Configure.ac file, but the profile has not been created at this time, so it automatically generates a Configure.scan file.
[root@localhost testautotools]# ls
autoscan.log configure.scan sayhello.c sayHello.h
4, autoconf
Configure.in is a autoconf script configuration file whose prototype file Configure.scan renamed Configure.in and changed the content to read as follows:
#-*-autoconf-*- //action notes starting with "#".
ac_prereq (2.59) //autoconf version required for this document.
ac_init (sayhello,1.0,liul.stone@gmail.com) //Ac_init macros are used to define information such as the name and version of the software.
Am_init_automake (sayhello,1.0) //is a prerequisite for Automake macros, software name and version number.
Ac_config_srcdir ([sayhello.c]) //is used to detect the existence of the specified source file.
Ac_config_header ([config.h]) //is used to generate config.h files for autoheader use.
AC_PROG_CC
Ac_config_files ([Makefile]) //is used to generate the corresponding Makefile file.
Ac_output
Next, run aclocal to generate a "aclocal.m4" file that handles local macro definitions.
[Root@localhost hello]# aclocal
Then run autoconf and build the Configure executable file.
[Root@localhost hello]# autoconf
[root@localhost hello]# ls
aclocal.m4 autom4te.cache autoscan.log Configure configure.in sayhello.c sayHello.h
5, Autoheader
Use the Autoheader command to become a config.h.in file. The tool typically replicates user-defined symbols from the Acconfig.h file, and does not need to create a acconfig.h file if there are no custom symbols here.
[Root@localhost testautotools]# autoheader
[root@localhost testautotools]# ls
aclocal.m4 Autom4te.cache autoscan.log config.h.in Configure configure.in sayhello.c SayHello.h
[Root@localhost testautotools]#
6, Automake
This step is an important step in creating makefile, Automake script configuration file is makefile.am, you need to create the appropriate file. Then use the Automake tool to turn it into a makefile.in file. The contents are as follows:
[Root@localhost testautotools]# cat makefile.am
automake_options=foreign
bin_programs= SayHello
sayhello_sources= sayhello.c sayHello.h
[root@localhost testautotools]#
Automake_options: Sets the Automake option. Since the GNU has a strict specification for its own software release, such as the need for a license declaration file copying and so on, otherwise automake execution will be an error. Automake offers 3 levels of software: foreign, GNU, GnitS, where we use foreign to detect only the necessary files.
Bin_programs: Defines the executable file name to be produced. If you want to produce multiple execution files separated by a space.
Sayhello_sources: Defines the source files needed to SayHello this executable file. If SayHello This program is generated by multiple original files, you must list all the source files it uses, separated by a space. For example, the definition: SayHello _sources=sayhello.c sayHello.h. Note: If you want to define multiple execution files, you define the appropriate _sources for each execution file.
Next, use Automake to generate configure.in files, where you can use options-adding-missing to have Automake automatically add some necessary script files. As shown below:
[Root@localhost testautotools]# automake--add-missing
configure.in:6: Installing './install-sh '
Configure.in:6: Installing './missing '
Makefile.am:installing './depcomp '
[root@localhost testautotools]# LS
aclocal.m4 autom4te.cache autoscan.log config.h.in Configure configure.in Depcomp install-sh makefile.am makefile.in missing sayhello.c sayHello.h
[ Root@localhost testautotools]#
7, configure
The
Configure makefile.in into the final makefile by running the automatic configuration settings file.
[Root@localhost testautotools]#./configure Checking for a bsd-compatible install .../usr/bin/install-c checking Build environment is sane ... yes checking for a thread-safe mkdir-p .../bin/mkdir-p checking for gawk ... gawk Whether make sets $ (make) ... yes checking for gcc ... gcc checking for C compiler default output file name ... a.out checkin
G whether the C compiler works ... yes checking whether we are cross ... no compiling for checking of suffix ... Checking for suffix of object files ... o checking whether we are using the GNU C compiler ... yes checking whether GCC acce Pts-g. Yes checking for GCC option to accept ISO C89 ... none needed checking for style, include used by make ... GNU checking dependency style of gcc gcc3 configure:creating/config.status config.status:creating Makefile config.s tatus:creating config.h config.status:executing depfiles commands [root@localhost testautotools]# [Root@localhost Test autotools]# ls ACLOCAL.M4Autoscan.log config.h.in config.status configure.in install-sh makefile.am missing sayHello.h Autom4te.cache Co Nfig.h config.log Configure Depcomp Makefile makefile.in sayhello.c stamp-h1 [root@localhost Testau totools]#
The makefile is automatically generated by this step. 1.3 using make command
After the makefile, we will use make project Manager to implement the makefile. 1, make
Make by default executes the make all command, and its execution is as follows:
[Root@localhost testautotools]# make
make all-am
make[1]: Entering directory '/home/liul/work_dir/ Testautotools '
gcc-dhave_config_h-i. -G-O2-MT sayhello.o-md-mp-mf. deps/sayhello.tpo-c-o sayhello.o sayhello.c mv-f. Deps/sayhello.tpo
Lo. Po
gcc -g-o2 -o sayhello
sayhello.o make[1]: Leaving directory '/home/liul/work_dir/ Testautotools '
[root@localhost testautotools]#
If the previous makefile operation is correct, the execution of the make command appears above, and the hello executable is generated in the directory, and the SayHello program is run:
[Root@localhost testautotools]#/sayhello program
say:hello autotools
[root@localhost testautotools]#
2, make install
Install the SayHello program into the system directory:
[root@localhost testautotools]# make install
make[1]: Entering directory '/home/liul/work_dir/testautotools '
test-z "/usr/local/bin" | |/bin/mkdir-p "/usr/local/bin"/usr/bin/install-c
sayhello '/usr/local/bin '
MAKE[1]: Nothing is done for ' install-data-am '.
MAKE[1]: Leaving directory '/home/liul/work_dir/testautotools '
[root@localhost testautotools]#
At this point, if the direct operation of the SayHello can be the following results show that the operation is correct:
[Root@localhost testautotools]# SayHello program
say:hello autotools
[root@localhost testautotools]#
3. Make Clean
The command is to purge previously compiled executables and target files (object file, *.O):
[Root@localhost testautotools]# make clean
test-z "SayHello" | | rm-f sayhello rm-f *.o
[root@localhost Te stautotools]#
4, Make Dist
When we do all the work, the final step is to compress the document for publication:
[Root@localhost testautotools]# make Dist CD. &&/bin/sh/root/link2workdir/testautotools/missing--run automake-1.11--foreign Makefile CD. &&/bin/sh/config.status Makefile depfiles config.status:creating Makefile config.status:executing depfiles C Ommands {test!-d "sayHello-1.0" | | {find "sayHello-1.0"-type d!-perm-200-exec chmod u+w {} '; ' && rm-fr ' sayHello-1.0 ';}; } test-d "sayHello-1.0" | | mkdir "sayHello-1.0" test-n "" \ | | Find "sayHello-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/root/link2workdir/testautotools/install-sh-c-M A+r {} {} \; \
|| Chmod-r a+r "sayHello-1.0" tardir=sayhello-1.0 &&/bin/sh/root/link2workdir/testautotools/missing--run tar Chof-"$tardir" | Gzip=--best gzip-c >sayhello-1.0.tar.gz {test!-d "sayHello-1.0" | | {find "sayHello-1.0"-type d!-perm-200-exec chmod u+w {} '; ' && rm-fr ' sayHello-1.0 ';}; [Root@localhost testautotools]# [root@localhost testautotools]# ls aclocal.m4 autoscan.log config.h.in config.sta Tus configure.in install-sh makefile.am missing sayhello.c stamp-h1 autom4te.cache Config . Log Configure Depcomp Makefile makefile.in sayhello-1.0.tar.gz sayHello.h [root@localhost testautotools ]# [Root@localhost testautotools]#
5, Summary
Sayhello-1.0.tar.gz is the compressed file that we end up releasing. Autotools the entire process is introduced, in reality more will use Autotools to generate makefile documents for us.