Generate a full manual for automake of makefile in Linux

Source: Internet
Author: User
Tags manual writing automake

I. Introduction to makefile

Makefileshi 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. However, Shi does not need to re-compile all files, 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 Shi: Let the compiler know which files are needed 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 complex. However, when a program developer starts to write makefile, Shi often doubts whether the Shi he writes is in line with the conventions.
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. This causes manual writing.
Automake can solve many problems of makefile.

With automake, program developers only need to write some simple files containing predefined macros. Autoconf generates configure based on a macro file
Automake generates makefile. In based on another macro file, and uses configure to generate a conforming
Makefile. The automake Generation Method of makefile is described in detail below.

II. Environment used

The program Shi mentioned in this article is based on the Linux release version: Fedora Core Release 1, which contains the Autoconf and automake programs we will use.

3. Start with helloworld

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

The following process is simply described as Shi:

Create three new files:

Helloworld. c
Configure. In
Makefile. AM

Then execute:

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.

Int main (INT argc, char ** argv)
{
Printf ("Hello, Linux World! /N ");
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.

Change Configure. Scan to configure. In and edit it. Modify the following content to remove irrelevant statements:

======================================= 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.

# R files of checks for hea.

# Checks for Type FS, structures, and compiler characteristics.

# Checks for library functions.
Ac_output (makefile)
======================================= Configure. in content end =================================================== ====

Then execute the commands aclocal and Autoconf to generate two files: 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 some macro definitions in Configure. In. These macros are processed by Autoconf and become shell scripts that check system features, environment variables, and required software parameters.

Autoconf Shi is a tool used to generate a 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. Method Shi uses the aclocal program to generate your aclocal. M4.

Aclocal automatically generates the aclocal. M4 file based on the content of the configure. In file. Aclocalshi Perl
Script program, which defines Shi: "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.

M4shi is a macro processor. Copy the input to the output and expand the macro. Macros can be embedded in Shi or defined by Shi. 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.

4. Create makefile. AM


Create a makefile. Am file and run the following command:

$ VI makefile. AM

The content is as follows:

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.

5. Run automake

Command:

$ Automake -- add-Missing
Configure. In: Installing './install-Sh'
Configure. In: Installing './mkinstalldirs'
Configure. In: Installing './missing'
Makefile. AM: Installing './pcomp'

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

6. Execute configure to generate makefile

$./Configure
Checking for a BSD-compatible install.../usr/bin/install-C
Checking whether build environment is sane... yes
Checking for gawk... gawk
Checking whether make sets $ (make)... yes
Checking for GCC... gcc
Checking for C compiler's fault 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 nee d
Checking for style of inclu used by make... GNU
Ncy style of GCC... gcc3 of the checking pen
Configure: Creating./config. Status
Config. Status: Creating makefile
Config. Status: executing pfiles commands
$ LS-l makefile
-RW-r -- 1 yutao 15035 Oct 15 makefile

As you can see, makefile has been generated.

7. Use makefile to compile 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 ". PS/helloworld. TPO "/
-C-o helloworld. O 'test-F' helloworld. c' | echo './''' helloworld. C ;/
Then MV-F ". PS/helloworld. TPO" ". PS/helloworld. Po ";/
Else Rm-F ". PS/helloworld. TPO"; Exit 1 ;/
Fi
Gcc-g-O2-O helloworld. o

Run helloworld

$./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

Autoscanshi 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
If this parameter is specified, autoscan considers the current Shi directory to be used. Autoscan scans the source files in the specified directory and creates the configure. Scan file.

2. Configure. Scan

Configure. Scan contains the basic options set by the system, which contain some macro definitions. We need to rename it Configure. In

3. aclocal

Aclocalshi Perl
Script program. Aclocal automatically generates the aclocal. M4 file based on the content of the configure. In file. Definition of aclocal Shi: "aclocal
-Create aclocal. M4 by scanning Configure. AC ".

4. Autoconf

Autoconfshi is used to generate the configure file. Configureshi 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 contains some macros, which are passed through Autoconf
After processing, the shell script checks system features, environment variables, and required software parameters. The macro sequence in the configure. In file is not specified, but Shi you must
The ac_init macro and ac_output macro are added at the beginning and end respectively.

In Configure. ini:

# Indicates the comment, and the content after this macro will be ignored.
Ac_init (file)
This macro is used to check the path of the source code.
Am_init_automake (package, Version)
This
Which describes the name and version number of the software package to be generated: the name of the packageshi software package, versionshi version number. When you use make
In the distcommand, a software release package named helloworld-1.0.tar.gz will be generated for you, with the corresponding software package name and version number.
Ac_prog_cc
This macro checks the C compiler used by the system.
Ac_output (file)
The name of the makefile to be output for this macro Shi.

We actually need some other macros when using automake, but Shi 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. amshi is used to generate makefile. In, which requires manual writing. Makefile. Am defines some content:
Automake_options

This shiautomake 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 Shi directory. When we set it to foreign, automake will use the standard of the general software package to check.

Bin_programs

This Shi specifies the name of the executable file to be generated. If you want to generate multiple executable files, separate them by spaces.

Helloworld_sources

This Shi specifies the source code required to generate "helloworld. If multiple source files are used, separate them with spaces. For example
Helloworld. H, helloworld. C, please write it as 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 -- add-missing to generate makefile. In.
Option -- add-missing defines Shi "add missing standard files to package", which will add automake to some files required by a standard software package.
The makefile. In File Shi generated by automake conforms to the GNU makefile Convention. Next we only need to execute the configure shell script to generate the appropriate MAKEFILE file.

7. makefile

Makefile conforming to the GNU makefiel Convention contains some basic pre-defined operations:
Make
Compile the source code, connect to generate the target file, and execute the file according to makefile.
Make clean
Clear the object files generated by the last make command (Files suffixed with ". O") and executable files.
Make install
Install the compiled executable files to the system directory, which is generally the/usr/local/bin directory.
Make Dist
Generate the release package file (distribution 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. Shi is the am_init_automake (package, Version) defined in Configure. In ).
Make distcheck
Generate and test 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.

========================================================== ========
Helloworld-1.0.tar.gz is ready for distribution
========================================================== ========
Make distclean

Similar to make clean, Shi also deletes all files generated by configure, including makefile.

V. 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.

Related Article

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.