Linux makefile Overview

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

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: Fedora Core Release 1, which contains the Autoconf and automake that we will use.

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:

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.


$ Autoscan $ lsconfigure. 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 (Hellworld, 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 bundle ================================================== =====

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

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.
4. Create makefile. AM

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

$ VI makefile. AM

The content is as follows:

Automake_options = foreignbin_programs = helloworldhelloworld_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


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

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
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-r -- 1 yutao 15035 Oct 15 makefile

As you can see, makefile has been generated.

7. Use makefile to compile code

$ Makeif 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;/figcc-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 some 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 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. Aclocal is defined as "aclocal-create aclocal. M4 by scanning Configure. ac ".

4. Autoconf

Autoconf is used to generate the configure file. 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 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)

This macro is used to check the path of the source code.

Am_init_automake (package, Version)

This macro is required. It describes the name and version number of the software package to be generated. Package is the name of the software package, 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.


This macro checks the C compiler used by the system.

Ac_output (file)

This macro 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:


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.


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


This is 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 -- add-missing to generate makefile. In.

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:


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.

It generates a file named "PACKAGE-VERSION.tar.gz" in the current directory. Package and version are 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, but all files generated by configure are also deleted, 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: 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.