Automake and Autoconf

Source: Internet
Author: User
ArticleDirectory
    • 1. Introduction to makefile
    • II. Environment used
    • 3. Start with helloworld
    • 4. In-depth Introduction

As a LinuxProgramDevelopers, everyone must have met makefile. It is really convenient to compile your own programs by using the make command. generally, you write a simple makefile manually. It is not easy to write a makefile that complies with the Free Software conventions.

In this article, we will introduce how to use Autoconf and automake tools to help us automatically generate makefiles that conform to the Free Software conventions, just like common GNU programs, as long ". /configure "," make "," make instal ", you can install the program in Linux.

This will be especially suitable for openingSource codeSoftware developers, or if you write a small toy program by yourself, this article will be of great help to you.

1. Introduction to makefile

Makefile is used for automatic compilation and linking. A project consists of many files. Changes to each file will lead to re-linking of the project, but not all files need to be re-compiled, the makefile records the file information. During make, it determines which files need to be re-compiled during the link.

The purpose of makefile is to let the compiler know which files need to be depended on to compile a file. when the dependent files have changed, the compiler will automatically find that the final generated file is out of date and re-compile the corresponding module.

The basic structure of makefile is not very complex. However, when a program developer starts to write makefile, it is often suspected that what he writes is in line with the conventions and that he writes
Makefile and your own
The development environment is associated. When the system environment variables or paths change, the makefile may need to be modified. This causes the makefile to be manually written.
Many problems, automake can help us solve these problems well.

With automake, developers only need to write some simple
File. Autoconf generates configure based on one macro file, and automake generates makefile. In based on another macro file, and then uses
Configure generates a conventional makefile Based on makefile. in. The following describes
Automake generation method.

II. Environment used

The program mentioned in this article is based on the Linux release version: Fedora Core Release 1, which contains the Autoconf, automake.

3. Start with helloworld

We start with helloworld, the most commonly used example program.

The following process is as follows:

Create three new files:

    1. Helloworld.C
    2. Configure.In
    3. Makefile.AM

Then execute:

    1. Aclocal;Autoconf;Automake-- Add-Missing; ./Configure;Make; ./Helloworld

The makefile is generated, and helloworld. C can be compiled through.

It's easy. Just a few commands can make a makefile that complies with the conventions. How do you feel.

Now we will introduce the detailed process:

1. Create a directory

Create a helloworld directory in your working directory and use it to store the helloworld program and related files, such as in/home/My/Build:

    1. $ Mkdir helloword
    2. $ CD helloworld

2. helloworld. c

Then use your favorite editor to write a hellowrold. c file, such as the command: VI helloworld. C. Use the followingCodeAs the content of helloworld. C.

    1. # Include <Stdio. h>
    2. Int Main(IntArgc, Char**Argv){
    3. Printf("% S", 'Hello, Linux World!\ N");
    4. Return 0;
    5. }

Save and exit.
Now there should be a self-written helloworld. c In the helloworld directory.

3. Generate configure

We use the autoscan command to 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 Configure. Scan will be generated in the hellowrold directory. We can use it as the blueprint for Configure. In.
Change Configure. Scan to configure. In and edit it. Modify the following content to remove irrelevant statements:

  1. ======================================Configure.InContent 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.InContent ended========================================================== =

Then execute the commands aclocal and Autoconf to generate two files: aclocal. M4 and configure:

    1. $ Aclocal
    2. $ Ls
    3. Aclocal.M4 configure.InHelloworld.C
    4. $ Autoconf
    5. $ Ls
    6. Aclocal.M4 autom4te.Cache configure.InHelloworld.C

As you can see, configure. In contains some macro definitions. These macros, after being processed by Autoconf, will become shell scripts that check system features, environment variables, and necessary software parameters.

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

To generate the configure file, you must tell Autoconf how to find your macro by using the aclocal program to generate your aclocal. M4.

Aclocal depends on configure. in File Content, automatically generate aclocal. m4 file. aclocal is a Perl script program, which is defined as "aclocal-create aclocal. m4 by scanning configure. AC ".

Autoconf creates configure from the configure. In template file that lists the parameters required for software compilation.

Autoconf requires the GNU M4 macro processor to process aclocal. M4 and generate the configure script.

M4 is a macro processor. copy the input to the output and expand the macro. macros can be embedded or user-defined. in addition to macro expansion, M4 also has some built-in functions used to reference files, execute commands, integer operations, text operations, loops, and so on. m4 can be used either as the front-end of the compiler or as a macro processor.

4. Create makefile. AM
Create a makefile. Am file and run the following command:

    1. $ VI makefile.AM
    2. The content is as follows::
    3. Automake_options=Foreign
    4. Bin_programs=Helloworld
    5. Helloworld_sources=Helloworld.C

Automake will automatically generate makefile. In according to your makefile. am.

Macros and targets defined in makefile. Am will guide automake to generate Specified Code. For example, macro bin_programs will generate compilation and connection targets.
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 generate some files based on the makefile. Am file, including the most important makefile. In.

6. Execute configure to generate makefile

  1. $./Configure
  2. CheckingForABSD-compatibleInstall... /Usr/bin/install -C
  3. Checking whether build environment is sane...Yes
  4. CheckingForGawk...Gawk
  5. Checking whether make sets$ (Make)...Yes
  6. CheckingForGcc...Gcc
  7. CheckingForC compiler default output...A.Out
  8. Checking whether the C compiler works...Yes
  9. Checking whether we are cross compiling...No
  10. CheckingForSuffix of executables...
  11. CheckingForSuffix of object files...O
  12. Checking whether we are using the gnu c Compiler...Yes
  13. Checking whether GCC accepts-G...Yes
  14. CheckingForGCC option to accept ANSI C...None needed
  15. CheckingForStyle of 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-LMakefile
  21. -RW-R--1Yutao15035Oct15 10:40Makefile

As you can see, makefile has been generated.

7. Use makefile to compile the code

  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 . -I . -G -O 2 -MT Helloworld . O -MD -MP -MF ". Deps/helloworld. TPO" -C -O Helloworld. O helloworld . C ; \
  3. ThenMV-F ". Deps/helloworld. TPO" ". Deps/helloworld. Po"; ElseRm-F ". Deps/helloworld. TPO"; Exit 1; Fi
  4. Gcc-G -O2-OHelloworld.O

Run helloworld

    1. $./Helloworld
    2. Hello,Linux World!

In this way, helloworld is compiled. If you follow the above steps, you should easily compile the correct helloworld file. you can also try to use other make commands, such as make clean, make install, and make Dist, to see what effect they will give you. how do you feel? If you can write such a professional makefile yourself, the boss will surely look at you.

4. In-depth Introduction

For the commands mentioned above, we will introduce them in detail.

1. autoscan
Autoscan is used to scan the source code directory to generate the configure. Scan file. autoscan
You can use the directory name as the parameter, but if you do not use the parameter, autoscan considers it to be the current directory.
Autoscan scans the source files in the specified directory and creates the configure. Scan file.
2. Configure. Scan
Configure. Scan contains the basic options for system configuration, which are some macro definitions. We need to change it
Configure. In
3. aclocal
Aclocal is a Perl script program. aclocal depends on the content of the configure. In file.
To automatically generate the aclocal. M4 file. The definition of aclocal is: "aclocal-create
Aclocal. M4 by scanning Configure. ac ".
4. Autoconf
Autoconf is used to generate the configure file. Configure is a script that can be set
The source program can adapt to different operating system platforms and generate appropriate makefiles based on different systems.
Your source code can be compiled on different operating system platforms.
The content of the configure. In file is a number of macros. These macros will become the checking system after being processed by Autoconf.
Features. environment variables. The macro sequence in the shell script. Configure. In file of the required software parameters is not
There are rules, but you must add the ac_init macro and ac_output macro at the beginning and end of all macros.
In Configure. ini:
# Indicates the comment, and the content after this macro will be ignored.
Ac_init (file)
This macro is used to check the path of the source code.
Am_init_automake (package, Version)
This macro is required. It describes the name and version number of the software package to be generated. package is a software package.
Version is the version number. When you use the make Dist command, it will generate
The software release package of the helloworld-1.0.tar.gz, where the corresponding package name and version number.
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 to be output.
We actually need some other macros when using automake, but we can use aclocal to help
We will automatically generate the aclocal. M4 file after executing aclocal.
After two macro files Configure. In And aclocal. M4 are generated, we can use autocon.
F to generate the configure file.
5. makefile. AM
Makefile. Am is used to generate makefile. in. You need to manually write. makefile.
Am defines some content:
Automake_options
This is the automake option. When you execute automake, it will check whether there is a standard in the directory
Various files in the GNU software package, such as authors. changelog. News.
When we set it to foreign, automake will use the standard of the general software package to check.
Bin_programs
This is the name of the executable file to be generated. If you want to generate multiple executable files,
Separate the names with spaces.
Helloworld_sources
This is the source code required to generate "helloworld". If multiple source files are used,
Use space characters to separate them. For example, if helloworld. h and helloworld. C are required, write them as follows:
Helloworld_sources = helloworld. h helloworld. C.
If you have defined multiple executable files in bin_programs, each executable file must be defined
Filename_sources.
6. automake
We use automake -- add-missing to generate makefile. In.
Option -- add-missing is defined as "add missing standard files
To package ", which will add automake to some files required by a standard software package.
The makefile. In file generated by automake complies with the GNU makefile convention.
Next, we only need to execute the configure shell script to generate a suitable MAKEFILE file.
.
7. makefile
Makefile conforming to the GNU makefiel Convention contains some basic pre-defined operations:

Make
Compile the source code based on makefile, connect to generate the target file, and execute the file.
Make clean
Clear the object files generated by the last make command (Files suffixed with ". O") and executable files.
Make install
Install the compiled executable file to the system directory, which is generally the/usr/local/bin directory.
Make Dist
Generate the release package file (distribution Package). This command will run the executable file and related
Package the file into a tar.gz compressed file to serve as the software package for release.
It will generate a file named "PACKAGE-VERSION.tar.gz" under the current directory. Pa
Ckage and version are the am_init_autom defined in Configure. In.
Ake (package, version ).
Make distcheck
Generate and test the release package to confirm the correctness of the release package.

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.