Linux Learning Notes (i) using Automake to generate makefile

Source: Internet
Author: User
Tags manual writing perl script automake

As a Linux program developers, we must have met makefile, with make command to compile their own written program is really convenient. In general, it is not easy to write a simple makefile by hand, if you want to write a makefile that conforms to the free software practice.

In this article, we will show you how to use the autoconf and automake two tools to help us automatically generate makefile that conform to free software practices, so that you can use the "./configure", "make", as well as the common GNU program, " Make instal "will be able to install the program into the Linux system. This will be especially useful for developers who want to do open source software, or if you just write your own little toy program, then this article will help you a lot.

   I. Introduction of Makefile

Makefile is used to automatically compile and link, a project has a lot of files, each file changes will lead to the project re-link, but not all the files need to be recompiled, makefile records the information of the file, When make determines which files need to be recompiled at the time of the link.

The purpose of makefile is to let the compiler know what other files you need to rely on to compile a file. When those dependent files are changed, the compiler automatically discovers that the final makefile is obsolete and compiles the appropriate modules.

The basic structure of makefile is not very complex, but when a program developer begins to write makefile, he often suspects that his writing is customary, and that his own makefile is often associated with his own development environment, and when the system environment variable or path has changed, Makefile may also have to follow the changes. This creates a lot of problems with manual writing makefile, Automake just helps us to solve these problems.

With Automake, the program developer only needs to write some simple files containing predefined macros, which are generated configure by autoconf based on a macro file. Generate makefile.in from another macro file by Automake, and then use configure to generate a custom makefile based on makefile.in. Below we will describe in detail the Automake generation method of makefile.

   second, the use of the environment

The program mentioned in this article is based on the Linux distribution: Fedora Core Release 1, which contains the autoconf,automake we want to use.

   third, starting from 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:

Helloworld.c
Configure.in
makefile.am

Then execute:

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. Build 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:

$ mkdir Helloword
$ CD HelloWorld


2, HELLOWORLD.C

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

int main (int argc, char** argv)
{
printf ("Hello, Linux world!\n");
return 0;
}


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:

$ autoscan
$ ls
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:

============================configure.in Content starts =========================================
#-*-Autoconf-*-
# Process This file with autoconf to produce a configure script.

Ac_init (HELLOWORLD.C)
Am_init_automake (HelloWorld, 1.0)

# 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 (Makefile)
============================configure.in Content End =========================================


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

$ aclocal
$ls
ACLOCAL.M4 configure.in HELLOWORLD.C
$ autoconf
$ ls
ACLOCAL.M4 Autom4te.cache Configure configure.in HELLOWORLD.C


As you can see, configure.in content is a macro definition that, after autoconf processing, becomes a shell script that examines system features, environment variables, and the parameters required by the software.

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 way 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 configure.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 while expanding the macro. A macro can be either inline or user-defined. In addition to the ability to expand macros, M4 also has built-in functions for referencing files, executing commands, integer arithmetic, text manipulation, looping, and so on. M4 can be either the front end of the compiler or a single macro processor.

4. New makefile.am

To create a new makefile.am file, command:

$ VI makefile.am


The contents are as follows:

Automake_options=foreign
Bin_programs=helloworld
Helloworld_sources=helloworld.c


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

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. Running Automake

Command:

$ automake--add-missing
Configure.in:installing './install-sh '
Configure.in:installing './mkinstalldirs '
Configure.in:installing './missing '
Makefile.am:installing './depcomp '


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

6, execute configure generate makefile

$./configure 
Checking for a bsd-compatible install .../usr/bin/install-c
checking whethe R build Environment is sane ... yes
checking for gawk ... gawk
Checking whether make sets $ (make) ... yes
checking f or gcc ... gcc
checking for C compiler default output ... a.out
checking whether the C compiler works ... yes
Checki Ng whether we is cross compiling ... no
checking for suffix of executables... 
checking for suffix of object fi Les ... o
checking whether we are using 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 Makefile
config.status:executing depfiles commands
$ ls-l Makefile
-rw-rw-r--1 yutao yutao 15035 Oct 15 10:40 Makefile


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

7. Compiling code with Makefile

$ make
If gcc-dpackage_name=\ "\"-dpackage_tarname=\ "\"-dpackage_version=\ "\"-

Dpackage_string=\ "\"-dpackage_bugreport=\ "\"-dpackage=\ "helloworld\"-dversion=\ "1.0\"

-I.-I.-G-O2-MT HELLOWORLD.O-MD-MP-MF ". Deps/helloworld. Tpo "\
-c-o helloworld.o ' test-f ' helloworld.c ' | | echo './' helloworld.c; \
Then Mv-f ". Deps/helloworld. Tpo "". Deps/helloworld. Po "; \
Else Rm-f ". Deps/helloworld. Tpo "; Exit 1; \
Fi
Gcc-g-o2-o HelloWorld HELLOWORLD.O


Run HelloWorld

$./helloworld
Hello, Linux world!

Four, in the light of the

for each of the commands mentioned above, we will do some more detailed introduction.

1, AutoScan

AutoScan is used to scan the source code directory for generating Configure.scan files. AutoScan can use directory names as parameters, but if you do not use parameters, then AutoScan will assume that the current directory is being used. AutoScan will scan the source files in the directory you have specified and create a Configure.scan file. The

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 script. Aclocal automatically generates ACLOCAL.M4 files based on the contents of the Configure.in file. The definition of aclocal is: "Aclocal-create aclocal.m4 by scanning configure.ac".

4, autoconf

autoconf are 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, so that your source code can be compiled on different operating system platforms. The contents of the

Configure.in file are macros that, when processed autoconf, become shell scripts that examine system characteristics, environment variables, and the parameters that the software must have. The order of macros in the configure.in file is not specified, but you must add Ac_init macros and Ac_output macros to the front and last sides of all macros.

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 resides.

Am_init_automake (Package, VERSION)


This macro is required, and it describes the name of the package we are going to generate and its version number: The package is the name of the bundle and version is the revision number. When you use the Make Dist command, it will give you a helloworld-1.0.tar.gz-like software release package 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.

When we use automake, we actually need to use some other macros, but we can use aclocal to help us generate them automatically. After executing the aclocal we will get the 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 executing Automake, it checks the directory for the existence of various files in the standard GNU software package, such as authors, ChangeLog, news, and so on. When we set it to foreign, Automake will use the standard of the generic package to check.

Bin_programs

This is the file name that specifies the executable file that we want to produce. If you want to produce more than one executable file, separate the names with a space between them.

Helloworld_sources

This is the source code that is required to specify the "HelloWorld" to be generated. If it uses more than one source file, separate them with a space number. 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, Makefile

In makefile, which complies with the GNU Makefiel Convention, contains some basic pre-defined actions:

Make

According to makefile compile source code, connect, generate target file, executable file.

Make clean

Clears the object file (the file with the suffix ". O") and the executable file that resulted from the last make command.

Make install

Installs the successfully compiled executable file into the system directory, typically the/usr/local/bin directory.

Make Dist

Generates a release package file (that is, distribution packages). This command will package the executable and related files into a tar.gz compressed file to be used as the software package for the release.

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 package. This will automatically unpack the package file, execute the Configure command, and execute make to confirm that the compilation does not appear to be wrong, and that the package is ready to be released.

===============================================
Helloworld-1.0.tar.gz is ready for distribution
===============================================
Make Distclean


Like make clean, but it also removes all configure generated files, including makefile.

  V. Concluding remarks

With the introduction above, you should be able to easily generate your own makefile files and corresponding project files that conform to the GNU Convention.

If you want to write more complex and customary makefile, you can refer to the configure.in and makefile.am files in some open code projects, such as: Embedded database SQLite, unit test Cppunit.

Linux Learning Notes (i) using Automake to generate makefile

Related Article

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.