As a program developer in Linux, you must have encountered makefile. It is really convenient to compile your own programs by using the make command. Generally, you can write a simple makefile manually. If you want to write a makefile that complies with the Free Software conventions, it is not that easy.
In this article, we will introduce how to use Autoconf and automake tools to help us automatically generate makefiles that conform to the Free Software conventions, just like common GNU programs, you only need to use ". /configure "," make ", and" make install "can install the program in Linux. This will be especially suitable for developers who want to be open-source software, or if you just write some small toy programs by yourself, this article will be of great help to you.
To compile a simple source file main. C, you must automatically generate a makefile. The steps are as follows:
Step 1:
----------
Create a file main. c In the/root/project/main directory. Its content is as follows:
------------------------------------------------
# Include <stdio. h>
Int main (INT argc, char ** argv)
{
Printf ("Hello, auto makefile! /N ");
Return 0;
}
------------------------------------------------
The status is as follows:
[Root @ localhost main] #PWD
/Root/project/main
[Root @ localhost main] #Ls
Main. c
[Root @ localhost main] #
Step 2:
----------
RunAutoscanAutomatically create two files: autoscan. Log Configure. Scan
The status is as follows:
[Root @ localhost main] #Autoscan
[Root @ localhost main] #Ls
Autoscan. Log Configure. ScanMain. c
[Root @ localhost main] #
Step 3:
----------
Change Configure. Scan to configure. In.
ViewConfigure. InContent:
------------------------------------------------
#-*-Autoconf -*-
# Process this file with Autoconf to produce a configure script.
Ac_prereq (2.61)
Ac_init (full-package-name, version, bug-Report-address)
Ac_config_srcdir ([main. C])
Ac_config_header ([config. H])
# 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
------------------------------------------------
Interpret the above documents:
------------------------------------------------
#-*-Autoconf -*-
# Process this file with Autoconf to produce a configure script.
# Ac_prereq:
# Make sure that a new Autoconf version is used. If you want to create an Autoconf version for configure
# This is earlier than version, so an error message is printed in the standard error output and no configure is created.
Ac_prereq (2.61)
#
# Initialize and define the basic information of the software, including the full name of the Set package, the version number, and the email address required to report the bug
#
Ac_init (full-package-name, version, bug-Report-address)
#
# Used to detect the existence of the specified source code file to determine the validity of the source code directory
#
Ac_config_srcdir ([main. C])
#
# It is used to generate the config. h file for autoheader to use
#
Ac_config_header ([config. H])
# Checks for programs.
Ac_prog_cc
# Checks for libraries.
# Checks for header files.
# Checks for typedefs, structures, and compiler characteristics.
# Checks for library functions.
#
# Create an output file. Call this macro once at the end of 'configure. in.
#
Ac_output
------------------------------------------------
Modify action:
1. Modify the parameters in ac_init: ac_init (main, 1.0, pgpxc@163.com)
2. Add the macro am_init_automake, which is an essential macro for automake. Similarly, the package is the name of the software suite to be generated, and the version is the version number.
3. Add the output file makefile after ac_output.
Modified results:
------------------------------------------------
#-*-Autoconf -*-
# Process this file with Autoconf to produce a configure script.
Ac_prereq (2.61)
Ac_init (main, 1.0, pgpxc@163.com)
Ac_config_srcdir ([main. C])
Ac_config_header ([config. H])
Am_init_automake (main, 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])
------------------------------------------------
Step 4:
RunAclocal, Generate aAclocal. M4"File and a buffer folderAutom4te. CacheThis file mainly processes the macro definition of the local device.
The status is:
[Root @ localhost main] #Aclocal
[Root @ localhost main] #Ls
Aclocal. M4 autom4te. CacheAutoscan. Log Configure. In Configure. In ~ Main. c
[Root @ localhost main] #
Step 5:
RunAutoconfTo generate configure
The status is:
[Root @ localhost main] #Autoconf
[Root @ localhost main] #Ls
Aclocal. M4 autoscan. Log Configure. In Main. c
Autom4te. CacheConfigureConfigure. In ~
[Root @ localhost main] #
Step 6:
RunAutoheaderWhich generates the config. H. In file. This tool usually copies the user-attached symbol definitions from the "acconfig. H" file, so there is no additional symbol definition here, so you do not need to create the "acconfig. H" file.
The status is:
[Root @ localhost main] #Autoheader
[Root @ localhost main] #Ls
Aclocal. M4 autoscan. Log configure Configure. In ~
Autom4te. CacheConfig. H. InConfigure. In Main. c
[Root @ localhost main] #
Step 7:
The following is about to runAutomakeBut we should make some preparations before that!
First
CreateMakefile. AMThis step is an important step for creating makefile. The script configuration file used by automake is makefile. am. You need to create the corresponding file by yourself. Then, convert the automake tool to makefile. In.
The contents of makefile. Am are as follows:
------------------------------------------------
Automake_options = foreign
Bin_programs = Main
Main_sources = Main. c
------------------------------------------------
The following describes the corresponding items of the script file.
Automake_options indicates the option to set automake. GNU (introduced in chapter 1st) has strict regulations on software released by itself, for example, the license declaration file copying must be attached; otherwise, an error is reported during automake execution. Automake provides three software levels: Foreign, GNU, and gnits, which are used by users. The default level is GNU. In this example, the foreign level is used to detect only required files.
Bin_programs defines the name of the execution file to be generated. If multiple execution files are to be generated, each file name is separated by a space.
Main_sources defines the original file required by the execution program "Main. If the "Main" program is generated by multiple original files, all the original files used by the program must be listed and separated by spaces. For example, if the target body "main" requires "Main. c "," sunq. c "," Main. h ", The main_sources = Main. c sunq. C main. h. Note that if you want to define multiple execution files, you need to define the corresponding file_sources for each execution program.
Second
Use automake to generate the "Configure. In" file for it. Here, use the "-adding-missing" option to allow automake to automatically add some necessary script files.
The status after running is:
------------------------------------------------
[Root @ localhost main] #Automake -- add-Missing
Configure. In: 8: Installing './missing'
Configure. In: 8: Installing './install-Sh'
Makefile. AM: Installing './depcomp'
[Root @ localhost main] #Ls
Aclocal. M4 config. H. In Configure. In ~ Main. c makefile. In
Autom4te. cache configureDepcompMakefile. AMMissing
Autoscan. Log Configure. InInstall-SHMakefile. Am ~
[Root @ localhost main] #
------------------------------------------------
Step 8
RunConfigureIn this step, the makefile. In is converted into the final makefile by running the automatic configuration setting file configure.
The running result is as follows:
------------------------------------------------
[Root @ localhost main] #./Configure
Checking for a BSD-compatible install.../usr/bin/install-C
Checking whether build environment is sane... yes
Checking for a thread-safe mkdir-p.../bin/mkdir-P
Checking for gawk... gawk
Checking whether make sets $ (make)... yes
Checking for GCC... gcc
Checking for C compiler default output file name... A. Out
Checking whether the C compiler works... yes
Checking whether we are 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 ISO c89... None needed
Checking for style of include used by make... GNU
Checking dependency style of GCC... gcc3
Configure: Creating./config. Status
Config. Status: Creating makefile
Config. Status: Creating config. h
Config. Status: executing depfiles commands
[Root @ localhost main] # ls
Aclocal. M4 config. H. In Configure. In Main. c makefile. In
Autom4te. cache config. Log Configure. In ~ MakefileMissing
Autoscan. Log config. StatusDepcompMakefile. AMStamp-h1
Config. hConfigureInstall-SHMakefile. Am ~
[Root @ localhost main] #
------------------------------------------------
Step 9
RunMakeTo test the configuration file makefile.
The status is as follows:
------------------------------------------------
[Root @ localhost main] #Make
CD. &/bin/sh/root/project/main/missing -- run aclocal-1.10
CD. &/bin/sh/root/project/main/missing -- run automake-1.10 -- foreign
CD. &/bin/sh/root/project/main/missing -- run Autoconf
/Bin/sh./config. Status -- recheck
Running config_shell =/bin/sh./configure -- no-create -- no-recursion
Checking for a BSD-compatible install.../usr/bin/install-C
Checking whether build environment is sane... yes
Checking for a thread-safe mkdir-p.../bin/mkdir-P
Checking for gawk... gawk
Checking whether make sets $ (make)... yes
Checking for GCC... gcc
Checking for C compiler default output file name... A. Out
Checking whether the C compiler works... yes
Checking whether we are 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 ISO c89... None needed
Checking for style of include used by make... GNU
Checking dependency style of GCC... gcc3
Configure: Creating./config. Status
/Bin/sh./config. Status
Config. Status: Creating makefile
Config. Status: Creating config. h
Config. Status: config. H is unchanged
Config. Status: executing depfiles commands
CD. &/bin/sh/root/project/main/missing -- run autoheader
Rm-F stamp-h1
Touch config. H. In
Make all-AM
Make [1]: Entering directory '/root/project/main'
Gcc-dhave_config_h-I.-G-O2-MT main. O-MD-MP-MF. deps/Main. TPO-c-o main. O main. c
MV-F. deps/Main. TPO. deps/Main. Po
Gcc-g-O2-O main. o
CD. &/bin/sh./config. Status config. h
Config. Status: Creating config. h
Config. Status: config. H is unchanged
Make [1]: Leaving directory '/root/project/main'
[Root @ localhost main] #Ls
Aclocal. M4 autoscan. Log config. H. In config. Status Configure. In depcompMain main. oMakefile. Am makefile. In stamp-h1
Autom4te. cache config. h config. Log configure Configure. In ~ Install-SH main. c makefile. Am ~ Missing
[Root @ localhost main] #
------------------------------------------------
Step 10
Run the generated file main:
------------------------------------------------
[Root @ localhost main] #./Main
Hello, auto makefile!
[Root @ localhost main] #
------------------------------------------------
I use ubuntu.
The above is the full text, but there is one thing to change: Use aclocal
There is an error in the M4 file. Locate the line that reported the error. Add a brackets to the variable.