Generate makefile using Autoconf and automake in Linux

Source: Internet
Author: User
Tags manual writing perl script automake

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 write a simple makefile manually. It is not easy to write a makefile that complies with the Free Software conventions. in this article, we will introduce how to use Autoconf and automake tools to automatically generate makefiles that conform to the Free Software conventions. In this way, you can use ". /configure "," make ", and" make instal "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.

I. Introduction to makefile

Makefile is used for automatic compilation and linking. A project consists of many files. Changes to each file will lead to re-linking of the project, but not all files need to be re-compiled, the makefile records the file information. During make, it determines which files need to be re-compiled during the link.

The purpose of makefile is to let the compiler know which files are dependent on to compile a file. When those dependent files have changed, the compiler will automatically find that the final generated file is out of date and re-compile the corresponding module.

The basic structure of makefile is not very complex. However, when a program developer starts to write makefile, he or she often doubts whether the content he or she writes meets the conventions, the makefile you write is often associated with your development environment. When the system environment variables or paths change, the makefile may need to be modified. In this way, many problems of manual makefile writing occur. automake can help us solve these problems well.

With automake, program developers only need to write some simple files containing predefined macros. Autoconf generates configure based on one macro file, and automake generates makefile based on another macro file. in, and then use configure Based on makefile. in to generate a regular makefile. The automake Generation Method of makefile is described in detail below.

II. Environment used

The program mentioned in this article is based on the Linux release version: Linux 2.6.18-194. EL5, which contains the Autoconf and automake that we will use. If not, you can download Autoconf and automake.

3. Start with helloworld

We start with helloworld, the most commonly used example program.

The following process is as follows:

Create three new files: helloworld. c Configure. In makefile. AM

Then execute: autoscan; aclocal; Autoconf; automake -- add-missing;./configure; Make;./helloworld;

The makefile is generated, and helloworld. C can be compiled. It's easy. Just a few commands can make a makefile that complies with the conventions. How do you feel. Now we will introduce the detailed process:

1. Create a directory
Create a helloworld directory in your working directory and use it to store the helloworld program and related files, such as in/home/My/Build:

$ Mkdir helloword

$ CD helloworld

2. helloworld. c
Then, use your favorite editor to write a hellowrold. c file, such as the command VI helloworld. C. Use the following code as the content of helloworld. C.

# Include <stdio. h>

Int main (INT argc, char ** argv)
{
Printf ("Hello, Linux World! ");
Return 0;
}

Save and exit. Now there should be a self-written helloworld. c In the helloworld directory.

3. Generate configure

We use the autoscan command to generate a Configure. In Template File Based on the source code in the directory.
Command:
$ Autoscan
$ Ls
Configure. Scan helloworld. c
After execution, a file Configure. Scan will be generated in the hellowrold directory. We can use it as the blueprint for Configure. In.

4. Generate Configure. In
Change Configure. Scan to configure. In and edit it. Modify the following content to remove irrelevant statements:
Code
======================================= Configure. in content start = ====

#-*-Autoconf -*-

# Process this file with Autoconf 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 =================================================== ====

5. Execute aclocal and Autoconf.
Then execute the commands aclocal and Autoconf to generate two files: aclocal. M4 and configure:
Code
$ Aclocal
$ Ls
Aclocal. M4 Configure. In helloworld. c
$ Autoconf
$ Ls
Aclocal. M4 autom4te. cache configure Configure. In helloworld. c

As you can see, configure. In contains macro definitions. These macros are processed by Autoconf and become shell scripts that check system features, environment variables, and required software parameters.
Autoconf is a tool used to generate an automatic configuration software source code script (configure. The configure script can run independently of Autoconf and does not require user intervention during the running process.
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 the aclocal. M4 file based on the content of the configure. In file. Aclocal is a Perl script program defined as "aclocal-create aclocal. M4 by scanning Configure. ac ".
Autoconf creates configure from the template file that lists the parameters required for software compilation in configure. In.
Autoconf requires the GNU M4 macro processor to process aclocal. M4 and generate the configure script.
M4 is a macro processor. Copy the input to the output and expand the macro. Macros can be embedded or user-defined. In addition to expanding macros, M4 also has some built-in functions used to reference files, execute commands, integer operations, text operations, loops, and so on. M4 can be used either as the front-end of the compiler or as a macro processor.

6. Create makefile. AM
Create a makefile. Am file. Run the following command: $ VI makefile. am.
The content is as follows:
Code:
Automake_options = foreign
Bin_programs = helloworld
Helloworld_sources = helloworld. c

Automake will automatically generate makefile. In based on your makefile. am. Macros and targets defined in makefile. Am will guide automake to generate specified code. For example, macro bin_programs will generate the compilation and connection targets.

7. Run automake
Command:
Code:
$ Automake -- add-Missing
Configure. In: Installing './install-Sh'
Configure. In: Installing './mkinstalldirs'
Configure. In: Installing './missing'
Makefile. AM: Installing './depcomp'

Automake generates some files based on the makefile. Am file, including the most important makefile. In.

8. Execute configure to generate makefile
Code:
$./Configure
Checking for a BSD-compatible install/usr/bin/install-C
Checking whether build environment is sane Yes
Checking for gawk
Checking whether make sets $ (make) Yes
Checking for GCC gcc
Checking for C compiler default Output 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 ansi c 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: executing depfiles commands
$ LS-l makefile
-RW-r -- 1 yutao 15035 Oct 15 makefile
As you can see, makefile has been generated.

9. use makefile to compile the code
Code
$ Make
If gcc-dpackage_name = ""-dpackage_tarname = ""-dpackage_version = ""-
Dpackage_string = ""-dpackage_bugreport = ""-dpackage = "helloworld"-dversion = "1.0"
-I.-I.-G-O2-MT helloworld. O-MD-MP-MF ". deps/helloworld. TPO"
-C-o 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;
Fi
Gcc-g-O2-O helloworld. o

10. Run helloworld.
Code
$./Helloworld
Hello, Linux World!

In this way, helloworld is compiled. If you follow the above steps, you should easily compile the correct helloworld file. You can also try to use other make commands, such as make clean, make install, and make Dist, to see what effect they will give you. How do you feel? If you can write such a professional makefile yourself, the boss will surely look at you.

Iv. In-depth Introduction

For the commands mentioned above, we will introduce them in detail.
1. autoscan
Autoscan is used to scan the source code directory to generate the configure. Scan file. Autoscan can use the directory name as the parameter, but if you do not use the parameter, autoscan considers it to be the current directory. Autoscan scans the source files in the specified directory and creates the configure. Scan file.
2. Configure. Scan
Configure. Scan contains the basic options for system configuration, which are macro definitions. We need to rename it Configure. In
3. aclocal
Aclocal is a Perl script program. Aclocal automatically generates the aclocal. M4 file based on the content of the configure. In file. The definition of aclocal is "aclocal-create aclocal. M4 by scanning Configure. ac ".
4. Autoconf
Use Autoconf to generate the configure file based on Configure. In And aclocal. M4. Configure is a script that can set source programs to adapt to different operating system platforms and generate suitable makefiles based on different systems, so that your source code can be compiled on different operating system platforms.
The content of the configure. In file is some macros. After Autoconf processing, these macros will become shell scripts that check system features, environment variables, and required software parameters. The macro sequence in the configure. In file is not specified, but you must add the ac_init macro and ac_output macro at the beginning and end of all macros.
In Configure. ini:
# Indicates the comment, and the content after this macro will be ignored.
Ac_init (File) macro is used to check the path of the source code.
The macro am_init_automake (package, version) is required. It describes the name and version of the software package to be generated. Package is the software package name and version is the version number. When you use the make distcommand, the producer will generate a software release package named helloworld-1.0.tar.gz, with the corresponding package name and version number.
The macro ac_prog_cc checks the C compiler used by the system.
The macro ac_output (File) is the name of the makefile to be output.
We actually need to use some other macros when using automake, but we can use aclocal to automatically generate them. After aclocal is executed, we will get the aclocal. M4 file.
After the macro files Configure. In And aclocal. M4 are generated, we can use Autoconf to generate the configure file.
5. makefile. AM
Makefile. Am is used to generate makefile. In, which requires manual writing. Makefile. Am defines some content:
Automake_options: This is the automake option. When running automake, it checks whether there are various files in the standard GNU software package, such as authors, changelog, and news files in the directory. When we set it to foreign, automake will use the standard of the general software package to check.
Bin_programs: Specifies the name of the executable file to be generated. If you want to generate multiple executable files, separate them by spaces.
Helloworld_sources: Specifies the source code required to generate "helloworld. If multiple source files are used, separate them with spaces. For example, if you need helloworld. h and helloworld. C, write helloworld_sources = helloworld. h helloworld. C.
If you have defined multiple executable files in bin_programs, the corresponding filename_sources must be defined for each executable file.
6. automake
We use automake to generate makefile. In Based on Configure. In And makefile. am.
Option -- add-missing is defined as "add missing standard files to package", which will add automake to some files required by a standard software package.
The makefile. In file generated using automake complies with the GNU makefile Convention. Next we only need to execute the configure shell script to generate a suitable MAKEFILE file.
7. makefile
Makefile conforming to the GNU makefiel Convention contains some basic pre-defined operations:
Make compiles the source code, connects to generate the target file, and runs the file according to makefile.
Make clean clears the object files (Files suffixed with ". O") and executable files generated by the previous make command.
Make install installs the compiled executable files to the system directory, which is generally the/usr/local/bin directory.
Make Dist generates the release package ). This command will pack executable files and related files into a tar.gz compressed file for software release. A file named "package-version.tar.gz" will be generated in the current directory. Package and version are the am_init_automake (package, Version) defined in Configure. In ).
Make distcheck generates and tests the release package to confirm the correctness of the release package. This operation will automatically unbind the compressed package file, execute the configure command, and execute make to confirm that there is no error in compilation, and finally prompt that your software package is ready and can be released.
Make distclean is similar to make clean, but it also deletes all files generated by configure, including makefile.

5. Process Diagram

Vi. Conclusion

Through the above introduction, you should be able to easily generate your own MAKEFILE file and the corresponding project file that complies with the GNU conventions. If you want to write more complex and conforming makefiles, you can refer to configure in some open code projects. in And makefile. am file, such as: Embedded Database SQLite, unit test cppunit.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.