[Linux] Autotools produced by makefile process

Recommended for you: Get network issues from WhatsUp Gold. Not end users.


   Makefile can help make to complete its mission, But to admit it, Preparation of Makefile really is not an easy thing, Especially for a large project is so. So, Is there a easy means of generating Makefile can also make us enjoy the advantages of make? Autotools series tool is designed for this purpose, It only needs users to input simple object file, the dependent files, file directory, you can easily generate Makefile., This is undoubtedly the users want. In addition, These tools can also complete the system configuration information collection, You can easily deal with all kinds of portability problems.

   It is also based on this, the present software development on Linux generally use Autotools to produce Makefile.

   But this tool for beginners like us is still relatively unfamiliar, use Internet to find and more scattered, so here I put the whole process fully recorded, benefit oneself and others.!

Autotools using the process

    as mentioned earlier, Autotools is a series of tools, readers first to confirm whether the system with the following tools (you can use the which command to view).

     · aclocal

     · Autoscan

     · Autoconf

     · autoheader

     · automake

   The use of Autotools is mainly using every tool to generate the Makefile script file. The is that the overall process:

     ·Use aclocal to generate a “ aclocal.m4” file, the file is a macro definition of local,

     ·Rewrite the “ configure.scan” file, and rename it to “ configure.in”, and use configure files to generate Autoconf files.

   Next, through a simple hello.c example to be familiar with the Autotools to generate makefile.


Look at the effect






   First run the Autoscan command, it will check the source files in the given directory and its subdirectories in the directory tree, if not given, are examined in the current directory and its subdirectories of the tree. It will search the source file for transplantation in general and create a file “ configure.scan”, the document is the next Autoconf to use “ configure.in” prototype.



   Configure.in is a script configuration file Autoconf, the prototype file“configure.scan”

   (Pay attention here) configure.scan will be renamed configure.ac, and manually modify the content. The paper is mainly used for the definition of procedures

Basic information (name, version number, etc.), the need for testing in the compiled before, in which directory to generate Makefile files etc.

   The modified configure.ac is shown as follows

1 #                                               -*- Autoconf -*-             
 2 # Process this file with autoconf to produce a configure script.             
 4 AC_PREREQ([2.68])                                                            
 6 AC_CONFIG_SRCDIR([hello.c])                                                  
 7 AC_CONFIG_HEADERS([config.h])                                                
 9 #add-me-------------                                                         
10 AC_INIT(hello,1.0,)                                    
11 AM_INIT_AUTOMAKE(hello,1.0)                                                  
13 # Checks for programs.                                                       
14 AC_PROG_CC                                                                   
16 # Checks for libraries.                                                      
18 # Checks for header files.                                                   
19 AC_CHECK_HEADERS([stdlib.h])                                                 
21 # Checks for typedefs, structures, and compiler characteristics.             
23 # Checks for library functions.                                              
25 AC_CONFIG_FILES([Makefile])                                                  

   Add-me---- words are added, the AC_INIT macro to define the software name and version information,  

   AM_INIT_AUTOMAKEIt is necessary to automake macros, parameters are the name and version number to generate software suite.


   The following explains other code

   ·“ #” start of annotation.

   ·The AC_PREREQ macro statement the file the required Autoconf version, as in this case the use of version 2.59.

   ·The AC_INIT macro is used to define the software name and version information L.

   ·The AM_INIT_AUTOMAKE is added, it is necessary to automake macros, also as before, the name of the PACKAGE is to produce software suite, VERSION is the version number.

   ·The AC_CONFIG_SRCDIR macro is used to detect the specified source file exists, to determine the source of the directory

Effect of. Here is the current directory under the hello.c.

   ·The AC_CONFIG_HEADER macro is used to generate the config.h file, so that the autoheader use.

   ·The AC_CONFIG_FILES macro is used to generate the corresponding Makefile file.

   ·Comments can be added respectively user testing procedures, test function library, test head files and other macro definition.


   The first run aclocal, generate a “ aclocal.m4” file, the file is a macro definition of local.

   Then run the Autoconf, to generate the &ldquo executable file; configure”.




   Then use the autoheader command, which is responsible for generating the config.h.in file. The tool is usually from “ acconfig.h” replication definition user additional files, no additional symbols defined here so, so do not need to create the &ldquo file; acconfig.h&rdquo. As shown below:



   This step is to create the Makefile an important step, automake script configuration file using Makefile.am, the user needs to create the file yourself. After that, the automake tool to convert it into Makefile.in.
    in this example, create a file for the Makefile.am is shown as follows:

bin_PROGRAMS= hello
hellohello_SOURCES= hello.c

   Following on the script file corresponding to explain.

   ·One of the AUTOMAKE_OPTIONS for setting the automake option. Because GNU has the strict standard to release software, for example, must be accompanied by the license file COPYING statement, or the automake execution will error. Automake provides three software level: foreign, GNU and gnits, allows the user to select the default level, gnu. In this case, using the foreign class, which only detect must file.

   ·The definition of bin_PROGRAMS to generate the executable file name. If you want to generate a plurality of executable files, each file name with a space.

   ·The definition of hello_SOURCES “ hello” the original file for this program need. If ” hello” this program is generated by a plurality of original documents, You must make a list of all the original file it uses all, And separated by spaces. For example: if the target “ hello” “ hello.c” “, sunq.c” “ hello.h” three, the dependent files, The definition of hello_SOURCES=hello.c sunq.c.
    next, can generate &ldquo to use automake; configure.in” file, Use the options here “ — adding-missing” allows automake to automatically add some necessary script file.

   In the aftermath of the automake can generate configure.in files.




5.Run configure

   In this step, through the operation of automatic configuration settings file configure, put the Makefile.in into the Makefile. As shown below

mystery@mystery-dell:~/Desktop/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... no
checking for mawk... mawk
checking whether make sets $(MAKE)... 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 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
configure: creating ./config.status
config.status: creating Makefile
config.status: executing depfiles commands

   As you can see, when running configure to collect information about the system, users can in a configure command to easily configure.

   In the custom parameter./configure has two kinds, one is the switch type (--enable-XXX or --disable-XXX), another is open, the back to fill in a string of characters (--with-XXX=yyyy) parameters. The reader can try to use the method. In addition, readers can view the same directory ” config.log” file, to facilitate debugging purposes.

   So far, makefile can be automatically generated. To recall the steps, users no longer need to customize different rules, and only need input file and directory simple name, this will greatly facilitate the users.

Autotools generated Makefile has common compiler function, also has the following main functions:


   The default implementation of &rdquo type make; make all” command, that is the target for the all, its implementation is shown as follows:

mystery@mystery-dell:~/Desktop/autotools$ make
cd . && /bin/bash ./config.status config.h
config.status: creating config.h
make  all-am
make[1]: Entering directory `/home/mystery/Desktop/autotools'
gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT hello.o -MD -MP -MF .deps/hello.Tpo -c -o hello.o hello.c
mv -f .deps/hello.Tpo .deps/hello.Po
gcc  -g -O2   -o hello hello.o
cd . && /bin/bash ./config.status config.h
config.status: creating config.h
config.status: config.h is unchanged
make[1]: Leaving directory `/home/mystery/Desktop/autotools'

   At this point in this directory will generate executable files “ hello” “./hello”, to show normal results, as shown below:

mystery@mystery-dell:~/Desktop/autotools$ ./hello
!!!Hello World!!!


2.make install

   At this time, will remove the installation to the system directory.,

   If you run Hello directly, also can appear the correct results.


3.make clean

   At this time, make will be removed before the compiled executable file and the target file (object file, *.o), as shown below:

mystery@mystery-dell:~/Desktop/autotools$ make clean
test -z "hello" || rm -f hello
rm -f *.o


4.make dist

   At this time, make will be packaged procedures and related documents as a compressed file for publication, as shown below:

mystery@mystery-dell:~/Desktop/autotools$ make dist
mystery@mystery-dell:~/Desktop/autotools$ ls hello-1.0-tar.gz

   The command generates a compressed file hello-1.0-tar.gz.

   From the above story is not difficult to see, Autotools is a software maintenance and publishing tool, also in view of this, now the GUN software are generally made by automake.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Antonio at November 11, 2013 - 7:43 PM