Transferred from: http://hi.baidu.com/liuyanqiong/blog/item/0a6f0ad9d28e1d3d32fa1c7b.html
As a Linux program developers, must have encountered makefile, with make command to compile their own written program is indeed very 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'll 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", and "makes" as you would a common GNU program. Instal "You can install the program into a 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:
New three files: helloworld.c configure.in makefile.am
Then execute: autoscan; 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!");
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.
4, Generate configure.in
Now rename Configure.scan to Configure.in, and edit it to modify it to remove extraneous statements as follows:
Code
============================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 =========================================
5 Performing aclocal and autoconf
Then execute the commands aclocal and autoconf, respectively, will produce ACLOCAL.M4 and configure two files:
Code
$ 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.
6. New makefile.am
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.
7. Running Automake
Command:
Code
$ 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.
8, execute configure generate makefile
Code
$./configure
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
Checking for C compiler default output 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 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 10:40 Makefile
As you can see, at this point the makefile has been produced.
9. Compiling code with Makefile
Code
$ 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
10, run HelloWorld
Code
$./helloworld
Hello, Linux world!
This HelloWorld is compiled, if you follow the above steps to do, it should also be easy to compile the correct HelloWorld file. You can also try using some other make commands, such as make Clean,make Install,make Dist, to see what effects they will give you. How do you feel? I can write such a professional makefile, the boss will be very impressed with you.
Iv. in a comprehensible and understandable
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 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.
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. The definition of aclocal is: "Aclocal-create aclocal.m4 by scanning configure.ac".
4, autoconf
Use autoconf to generate configure files according to configure.in and ACLOCAL.M4. 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, after autoconf processing, become shell scripts that examine system features, 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 the Configure.ini:
#号表示注释, the content behind this macro will be 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 and version number of the packages we are going to generate: The package is the name of the packages, 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 produce. 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 to generate makefile.in according to Configure.in and makefile.am.
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 successful 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 generates and tests 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.
Make Distclean is similar to do clean, but it also removes all configure generated files, including makefile.
Five, Process diagram
Vi. 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.
Using Linux under Autoconf and Automake