Generate makefile files using Automake, autoconf

Source: Internet
Author: User
Tags perl script automake

Start with the HelloWorld

We start with the example program HelloWorld we use most often.

The following procedure, if simply speaking, is:

Create a new three file:

    1. HelloWorld. C
    2. Configure. Inch
    3. Makefile. Am

Then execute:

    1. Aclocal; autoconf;automake --add-missing; . /configure; make; . /helloworld

You can see that the makefile is generated, and you can compile the HELLOWORLD.C.

Quite simply, a few commands can make a Makefile, how do you feel.

  Now let's start with the detailed procedure:

1. Building the Catalogue

Build a HelloWorld directory under your working directory, and we use it to store HelloWorld programs and related files, such as under/home/my/build:

    1. $ mkdir Helloword
    2. $ CD HelloWorld

2. HELLOWORLD.C

Then write a hellowrold.c file with your favorite editor, such as command: VI helloworld.c. Use the following code as the content of the HELLOWORLD.C.

    1. #include <stdio.h>
    2. int main(int argc, Char* * argv){
    3. printf("%s", ' Hello, Linux world! \ n");
    4. return 0;
    5. }

Save exit when finished.
Now in the HelloWorld directory there should be a helloworld.c you wrote yourself.

3. Generate Configure

We use the AutoScan command to help us generate a configure.in template file based on the source code in the directory.
Command:

    1. $ autoscan
    2. $ ls
    3. Configure. Scan HelloWorld. C

After execution, a file is generated in the Hellowrold directory: Configure.scan, we can take it as a blueprint for configure.in.
  Now rename Configure.scan to Configure.in, and edit it to modify it to remove extraneous statements as follows:

  1. ==========================configure. In content start =========================================
  2. #-*-Autoconf-*-
  3. # Process This file with autoconf to produce a configure script.
  4. Ac_init(HelloWorld. C)
  5. Am_init_automake(HelloWorld, 1.0)
  6. # Checks for programs.
  7. Ac_prog_cc
  8. # Checks for libraries.
  9. # Checks for header files.
  10. # Checks for typedefs, structures, and compiler characteristics.
  11. # Checks for library functions.
  12. Ac_output(Makefile)
  13. ==========================configure. In content end =========================================

  Then execute the commands aclocal and autoconf, respectively, will produce ACLOCAL.M4 and configure two files:

    1. $ aclocal
    2. $ls
    3. Aclocal. M4 Configure. In HelloWorld. C
    4. $ autoconf
    5. $ ls
    6. Aclocal. M4 autom4te. Cache Configure configure. In HelloWorld. C

As you can see, configure.in content is a macro definition that, when processed by autoconf, becomes the check System feature. Environment variables. The software must have parameters for the shell script.

Autoconf is a tool used to generate automatic configuration software source code scripts (configure). The Configure script can run independently of the autoconf and does not require user intervention during the run.

To generate the Configure file, you must tell autoconf how to find the macro you are using. The method is to use the Aclocal program to generate your ACLOCAL.M4.

Aclocal automatically generates ACLOCAL.M4 files based on the contents of the Configure.in file. Aclocal is a Perl script that is defined as: "Aclocal–create aclocal.m4 by scanning config Ure.ac ".

Autoconf creates configure from configure.in, a template file that enumerates the various parameters required to compile the software.

Autoconf needs the GNU M4 macro processor to handle ACLOCAL.M4, generating configure scripts.

M4 is a macro processor. Copies the input to the output and expands the macro. Macros can be either inline or user-defined. In addition to expanding macros, M4 also has built-in functions for referencing files, executing commands, integer operations, text manipulation, looping, and so on. M4 can be used as the front-end of the compiler, can also be used as a single macro processor.

4. New makefile.am
To create a new makefile.am file, command:

    1. $ VI Makefile. AM
    2. The contents are as follows :
    3. Automake_options=foreign
    4. Bin_programs=helloworld
    5. Helloworld_sources=helloworld. C

 Automake will generate makefile.in based on the makefile.am you write.

  The macros and targets defined in makefile.am instruct Automake to generate the specified code. For example, macro Bin_programs will cause the compilation and connection targets to be generated.
5. Run Automake:

    1. $ automake --add-missing
    2. Configure. In: Installing '. /install-sh'
    3. Configure.in:installing './mkinstalldirs '
    4. Configure. In: Installing '. /missing'
    5. Makefile.am:installing './depcomp '

Automake will produce some files based on the makefile.am file, including the most important makefile.in.

6. perform configure build makefile

  1. $. /configure
  2. Checking for a bsd-compatible install... /usr/bin/install- c
  3. Checking whether build environment is sane... yes
  4. Checking for gawk ...gawk
  5. Checking whether make sets $ (make)... yes
  6. Checking for gcc ...gcc
  7. Checking for C compiler default output ...a. out
  8. Checking whether the C compiler works... yes
  9. Checking whether we is cross compiling... no
  10. Checking for suffix of executables ...
  11. Checking for suffix of object files... o
  12. Checking whether we are using the GNU C compiler ...yes
  13. Checking whether GCC accepts -G ...yes
  14. Checking for gcc option to accept ANSI C... none needed
  15. Checking for style's include used by Make... GNU
  16. Checking dependency style of gcc... gcc3
  17. Configure: Creating . /config. Status
  18. Config. Status: Creating Makefile
  19. Config. Status: Executing depfiles commands
  20. $ ls- l Makefile
  21. -rw-rw-r-- 1 Yutao yutao 15035 Oct :Makefile

As you can see, at this point the makefile has been produced.

7. Compiling code with Makefile

  1. $make
  2. If GCC-dpackage_name=\ "Full-package-name\"-dpackage_tarname=\ "Full-package-name\"-dpackage_version=\ "VERSION\"-dpackage_string=\ "Full-package-name\ VERSION\"-dpackage_bugreport=\ "Bug-report-address\"-dpackage=\ "HelloWorld\"-dversion=\"1.0\"-dstdc_headers=1-dhave_sys_types_h=1-dhave_sys_stat_h=1-dhave_stdlib_h=1-dhave_string_h=1-dhave_memory_h=1-dhave_strings_h=1-dhave_inttypes_h=1-dhave_stdint_h=1 -dhave_unistd_h =1 -dhave_ Stdlib_h=1 -i Span class= "Sh_normal" >-i-g -o2 -MT HelloWorld .o -md -mp -MF Span class= "sh_string" ". Deps/helloworld. Tpo "-c -o helloworld.O Helloworld.c\
  3. Then MV- F ". Deps/helloworld. Tpo " ". Deps/helloworld. Po "; Else RM -F ". Deps/helloworld. Tpo "; exit 1; fi
  4. GCC -G-o2 -o helloworld helloworld. o

Run HelloWorld

    1. $ . /helloworld
    2. Hello, Linux world!

So HelloWorld compiled, if you follow the above steps to do, it should also be easy to compile the correct HelloWorld file. You can also try to use some other make commands, such as make Clean,make Install,make Dist, see what effect they will give you. How's it feel? I can write such a professional Makefile, the boss will be very impressed with you.

Four. In Layman's

For each of the above-mentioned commands, let's do some more detailed introductions.

  1. AutoScan AutoScan is used to scan the source code directory to generate Configure.scan files. AutoScan can use directory names as parameters, but if you don't use parameters,  Then AutoScan will assume that the current directory is being used. AutoScan will scan the source files in the directory you specify and create Configure.scan files. 2. Configure.scan Configure.scan contains the basic options for system configuration, which are all macro definitions. We need to rename it to Configure.in 3. Aclocal Aclocal is a Perl scripting program. aclocal automatically generates ACLOCAL.M4 files based on the contents of the Configure.in file. Aclocal is defined as: "ACLOCAL-CREATEACLOCAL.M4 by S  Canning Configure.ac ". 4. Autoconf autoconf is used to generate configure files. Configure is a script that can set up the source program to accommodate a variety of operating system platforms and generate the appropriate Makefile based on different systems.  This allows your source code to be compiled on different operating system platforms. The content of the Configure.in file is a macro that, when processed autoconf, becomes a check System feature. Environment variables. The order of macros in the software must be the parameters of the shell script. configure.in file is not specified, but you must  The first and last faces are added Ac_init macros and Ac_output macros respectively.  In Configure.ini: #号表示注释, the content behind this macro is ignored. Ac_init (FILE) This macro is used to check the path where the source code is located. Am_init_automake (Package, version) This macro is a must, which describes the name of the packages we are going to generate and their version number: package is the name of the packages, VE Rsion is the version number. When you use the Make Dist command, it will generate a software release package similar to helloworld-1.0.tar.gz, with the name and version number of the corresponding package.  AC_PROG_CC This macro will check the C compiler used by the system. Ac_output (FILE) This macro is the name of the makefile that we want to output. We are using AUTOmake, we actually need to use some other macros, but we can use aclocal to help us generate them automatically. After executing aclocal we will get aclocal.m4 file.  After generating the configure.in and ACLOCAL.M4 two macro files, we can use the autoconf to generate the Configure file. 5. makefile.am makefile.am is used to generate makefile.in, which you need to write manually. Some of the content is defined in makefile.am: Automake_options This is the Automake option. When Automake is executed, it checks the directory for the existence of various files in the standard GNU software package, such as AUTHORS.ChangeLog.NEWS files. When we set it to foreign, Automake will use the standard package to check. Bin_programs This is to specify the executable file we want to produce. The file name. If you want to produce multiple executables, separate the names with spaces. Helloworld_sources This is the source code required to specify the "HelloWorld". If it uses more than one source file,  Then use a space number to separate them. For example need HELLOWORLD.H,HELLOWORLD.C then please write: helloworld_sources= helloworld.h helloworld.c.  If you define more than one executable file in Bin_programs, you define a relative filename_sources for each executable file.  6. Automake we use Automake--add-missing to generate makefile.in.  The definition of option--add-missing is "add missing standard files to the package", which will allow Automake to add some of the files that are necessary for a standards-only software bundle.  The makefile.in file that we produced with Automake is in accordance with the GNU Makefile Convention, and then we can produce the appropriate Makefile file just by executing the Configure Shell script. 7. In Makefile, which complies with the GNU Makefiel Convention, Makefile contains some basic pre-defined actions: make the source code according to the Makefile, connect, generate the targetfile, executable file. Make clean clears the object file (the file with the suffix ". O") from the last make command and the executable file. Do install installs the successful executable file into the system directory, typically/usr/local/  The bin directory. Make Dist generates a release package file (that is, the distribution package). This command will package the executable and related files in a tar.gz compressed file to be used as software packages for publishing. It generates a file with a name similar to "package-version.tar.gz" in the current directory. Package and version are the Am_init_automake (package, version) that we defined in configure.in. Make Distcheck Build and test the release package to determine the correctness of the release .

Generate makefile files using Automake, autoconf

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.