How to use the GNU Autotools

Source: Internet
Author: User
Tags automake

Writing makefile by hand is a very interesting thing, and for larger projects, it is a good thing if there are tools to do it. In the Linux system development environment, the GNU Autotools undoubtedly played this important role. (In a Windows system development environment, IDE tools, such as visual Studio, are also handy for managing projects.) )

This article takes a simple project as an example to illustrate the use of a list of the GNU Autotools Tools and their commands.

Autotools is a series of tools, mainly composed of autoconf, Automake, Perl language Environment and M4, and contains five commands:
(1) aclocal
(2) AutoScan
(3) autoconf
(4) Autoheader
(5) Automake



first, prepare the source code

(1) The Catalog project contains a MAIN.C file and two subdirectories with a test.c,include directory in the Lib and Include;lib directory containing a test.h. In the system, the following is displayed:

[email protected] project]# ls  include  lib main.c [[  email protected] project]#  [[Email Protected] project]# ls include/  test.h  [[email protected] project]# ls lib/  test.c  [[email protected] project]#
(2) The source code is as follows:



/* PROJECT/MAIN.C *  /#include <stdio.h>  #include "include/test.h"  int main ()  {      printf ("Main entrance./n");      Test_method ();      return 0;  }
/* project/lib/test.c */      #include <stdio.h>      #include ". /include/test.h "      void Test_method ()      {          printf (" Test method./n ");      }
/* project/include/test.h*/      void Test_method ();




Second, autotools use steps 2.1 AutoScan

Using the AutoScan command, it will scan the working directory and generate the Configure.scan file.

[Email protected] project]# AutoScan      autom4te:configure.ac:no such file or directory      AutoScan:/usr/bin/ Autom4te failed with exit status:1      [[email protected] project]# ls      autoscan.log  configure.scan  Include  Lib  main.c      [[email protected] project]#
2.2 Configure.scan renamed as: Configure.ac

Rename the Configure.scan file to Configure.ac and make the appropriate modifications. In Configure.ac, the line starting with the # is a comment, the other is the M4 Macro command, and the main function of the macro in Configure.ac is to detect the system.

[Email protected] PROJECT]MV configure.scan configure.ac      [[email protected] project]# ls      autoscan.log  Configure.ac include  Lib  main.c      [[email protected] project]#      [[email protected] project]# cat Configure.ac      #                                               -*-Autoconf-*-      # Process This file with Autoconf to produce a configure script.      Ac_prereq (2.59)      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.      # Checks for typedefs, structures, and compiler characteristics.      # Checks for library functions.      Ac_output      [[email protected] project]#
2.3 Modifying Configure.ac

Make appropriate changes to the Configure.ac file, and the modification is displayed as follows [1]:

[[email protected] project]# cat configure.ac      #                                               -*-Autoconf-*-      # Process This file with Autoconf to produce A configure script.      Ac_prereq (2.59)      #AC_INIT (Full-package-name, VERSION, bug-report-address)      Ac_init (hello,1.0,[email Protected])      Am_init_automake (hello,1.0)      ac_config_srcdir ([main.c])      ac_config_header ([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

Description

(1) lines starting with "#" are comment lines.
(2) Ac_prereq macro declares the autoconf version required by this article, as in this example, version 2.59.

(3) Ac_init macro is used to define the software name, version and other information, the author's e-mail and so on.
(4) Am_init_automake is manually added, it is necessary for Automake macros, Full-package-name is the software name, version is the software release number.
(5) Ac_config_scrdir macro is used to detect the existence of the specified source code files, to determine the validity of the source directory. Here is the current directory under MAIN.C.

(6) The Ac_config_header macro is used to generate the Config.h file so that the Autoheader command is used.
(7) AC_PROG_CC is used to specify the compiler, if not specified, the default GCC.
(8) Ac_output used to set the Configure to produce the file, if makefile,configure will check it out of the results brought into the makefile.in file to produce the appropriate makefile. When using Automake, some additional parameters are required, which are generated with the Aclocal tool.
(9) The Ac_config_files macro is used to generate the corresponding makefile file.

2.4 aclocal

Using the aclocal command, scan the Configure.ac file to generate a ACLOCAL.M4 file that primarily handles local macro definitions, which are based on macros that have been installed, user-defined macros, and ACINCLUDE.M4 files Configure.ac The file requires a macro set defined in the file ACLOCAL.M4. [2]

[Email protected] project]# aclocal  [[email protected] project]# ls  aclocal.m4  autom4te.cache  Autoscan.log  configure.in  include  Lib  main.c  [[email protected] project]#
2.5autoconf

Use the autoconf command to generate the Configure file. This command expands the macros in the Configure.ac file to generate the Configure script. This process may need to use the macros defined in ACLOCAL.M4.

[Email protected] project]# autoconf      [[email protected] project]# ls      aclocal.m4  autom4te.cache  Autoscan.log  Configure  configure.in  include  Lib  main.c
2.6 Autoheader

Use the Autoheader command to generate the config.h.in file. This command typically copies the user-attached symbol definitions from the "acconfig.h" file. There is no additional symbol definition in this example, so you do not need to create a "acconfig.h" file [2].

[Email protected] project]# autoheader      [[email protected] project]# ls      aclocal.m4  autom4te.cache  Autoscan.log  config.h.in  Configure  configure.in  include  Lib  main.c      [[Email Protected] project]#


2.7 Creating makefile.am

Create the makefile.am file manually. The Automake tool converts makefile.am into makefile.in files according to the parameters in the configure.in.

[Note: The first line should be: automake_options = foreign, the original author less write a ' a ']

[email protected] project]# Cat makefile.am  utomake_options = foreign  bin_programs = Hello  hello_ SOURCES = main.c include/test.h lib/test.c

Description

(1) Where the automake_options is the option to set the Automake. Since GNU has strict specifications for its own software release, such as the need to attach a license statement file copying, or Automake execution will be an error. Automake offers 3 software levels: foreign, GNU and gnits for users to choose from. The default level is GNU. In this example, the foreign level is used, which detects only the necessary files.

(2) Bin_programs defines the execution file name to be generated. If you want to produce multiple execution files, each file name is separated by a space.
(3) hello_sources defines "Hello" as the original file required by this executable program. If the "Hello" program is generated by multiple source files, you must list all the source files it uses, separated by a space. If you want to define multiple executable programs, you need to establish a corresponding file_sources for each executable program.

2.8 Automake

Use the Automake command to generate the makefile.in file. Use the option "--add-missing" to have Automake automatically add some required script files.

[Email protected] project]# automake--add-missing      configure.ac:installing './install-sh '      configure.ac: Installing './missing '      Makefile.am:installing './install '      Makefile.am:required file './news ' not found      Makefile.am:required file './readme ' not found      Makefile.am:required file './authors ' not found      Makefile.am:req uired file './changelog ' not found      Makefile.am:installing './copying '      Makefile.am:installing './depcomp '      [email protected] project]#
2.8.1 runs once again using automake--add-missing, which can assist in generating several necessary files.


[Email protected] project]# automake--add-missing      Makefile.am:required file './news ' not found      makefile.am: Required file './readme ' not found      Makefile.am:required file './authors ' not found      Makefile.am:required file '. ChangeLog ' not found      [[email protected] project]# ls      aclocal.m4  autom4te.cache  autoscan.log  config.h.in  config.h.in~  Configure  configure.ac  COPYING  depcomp  include  INSTALL  install-sh  lib  main.c  makefile.am  missing      [[email protected] project]#


2.8.2 four files that were not found above in the current directory and run once again using Automake--add-missing.


    [email protected] project]# Touch NEWS [[email      protected] project]# touch README      [[email protected] project]# to Uch AUTHORS      [[email protected] project]# Touch ChangeLog [      [email protected] project]#      [[email protected] project]# automake--add-missing      [[email protected] project]# ls      aclocal.m4  autom4te.cache  ChangeLog    config.h.in~  config.status  configure.ac  depcomp  INSTALL     Lib     makefile.am  missing  README      AUTHORS     autoscan.log    config.h.in  config.log    Configure      COPYING       include  install-sh  main.c  makefile.in  NEWS      [email Protected] project]#  
2.9./configure

Use the Configure command to turn makefile.in into the final Makefile file.



    [[email protected] project]#./configure checking for a bsd-compatible install .../usr/bin/install-c c Hecking whether build environment is sane ... yes checking for gawk ... gawk checking whether make sets $ (make): . Yes checking for gcc ... gcc checking for C compiler default output file name ... a.out checking whether the      C compiler Works ... yes checking whether we is cross compiling ... no checking for suffix of executables ... Checking for suffix of object files ... o checking whether we are using the GNU C compiler ... yes checking Wheth Er gcc accepts-g ... yes checking for GCC option to accept ANSI C ... none needed checking for style of include U Sed by make ... GNU checking dependency style of gcc gcc3 configure:creating./config.status config.status:creating Ma Kefile config.status:creating config.h config.status:config.h is unchanged config.status:executing DEPFI Les Commands [[email protected] project]# ls aclocal.m4 autom4te.cache ChangeLog config.h.in Config.log Co nfigure COPYING Hello INSTALL lib main.o makefile.am missing README test.o AUTHORS Autosc An.log config.h config.h.in~ config.status configure.ac depcomp include Install-sh main.c Makefile makefile.i   n NEWS stamp-h1 [[email protected] project]#
The makefile file has been generated successfully.

third, the use of makefile 3.1 Make command

To compile the code, by default executing the "Make all" command, you can see the executable file that generated "Hello",

[[email protected] project]# make  make  all-am  make[1]: Entering directory '/home/chenjie/project '  GCC  -g-o2-   o hello  main.o test.o  make[1]: Leaving directory '/home/chenjie/project '  [[email Protected] project]#  [[email protected] project]# ls  aclocal.m4  autom4te.cache  ChangeLog  config.h.in   config.log     Configure     COPYING  Hello    INSTALL     lib     main.o    makefile.am  missing  README    test.o  AUTHORS     autoscan.log   config.h config.h.in ~  config.status  configure.ac  depcomp  include  install-sh  main.c  Makefile  makefile.in  NEWS     stamp-h1  [[email protected] project]#
3.2 Make Clean

command clears the compile-time obj file, which corresponds to the make command, one to compile, and one to clear the compiled file

3.3 Run

"./hello" to see the results of the operation:

    [Email protected] project]#/hello      main entrance.      Test method.      [Email protected] project]#  
3.4 make install

command to install the target file into the system. This, directly enter Hello, you can see the program's running results.

    [[email protected] project]# make install      make[1]: Entering directory '/home/chenjie/project ' test-z '      /usr/ Local/bin "| | Mkdir-p--"/usr/local/bin"        /usr/bin/install-c ' hello '/usr/local/bin/hello ' make[1]: Nothing to is done for      ' I Nstall-data-am '.      MAKE[1]: Leaving directory '/home/chenjie/project '      [[email protected] project]#      [[email protected] project]# Hello      main entrance.      Test method.      [Email protected] project]#  
3.5 Make Uninstall

command to unload the target file from the system.

3.6 Make Dist

command to package the program and related documents as a compressed document for publication, in this example, the resulting package file is named: hello-1.0.tar.gz.

    [[email protected] project]# make dist {test!-D hello-1.0 | | {Find Hello-1.0-type D!-perm-200-exec chmod u+w {} '; ' && rm-fr hello-1.0;}; } mkdir hello-1.0 find Hello-1.0-type d! -perm-755-exec chmod A+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/home/chenjie/project/install-sh-c-M A+r {} {}/; /              ||  Chmod-r a+r hello-1.0 tardir=hello-1.0 &&/bin/sh/home/chenjie/project/missing--run tar Chof-"$tardir" | Gzip=--best gzip-c >hello-1.0.tar.gz {test!-D hello-1.0 | | {Find Hello-1.0-type D!-perm-200-exec chmod u+w {} '; ' && rm-fr hello-1.0;}; } [[email protected] project]# ls aclocal.m4 autom4te.cache ChangeLog config.h.in config.log confi Gure COPYING hello include install-sh maiN.C Makefile makefile.in NEWS stamp-h1 AUTHORS autoscan.log config.h config.h.in~ config.status C Onfigure.ac Depcomp hello-1.0.tar.gz INSTALL lib main.o makefile.am missing README test.o [[Email    protected] project]#


four How to use a published compressed document

4.1 Download to "hello-1.0.tar.gz" compressed document

4.2 Extracting using the "tar-zxvf hello-1.0.tar.gz" command

4.3 using the "./configure" command, the primary function is to configure the software to be installed to check whether the current environment meets the dependencies for installing the software.

4.4 Use the "make" command to compile the source code file to generate the package.

4.5 Use the Make install command to install the compiled package.

    [[email protected] chenjie]# ls hello-1.0.tar.gz [[email protected] chenjie]# tar-zxvf hello-1.0. tar.gz [[email protected] chenjie]# ls hello-1.0 hello-1.0.tar.gz [[email protected] chenjie]# C D hello-1.0 [[email protected] hello-1.0]# ls aclocal.m4 AUTHORS ChangeLog config.h.in Configure Confi      Gure.ac COPYING Depcomp include INSTALL install-sh Lib main.c makefile.am makefile.in missing NEWS README [[email protected] hello-1.0]# [[email protected] hello-1.0]# [[email protected] hello-1.0]#./C Onfigure Checking for a bsd-compatible install .../usr/bin/install-c checking whether build environment is sane ... yes checking for gawk ... gawk checking whether make sets $ (make) ... yes checking for gcc ... gcc ch Ecking for C compiler default output file name ... a.out checking whether the C compiler works ... yes checking wh Ether We is cross compiLing ... No checking for suffix of executables ... checking for suffix of object files ... o checking whether we are U Sing the GNU C compiler ... yes checking whether GCC accepts-g ... yes checking for GCC option to accept ANSI C. . None needed checking for style's include used by make ... GNU checking dependency style of gcc gcc3 configure:creating./config.status config.status:creating Ma Kefile config.status:creating config.h config.status:executing depfiles commands [[email protected] hello-1.0]# [[email protected] hello-1.0]# make make make All-am make[1]: Entering directory '/home/chenji     e/hello-1.0 ' if gcc-dhave_config_h-i.-I.-I. -G-O2-MT main.o-md-mp-mf ". Deps/main. Tpo "-c-o main.o main.c; /Then Mv-f ". Deps/main. Tpo "". Deps/main. Po "; Else Rm-f ". Deps/main. Tpo "; Exit 1;     fi if gcc-dhave_config_h-i.-I.-I. -G-O2-MT test.o-md-mp-mf ". Deps/test. Tpo "-C-o test.o ' test-f ' lib/test.c ' | | echo './' lib/test.c; /Then Mv-f ". Deps/test. Tpo "". Deps/test. Po "; Else Rm-f ". Deps/test. Tpo "; Exit 1; Fi gcc-g-o2-o Hello main.o test.o make[1]: Leaving directory '/home/chenjie/hello-1.0 ' [[email  protected] hello-1.0]# [[email protected] hello-1.0]# make install make[1]: Entering directory '/home/chenj ie/hello-1.0 ' test-z "/usr/local/bin" | | Mkdir-p--"/usr/local/bin"/usr/bin/install-c ' hello '/usr/local/bin/hello ' make[1]: Nothing to being done fo      R ' Install-data-am '.  MAKE[1]: Leaving directory '/home/chenjie/hello-1.0 ' [[email protected] hello-1.0]# [[email protected]      hello-1.0]# Hello main entrance.   Test method.
v. the entire flowchart used by the command

Figure I do not draw, reproduced two pictures [2][3], compared to see, perhaps more understand some.





Vi. Summary

This article describes how to use the GNU Autotools to manage source code, publish the source code package, and how to compile and install the source code package after it has been obtained. As this example is too simplistic, the use of the GNU Autotools is not fully described and is mainly reflected in the following points:

(1) in creating a makefile.am file, the description is simple. In real projects, file relationships are complex, and there are relationships that reference other dynamic libraries, third-party dynamic libraries, and so on.

(2) Although makefile is generated automatically, it is important to understand its rules. Makefile the rules involved are not described in this article.

When you are free, write a blog to describe both of these issues.

[1] http://book.chinaitlab.com/linux/777286.html

[2] http://blog.ossxp.com/2010/04/954/



How to use the GNU Autotools

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.