Originally from: http://www.laruence.com/2009/11/18/1154.html
As a Linux program developers, we must have met makefile, with make command to compile their own written program is really very convenient. In general, everyone is writing a simple makefile by hand, It's not that easy to write a makefile that conforms to free software practices.
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. Makefile INTRODUCTION
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 in the file, 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 are needed to compile a file. When those dependent files change, 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, The makefile may have to be modified as well. This creates a lot of problems with hand-written makefile, and automake happens to be a good way to help us solve these problems.
With Automake, the program developer only needs to write some simple files containing predefined macros, generated by autoconf based on a macro file configure, makefile.in from another macro file by Automake, Then use configure to generate a customary Makefile based on makefile.in. The Automake generation method for Makefile is described in detail below.
Two. 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.
Three. Starting from 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. inch makefile.am
Then execute:
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:
mkdir helloword$ 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.
#include <stdio.h>int main (intChar* * argv) { printf ("%s "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:
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 ThisfileWith autoconf to produce a configure script. Ac_init (HELLOWORLD.C) am_init_automake (HelloWorld,1.0) # Checks forprograms. ac_prog_cc# Checks forlibraries.# Checks forHeader files.# Checks fortypedefs, structures, and compiler characteristics.# Checks forLibrary 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. inch lsaclocal.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:
VI makefile.am content is as follows: automake_options=foreignbin_programs=helloworldhelloworld_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:
$ automake--add-missingconfigure. 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. Perform Configure build Makefile
$./configurechecking forA bsd-compatibleInstall.../usr/bin/Install-cchecking Whether build environment is sane ... yeschecking for gawk...gawkchecking whether Makesets $ (make) ... yeschecking for GCC...GCCchecking forC compiler Default output ... a.outchecking whether the C compiler works ... yeschecking whether we are cross compiling . .. nochecking forsuffix of executables...checking forSuffix ofObjectfiles ... ochecking whether we are using the GNU C compiler ... yeschecking whetherGCCAccepts-g ... yeschecking for GCCoption to accept ANSI C ... none neededchecking forStyle of include used by Make... Gnuchecking dependency Style ofGCC... gcc3configure:creating ./config.statusconfig.status:creating Makefileconfig.status:executing depfiles commands$ls-L Makefile-rw-rw-r--1Yutao Yutao15035Oct the Ten: +Makefile
As you can see, at this point the makefile has been produced.
7. Compiling code with Makefile
$ Makeif 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-O2-MT HELLOWORLD.O-MD-MP-MF". Deps/helloworld. Tpo"-c-o helloworld.o helloworld.c; Then MV-F". Deps/helloworld. Tpo" ". Deps/helloworld. Po";Else RM-F". Deps/helloworld. Tpo"; Exit1;fiGCC-g-o2-o HelloWorld HELLOWORLD.O
Run HelloWorld
$./Helloworldhello, Linuxworld!
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.
1the. AutoScan AutoScan is used to scan the source code directory for generating configure.scan files. AutoScan can be used 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. 2The . Configure.scan Configure.scan contains the basic options for system configuration, which are all macro definitions. We need to rename it to configure.inch 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-createACLOCAL.M4 by scanning Configure.ac". 4the. 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 similar helloworld for you-1.0.TarThe . GZ software release package, which has 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 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 designation of the resulting"HelloWorld"if it uses multiple source files, separate them with a space number. For example, if you need helloworld.h,helloworld.c then 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 produce makefile.inch. OptionsThe definition of--add-missing is"add missing standard filesto the package", it will allow Automake to include some of the files necessary for a standard package.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 contains some basic pre-defined actions in Makefile that conform to the GNU Makefiel Convention: Makeaccording to makefile compile source code, connect, generate target file, executable file. Makeclean clears the object file generated by the last make command (suffix". O"file) and the executable file. Make Installinstalls the successful executable file to the system directory, typically/usr/local/Bin directory. MakeDist 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 will generate a name similar to the one in the current directory"package-version.tar.gz"the file. Package and version are the Am_init_automake (package, version) that we defined in configure.in. MakeDistcheck generates and tests the release package to determine the correctness of the release package.
automake,autoconf use of the detailed