The truth about the Linux Makefile system is in line with free software practices.

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

Do you want to know the truth about the Linux Makefile system? Do you want to know what is inherent in the Linux Makefile system, I will only give you a full introduction to the Linux Makefile system as a program developer in Linux. You must have met the Linux Makefile system, it is really convenient to compile your own programs by using the make command. Generally, you write a simple Linux Makefile manually. If you want to write a Linux Makefile that complies with the Free Software conventions, it is not that easy.

In this article, we will introduce how to use the autoconf and automake tools to help us automatically generate Linux makefiles that conform to the Free Software conventions. This way, like common GNU programs, you only need to 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 Linux Makefile

Linux 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 Linux Makefile records the file information. When making, it determines which files need to be re-compiled during the link.

The purpose of Linux Makefile is to 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 the Linux Makefile is not very complex. However, when a program developer starts to write the Linux Makefile, he or she often doubts whether the file he or she writes meets the conventions, in addition, the Linux Makefile you write is often associated with your development environment. When the system environment variables or paths change, the Linux Makefile may need to be modified. In this way, many problems are caused by manual Writing of Linux Makefile. 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 Linux Makefile based on another macro file. in, and then use configure Based on Linux Makefile. in to generate a Linux Makefile. The following describes how to generate automake for Linux Makefile.

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
Linux Makefile. am

Then execute:

Aclocal; autoconf; automake -- add-missing;./configure; make;./helloworld, you can see that the Linux Makefile is generated and helloworld. c can be compiled. It's easy. Just a few commands can make a Linux 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.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.
AC_OUTPUT (Linux 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. 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 Linux Makefile. am

Create a Linux Makefile. am file with the command: $ vi Linux Makefile. am

The content is as follows:

AUTOMAKE_OPTIONS = foreign
Bin_PROGRAMS = helloworld
Helloworld_SOURCES = helloworld. c

Automake will automatically generate Linux Makefile. in based on the Linux Makefile. am you wrote. The macros and targets defined in Linux 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'
Linux Makefile. am: installing './depcomp'


Automake generates some files based on the Linux Makefile. am file, including the most important Linux Makefile. in.

6. Execute configure to generate Linux 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 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 Linux Makefile
Config. status: executing depfiles commands
$ Ls-l Linux Makefile
-Rw-r -- 1 yutao 15035 Oct 15 Linux Makefile

You can see that the Linux Makefile has been generated.

7. Use Linux 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 ". 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

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 Linux Makefile yourself, the boss will surely be impressed with 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

Autoconf is used to generate the configure file. Configure is a script that can set the source program to adapt to different operating system platforms and generate appropriate Linux Makefile 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 Linux 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. Linux Makefile. am

Linux Makefile. am is used to generate Linux Makefile. in, which requires manual writing. In Linux Makefile. am, some content is defined: AUTOMAKE_OPTIONS, which 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 -- add-missing to generate Linux 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 Linux Makefile. in file generated using automake complies with the GNU Linux Makefile Convention. Next we only need to execute the configure shell script to generate a suitable Linux Makefile file.

7. Linux Makefile

In Linux Makefile that complies with the GNU Makefiel convention, it contains some basic pre-defined operations: make compiles the source code, connects to generate the target file and executable file according to the Linux Makefile. Make clean clears the objects generated by the previous make command with the suffix ". o") and executable files.

Make install installs 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 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 is similar to make clean, but all files generated by configure are also deleted, including Linux Makefile.

V. Conclusion

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

  1. Linux Makefile
  2. Environment in which Linux Makefile is automatically compiled and linked
  3. Introduction to the Linux Makefile Environment
  4. Describe the concept of Linux Makefile
  5. Linux Makefile

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.