Turn: Http://my.oschina.net/qihh/blog/66113?fromerr=6ej3CfGJ Writing makefile files is a very troublesome matter for a beginner or a veteran Linux programmer; Developers should focus on programming code, and it is obviously unwise to spend too much effort on makefile files, and for different processor architectures, the compiler is different, the environment is different, especially the compilation of programs in some embedded systems, So the transplant problem also makes the makefile file writing tend to complicate, also appears this question is very important. Linux experts have already thought about these issues, so they have developed some tools that automatically generate makefile files. They are the following tools:
〉gnu Automake
〉gnu Autoconf
〉GNU M4
〉perl
〉gnu Libtool
Therefore, your operating system must install the above software, otherwise it will not be able to automatically generate makefile files or in the process of generating a variety of problems. Use the Autoconf/automake/autoheader tool to handle a variety of portability issues, use these tools to complete system configuration information collection, production makefile files. Then, when you intend to compile the source code, you only need to pass "./configure; Make "This simple command will get a neat compilation.
1.autoscan (autoconf): Scan the source code to search for common portability issues, such as checking compilers, libraries, header files, etc., generating file Configure.scan, which is a prototype of CONFIGURE.AC.
Your source files---[autoscan*]--[Configure.scan]--configure.ac
2.aclocal (Automake): Depending on the macros you have installed, macros in user-defined macros and ACINCLUDE.M4 files are defined in the file aclocal.m4 by the macro set configure.ac the file. Aclocal is a Perl script that is defined as: "Aclocal-create aclocal.m4 by scanning configure.ac"
user input files optional input process output files================ ============== ======= ============ ACINCLUDE.M4-----. V.-------, CONFIGURE.AC------------------------>|aclocal| {User macro Files}->| |------> ACLOCAL.M4 '-------3.autoheader (autoconf): Based on some macros in configure.ac, such as the CPP macro definition, Run M4, claiming config.h.inuser input files optional input process output files================ ============== ======= ============ ACLOCAL.M4-------. | V.----------, configure.ac----------------------->|autoheader|----> AutoConfig. H.in '----------'
4.automake:automake establishes the structure defined in makefile.am makefile.in, and then configure the script to convert the resulting makefile.in file to makefile. If you define some special macros in Configure.ac, such as Ac_prog_libtool, it will call libtoolize, otherwise it will generate config.guess and config.sub itself
User input files Optional input processes output files================ ============== ========= ============ .--------, | | --COPYING | | --INSTALL | |------> Install-sh | |------> Missing |automake|------> Mkinstalldirsconfigure.ac------------------- ---->| | makefile.am----------------------->| |------> Makefile.in | |------> stamp-h.in---+ | --Config.guess | | | --Config.sub | '------+-' | | ----config.guess |libtoolize| ---Config.sub | |--------> Ltmain.sh | |--------> Ltconfig '----------'
5.AUTOCONF: Expand the macro in CONFIGURE.AC to generate the Configure script. This process may need to use the macros defined in ACLOCAL.M4.
User input files optional input processes output files================ ============== ======== = ============ACLOCAL.M4, autoconfig.h.in--------. V. --------, configure.ac----------------------->|autoconf|------> Configure
6. The process of/configure
.-------------> [Config.cache] configure*--------------------------+-------------> Config.log | [Config.h.in]-. V .--> [autoconfig.h] +-------> config.status*-+ makefile.in---"-- Makefile
7. Make process
[Autoconfig.h]-. +--> make*---> program Makefile ---'
.---------, Config.site--->| | Config.cache--->|Configure| --Config.cache | +-, '-+-------' | | |----> Config.status config.h.in------->|config-|----> Config.h MAKEFILE.I n------->| . status|----> Makefile | |----> Stamp-h | +--, .-+ | | | '------+--' | ltmain.sh------->|ltconfig|-------> Libtool | | | '-+------' | |config.guess| | Config.sub | '------------'
.--------, Makefile------>| | config.h------>| Make | {Project Sources}---------------->| | --------> {Project targets} .-+ +--, | '--------' | | Libtool | | Missing | | Install-sh | | mkinstalldirs| '-------------'
Instance:
Create a hello.c file under the/hello/directory and compile and run it:
#cd/hello/
(1) Write the source file hello.c:
#include <stdio.h>
int main (int argc, char** argv)
{
printf ("Hello, gnu!n");
return 0;
}
[email protected] hello]$ LL
Total 4
-rw-rw-r--1 Litao Litao 12:02 hello.c
First, AutoScan
[Email protected] hello]$ AutoScan
Autom4te:configure.ac:no such file or directory
AutoScan:/usr/bin/autom4te failed with exit Status:1
[email protected] hello]$ LL
Total 8
-rw-rw-r--1 Litao Litao 0 12:03 Autoscan.log
-rw-rw-r--1 Litao Litao 457 12:03 Configure.scan
-rw-rw-r--1 Litao Litao 12:02 hello.c
The Configure.scan,autoscan.log file has been generated
Modify Configure.scan to Configure.in, and the last modification is as follows:
[Email protected] hello]$ MV Configure.scan configure.in
[Email protected] hello]$ vim configure.in
#-*-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 ([hello.c])
#AC_CONFIG_HEADER ([config.h])
Am_init_automake (Hello, 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)
Second, Acloacl
[Email protected] hello]$ aclocal
Generate Aclocal.m4 and Autom4te.cache (Configure.in is involved in generating aclocal.m4)
[email protected] hello]$ LL
Total 44
-rw-rw-r--1 Litao litao 31120 12:08 aclocal.m4
Drwxr-xr-x 2 Litao litao 4096 12:08 Autom4te.cache
-rw-rw-r--1 Litao Litao 0 12:03 Autoscan.log
-rw-rw-r--1 Litao Litao 496 12:08 configure.in
-rw-rw-r--1 Litao Litao 12:02 hello.c
Third, antoconf
[Email protected] hello]$ autoconf
Generate configure (based on configure.in, and ACLOCAL.M4)
[email protected] hello]$ LL
Total 168
-rw-rw-r--1 Litao litao 31120 12:08 aclocal.m4
Drwxr-xr-x 2 Litao litao 4096 12:11 Autom4te.cache
-rw-rw-r--1 Litao Litao 0 12:03 Autoscan.log
-rwxrwxr-x 1 Litao Litao 122297 12:11 Configure
-rw-rw-r--1 Litao Litao 496 12:08 configure.in
-rw-rw-r--1 Litao Litao 12:02 hello.c
Iv. Preparation of makefile.am:
automake_options= Foreign
bin_programs= Hello
hello_sources= hello.c
Wu, Automake
Generate Makefile.in, Depcomp, Install-sh, and missing (according to Makefile.am, and ACLOCAL.M4)
[Email protected] hello]$ Automake
configure.in:required file './install-sh ' not found
configure.in:required file './missing ' not found
Makefile.am:required file './depcomp ' not found
[Email protected] hello]$ Automake--add-missing
Configure.in:installing './install-sh '
Configure.in:installing './missing '
Makefile.am:installing './depcomp '
[email protected] hello]$ LL
Total 192
-rw-rw-r--1 Litao litao 31120 12:08 aclocal.m4
Drwxr-xr-x 2 Litao litao 4096 12:14 Autom4te.cache
-rw-rw-r--1 Litao Litao 0 12:03 Autoscan.log
-rwxrwxr-x 1 Litao Litao 122297 12:11 Configure
-rw-rw-r--1 Litao Litao 496 12:08 configure.in
lrwxrwxrwx 1 Litao Litao 12:16 depcomp
-rw-rw-r--1 Litao Litao 12:02 hello.c
lrwxrwxrwx 1 Litao Litao 12:16 install-sh
-rw-rw-r--1 Litao Litao 12:15 makefile.am
-rw-rw-r--1 Litao litao 16561 12:16 makefile.in
lrwxrwxrwx 1 Litao Litao 12:16 missing
Liu, configure
Generate Makefile, Config.log, and Config.status
Generate Makefile Entire process
Configure.in:
The contents of the Configure.in file are a series of GNU M4 macros that, when processed autoconf, become shell scripts that examine the system's characteristics. The order of Configure.in macros is not specified, but each configure.in file must include a Ac_init macro in front of all macros and then add Ac_output macros at the end of all macros. You can first scan the original file with AutoScan to produce a Configure.scan file, and then make some changes to the Configure.scan configure.in file. The macros used in the example are as follows:
Dnl
The word behind this macro is not processed and can be treated as a comment
Ac_init (FILE)
This macro is used to check the path where the source code is located, and AutoScan is automatically generated, usually without modifying it.
Am_init_automake (package,version)
This is the necessary macro to use Automake, the package is the name of the software suite to generate, and version is the revision number.
Ac_prog_cc
Check the C compiler available to the system, if the source code is written in C, you need this macro.
Ac_output (FILE)
Set the Configure to produce the file, if Makefile, configure will be checked out of the results into the makefile.in file to produce the appropriate Makefile.
In fact, when using automake here, we also need some other macros, which we use aclocal to help produce. Execute aclocal will produce ACLOCAL.M4 file, if no special purpose, can not need to modify it, with aclocal generated macro will tell automake how to action.
With the configure.in and aclocal.m4 two files, you can execute the autoconf to generate the Configure file.
makefile.am
Automake will turn makefile.am into makefile.in file according to the macro in configure.in. The makefile.am file defines the target to be produced:
Automake_options
Sets the options for Automake.
Automake is mainly to help develop GNU software personnel to maintain the software, so when performing automake, it will check the directory under the standard GNU software should have files, such as ' NEWS ', ' AUTHOR ', ' ChangeLog ' and other documents. When setting up foreign, Automake will use the standard of general software to check.
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 blank character.
Hello_sources
Define ' Hello ' as the original file required for the execution of the program. If the ' Hello ' program is generated by a number of original files, it must be listed with all the original files it uses, separated by whitespace characters. Suppose ' hello ' also requires ' hello.c ', ' main.c ', ' hello.h ' three files, then define
hello_sources= hello.c main.c hello.h
If more than one execution file is defined, a relative filename_sources is defined for each executor.
Edit good makefile.am file, you can use Automake--add-missing to produce makefile.in. Add the--add-missing option to tell Automake by the way if a file is required to package a software. The makefile.in file produced by Automake is fully compliant with the GNU Makefile practice, as long as the Configure Shell script is executed to produce the appropriate Makefile file.
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
The 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, 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 # sign indicates a comment, and the content behind the 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 so 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.
Configure,make,make Install detailed