This document describes how to use autoconf‑automaketo produce software released in the form of source code (.tar.gz) and use custom parameters when performing configure.
I. Overview and basic knowledge
In linux, we can use a package (in the format of .tar.gzor .tar.bz2) in the form of source code.
./Confiugure, make, make
Install to compile and install. When running./configure, you can add different parameters as needed (available./configure ).
-- Help to view the parameter table ).
First, let's talk about what will be generated after execution of./configure? After running, the system will generate config. h and multiple makefiles based on your actual situation. Where
Makefile is the template used to run make. config. h records user-defined parameters in the form of a macro (Marco). The compiler can use config. h
Pre-compile the source code to generate a personalized execution file.
2. Our "Software"
Now we can design our own "software". In order to be more practical, we will use multiple source programs. First, we will create a directory tt to store our things, then create a src directory under tt. Generally, the source code is put in src (it seems to have become an unwritten rule: P ). The overall architecture is as follows:
<Tt>
|-Configure. in
|-Makefile. am
|-Acconfig. h
|-<Src>
|-Tt. c
|-Qq. c
|-Qq. h
|-Makefile. am
※Description:
1. configure. in is the most important document and is dominated by it throughout the installation process.
2. makefile. am automake will generate Makefile based on it. in, and then. /configure Makefile. in is the final Makefile. in general, there should be a Makefile in the top-level directory and each subdirectory. am
3. acconfig. h autoheader will generate config. h. in based on it, and then change config. h. in to the final config. h by./configure.
4. tt. c qq. c qq. h. This is our source program.
※Source code content:
Tt. c
# Include <stdio. h>
# Include <qq. h>
# Ifdef HAVE_CONFIG_H
# Include <config. h>
# Endif
Int main (void)
{
Int a = 23;
Printf ("Hello, I am teacher (% d), pls tell me your names! /N ", );
# Ifdef POPO
Printf ("My name is PoPo! /N ");
# Endif
Qq ();
Return 0;
}
Qq. c
# Include <stdio. h>
# Include <qq. h>
# Ifdef HAVE_CONFIG_H
# Include <config. h>
# Endif
Void qq (void)
{
Printf ("My name is QQ/n ");
# Ifdef POPO
Printf ("QQ: Hey PoPo, long time no see./n ");
# Endif
}
Qq. h
# Ifndef _ QQ __
# Define _ QQ __
Void qq (void );
# Endif
※Running process:
1. First, let the teacher name the name.
2. If PoPo is available, its name will be reported.
3. Then it's QQ's turn to report. If PoPo comes, QQ will say hello to PoPo.
Obviously, it is easy to see whether PoPo is present depends on whether the Macro (Macro) of POPO is defined. We only need to decide whether to define it before compilation to achieve different effects.
If config. h exists, Makefile will pass the macro HAVE_CONFIG_H to the compiler during compilation. If HAVE_CONFIG_H is not defined, our program should not include config. h.
3. Production Process
Perform the following steps:
Step 1 Write configure. in
Generate configure. there are two in methods. One is to write data from scratch, and the other is to use autoscan. After autoscan is executed, configure is generated. scan, which contains some template content. You only need to change the name. in.
There are two types of commands used in configure. in: one is started with AC, which indicates that it is provided by autoconf, and the other is started with AM, which indicates that it is provided by automake.
In configure. in, we can perform a lot of detection operations, such as checking the program, header file, library, and so on required for compilation. in short, the function is very powerful, but here we only detect the compiler and header file, for detailed usage, see GNU Manuals Online
Behavior annotation line (green part in code) headed by "dnl ).
Configure. in
Dnl initializes autoconf. The parameter is the file where the entry function is located.
AC_INIT (src/tt. c)
Dnl initializes automake. The parameter is the software name and version number.
AM_INIT_AUTOMAKE (tt, 0.1.0)
Dnl tells automake that the configuration file we use is usually config. h.
AM_CONFIG_HEADER (config. h)
Dnl here is the part for implementing custom parameters. See the following description.
AC_ARG_ENABLE (popo, AS_HELP_STRING ([-- enable-popo], [PoPo is present]), enable_popo = no)
If test "$ enable_popo" = yes; then
Echo "PoPo is here! "
AC_DEFINE (POPO)
Else
Echo "PoPo isn' t here! "
Fi
Dnl detection Compiler
AC_PROG_CC
Dnl detects Standard C header files
AC_HEADER_STDC
Dnl output file. In general, the top-level directory and each subdirectory should have Makefile output.
AC_OUTPUT (Makefile src/Makefile)
. /Configure has two types of custom parameters, one is the switch type (-- enable-XXX or -- disable-XXX), and the other is open, enter a string of characters (-- with-XXX = yyyy.
The preceding Code uses the switch mode. The first parameter is the parameter name, the second parameter is the description (the content displayed after "./configure -- help" is executed), and the last parameter is the default value. In general, the default value and user prompt should be mutually exclusive, that is, if the default value is no, the user should be prompted to use enable for modification, and vice versa.
From the code above, we can see that if $ enable_popo is yes, we will use AC_DEFINE to define the Macro (Macro) of POPO. Otherwise, we will not define the Macro we use here, it must be in acconfig. h.
Step 2 run aclocal and run aclocal In the tt directory to generate aclocal. m4.
Step 3 Write acconfig. h
Macro (Macro) used in configure. in should be declared in this file. It is generally declared using # undef.
Acconfig. h
# Undef POPO
Step 4 run autoheader
After running autoheader, config. in, acconfig. h, and the default acconfig. h are used to generate config. h. in.
Step 5 Write Makefile. am
In general, there should be a Makefile. am in both the top-level directory and each subdirectory.
Makefile
AUTOMAKE_OPTIONS = foreign
SUBDIRS = src
The first line is to tell automake not to check whether files such as AUTHORS and README exist in the directory.
The second line tells automake to process the src subdirectory.
Src/Makefile
AUTOMAKE_OPTIONS = foreign
Bin_PROGRAMS = tt
Tt_SOURCES = tt. c qq. c qq. h
The first line serves the same purpose as the previous line.
The second row is the name of the target execution file.
The third line is to generate the names of all source programs and header files required for the tt execution file.
Step 6 run automake
Then you can run automake and enter
Automake-a and
Automake-a src/Makefile
When "automake-a" or "automake -- add-missing" is used, files such as install. sh and mkinstalldirs are automatically filled up. Otherwise, errors will occur. Remember!
Step 7 run autoconf
Finally, you can execute autoconf. The final configure will be generated after completion!
Iv. Compilation and Testing
Compile with default values:
[Root @ chiosoft tt] #./configure
Checking ......
PoPo isn' t here!
Checking ......
[Root @ chiosoft tt] # make
......
[Root @ chiosoft tt] # src/tt
Hello, I am teacher (23), pls tell me your names!
My name is QQ
By default, the macro POPO is not defined, so "PoPo isn' t here is output in./configure! ", Only QQ is reported during operation.
Let's take a look at this:
[Root @ chiosoft tt] #./configure -- help
......
-- Enable and -- with options recognized:
-- Enable-popo PoPo is present
[Root @ chiosoft tt] #./configure -- enable-popo
Checking ......
PoPo is here!
Checking ......
[Root @ chiosoft tt] # make
......
[Root @ chiosoft tt] # src/tt
Hello, I am teacher (23), pls tell me your names!
My name is PoPo!
My name is QQ
QQ: Hey PoPo, long time no see.
The output "PoPo is here!" is displayed in./configure! ", The execution results are completely different!
In addition, we can also use make install to install it. The default setting is to/usr/local/bin. Of course, these can be modified.
5. Generate the release package tarball
Well, so far, our small software has been tested and can be released. There are many files in tt, some of which are written by ourselves, and some are temporary files generated during compilation, what are the requirements?
What if I want to package it into the release package? Of course, you can select files by yourself, but the Makefile generated by automake provides several very convenient functions for us.
We can use make dist or make distcheck to generate the corresponding tarball. The latter will also help us test whether the release package works normally. Therefore, we recommend using make distcheck.
See it? The tt-0.1.0.tar.gz package has been put in tt. Are you aware that the software name and version number used here are the two parameters in configure. in AM_INIT_AUTOMAKE! Now you can try to unzip and install it.