As a program developer under Linux, everyone must have encountered Makefile, using the make command to compile the procedures you write, is really very convenient. In general, everyone is hand-written a simple makefile. If you want to write Makefile that is conforming to free software practices, it is not so easy.
In this article, you will give you how to use AutoConf and Automake tools to help us automatically generate Makefile that meets free software practices, so you can use "./configure" ,"make" like a common GNU program. "Make INSTAL" can be installed to the Linux system. This will be particularly suitable for program developers who want to open source software, or if you just write some small Toy programs, then this article will also have a lot of help.
First, Makefile Introduction
Makefile is used for automatic compilation and link, a multi-file consisting of a multi-file, each file change will cause a re-link of the project, but not all files need to be recompiled, Makefile records file information, at MAKE Will decide which files need to be recompiled when link.
Makefile's purpose is to let the compiler know which files need to be used to compile a file. When those dependent files have changed, the compiler will automatically discover the final generated files that are outdated, and recompile the corresponding modules.
The basic structure of makefile is not very complicated, but when a program developer starts writing makefile, it is often doubt whether it is conforming to conventions, and written Makefile often associates its own development environment, when the system environment variable or path occurs After changing, Makefile may still be modified. This causes many problems with manually writing makefile, and Automake can help us solve these problems well.
Using Automake, program developers only need to write some files with a predefined macro, generate configure by AutoConf, generate configure according to another macro file, generate makefile.in according to another macro file, and use configure based on makefile.in to generate one Makefile in conformity. Here we will detail the Automake generating method of Makefile.
Second, the environment used
The procedures mentioned in this article are based on Linux release: Fedora Core Release 1, which contains AutoConf, Automake we want to use.
Third, start with HelloWorld
We started from everyone's most commonly used example helloWorld.
The following procedure is simply:
New three files:
HelloWorld.c
Configure.in
Makefile.am
Then execute:
Aclocal; AutoConf; Automake --Add-missing ;./helloworld
You can see that makefile is generated, and helloWorld.c can be compiled.
Very simple, a few commands can make a conformance Makefile, how is it feeling?
Now introduce the detailed process:
1, build a directory
Built a helloWorld directory in your working directory, we use it to store the HelloWorld program and related files, such as under / home / my / build:
$ MKDIR HELLOWORD $ CD HelloWorld
2, HelloWorld.c
Then write a hellowrold.c file with your own favorite editor, such as command: vi helloworld.c. Use the following code as the content of HelloWorld.c. INT Main (int Argc, char ** argv) {printf ("Hello, Linux World! / N"); Return 0;}
Save and exit after completion.
Now there should be a HelloWorld.c you write in the HelloWorld directory.
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 $ Lsconfigure.scan HelloWorld.c
After execution, a file will be generated in the HelloWrold directory: configure.scan, we can take it as a Bluena of Configure.in.
Now rename Configure.scan as configure.in, and edit it, press the contents below to modify it, remove unrelated statements:
============================ CONFIGURE.IN content start ========================================================================================================================================== ========================= # - * - AutoConf - * - # process this file with autoc 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, which will generate both ACLOCAL.M4 and CONFIGURE:
$ ACLOCAL $ LS ACLOCAL.M4 Configure.in HelloWorld.c $ AutoConf $ LS ACLOCAL.M4 Autom4te.Cache Configure Configure.in HelloWorld.c You can see configure.in content is some macro definition, these macros will be processed after AutoConf The shell script is turned into the inspection system characteristics, environment variables, and software must have a shell script.
AutoConf is a tool for generating automatic configuring software source code scripts (CONFIGURE). The Configure script can operate independently of AutoConf and do not require user intervention during operation.
To generate a configure file, you must tell AutoConf how to find the macro you used. The way is to generate your ACLocal.m4 using the ACLOCAL program.
Aclocal automatically generates ACLOCAL.M4 files according to the content of the Configure.in file. ACLOCAL is a Perl script, its definition is: "ACLOCAL - CREATE ACLOCAL.M4 BY Scanning Configure.ac".
AutoCONF Create Configure from the template file that requires various parameters when the Configure.in is enumerated.
AutoConf requires a GNU M4 macro processor to process aclocal.m4 to generate configure scripts.
M4 is a macro processor. The input is copied to the output while the macro is displayed. Macros can be embedded or defined by users. In addition to the expansion macro, M4 has some built-in functions, used to reference files, execute commands, integer operations, text operations, cycles, etc. M4 can be used as the front end of the compiler, or may be separately as a macro processor.
4, new Makefile.am
Create a new makefile.am file, command:
$ vi makefile.am
The content is as follows:
Automake_options = foreignbin_programs = HelloWorldHelloWorld_Sources = HelloWorld.c
Automake will generate makefile.in based on your Makefile.am.
The macros defined in makefile.am will guide Automake to generate the specified code. For example, macro bin_programs will result in the target of compilation and connection.
5, run automake
command:
$ Automake --Add-MissingConfigure.in: Installing `./install-sh'configure.in: Installing`/mkinstallDirs'Configure.in: installing` ./missing'makefile.am: installing `/depcomp '
Automake will generate some files according to the Makefile.am file, which contains the most important makefile.in.
6. Execute Configure to generate makefile
$ ./Configure checking for a BSD-compatible install ... / usr / bin / install -cchecking whether build environment is sane ... yeschecking for gawk ... gawkchecking whether make sets $ (MAKE) ... yeschecking for gcc ... gccchecking for C compiler default output ... a.outchecking whether the C compiler works ... yeschecking whether we are cross compiling ... nochecking for suffix of executables ... checking for suffix of object files ... ochecking whether we are using the GNU C compiler ... yeschecking whether gcc accepts -g ... yeschecking for gcc option to accept ANSI C ... none neededchecking for style of include used by make ... GNUchecking dependency style of gcc .. . gcc3configure: creating ./config.statusconfig.status: creating Makefileconfig.status: executing depfiles commands $ ls -l Makefile-rw-rw-r-- 1 yutao yutao 15035 Oct 15 10:40 Makefile you can see, at this time Makefile has been generated.
7, compile code using Makefile
$ makeif gcc -dpackage_name = / "/" -dpackage_tarname = / "/" -dpackage_string = / "/" -dpackage_bugReport = / "/" -dpackage = / "HelloWorld /" -dversion = / "1.0 /" -i. -I. -G -O2 -mt helloworld.o -md -mp-mf ".deeps / helloworld.tpo" / -c-h 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 ; / figcc -g -o2 -o helloworld HelloWorld.o
Run HelloWorld
$ ./helloWorld Hello, Linux World!
In this way, helloWorld is compiled, if you do it on the above steps, you should also easily 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 the effect will give you. how do you feel? You can also write such a professional Makefile, and the boss will definitely look at you. Fourth, in-depth
We will introduce some detailed introductions for each of the orders mentioned above.
1, AutoScan
AutoScan is used to scan the source code directory to generate configure.scan files. AutoScan can be used as a parameter with a directory name, but if you don't use parameters, then AutoScan will consider the current directory. AutoScan will scan the source files in your specified directory and create a configure.scan file.
2, Configure.scan
Configure.scan contains basic options for system configuration, which are some macro definitions. We need to change it to configure.in
3, ACLOCAL
ACLOCAL is a Perl scriptor. Aclocal automatically generates ACLOCAL.M4 files according to the content of the Configure.in file. The definition of ACLOCAL is: "ACLOCAL - CREATE ACLOCAL.M4 BY Scanning Configure.ac".
4, AutoConf
AutoConf is used to generate configure files. Configure is a script that sets the source program to accommodate a variety of different operating system platforms, and generate suitable Makefile according to different systems, allowing your source code to be compiled on different operating system platforms.
The content of the Configure.in file is some macros, which will turn the shell script that checks system features, environment variables, and software must be inspected after AutoConf. The order in the configure.in file is not specified, but you must add the AC_INIT macro and the AC_OUTPUT macro at all and the last side of all macros.
In 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 is located.
AM_INIT_AUTOMAKE (Package, Version)
This macro is required, which describes the name of the package we will generate and its version number: package is the name of the package, and Version is the version number. When you use the make Dist command, it will generate a software issued package similar to HelloWorld-1.0.Tar.gz, which has the name and version number of the corresponding package.
AC_PROG_CC
This macro will check the C compiler used in the system.
AC_OUTPUT (File)
This macro is the name of Makefile we have to output.
When we use Automake, we can actually use some other macros, but we can use aclocal to help us automatically. We will get ACLOCAL.M4 files after executing Aclocal.
After generating configure.in and aclocal.m4 two macro files, we can use AutoConf to generate configure files.
5, Makefile.am
Makefile.am is used to generate makefile.in and need you to write. There are some contents defined in makefile.am:
Automake_Options
This is the option of Automake. When executing Automake, it checks if there is a variety of files that should have in the standard GNU package, such as authors, changelog, news, etc. When we set it into Foreign, Automake will check the standard for general packages. BIN_PROGRAMS
This is the file name that specifies the executable of the executable we have to produce. If you want to generate multiple executables, separated by spaces between each name.
HelloWorld_Sources
This is the source code required when "HelloWorld" is generated. If it uses multiple source files, then use space symbols to separate them. For example, helloworld.h, helloworld.c, then write it into helloworld_sources = helloworld.h helloworld.c.
If you define multiple executable files in bin_programs, each executable is defined to define a relative filename_sources.
6, Automake
We use Automake --Add-missing to generate makefile.in.
Option -Add-missing definition is "Add Missing Standard Files To Package", which will allow Automake to join some files necessary for a standard software.
The Makefile.in file we produced with Automake is in line with GNU Makefile practices, follows us as long as you perform the configure, you can generate the appropriate makefile file.
7, Makefile
In the Makefile that complies with GNU Makefiel practices, some basic pre-defined operations are included:
Make
Compile the source code, connection, generate the target file, and execute files according to Makefile.
Make clean
Clear the Object file generated by the last MAKE command (suffix ".o" file) and executable.
Make Install
Install the executable of the compiled success to the system directory, generally to / usr / local / bin directory.
Make dist
Generate a release package file (ie, Distribution Package). This command will pack the executable file and related files into a TAR.GZ compressed file used as a package of publishing software.
It will generate a name similar to "package-version.tar.gz" files in the current directory. Package and Version are am_init_automake (package, version) defined in configure.in.
Make disccheck
Generate a publish package and test it to determine the correctness of the release package. This action will automatically unlock the compressed package file, then execute the configure command, and execute make, confirm that the compilation does not have an error, and finally prompts your package ready to be released.
======================================================= HelloWorld-1.0 .tar.gz is ready for distribution =================================================================================================================================================================================== ===== made distclean is similar to Make Clean, but also deletes all the files generated by Configure, including Makefile.
Five, conclude
With the above introduction, you should easily generate a Makefile file that meets the GNU convention and the corresponding project file.
If you want to write more complex and conformable 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.