Linux under the makefile Automake generation of the full introduction _unix Linux

Source: Internet
Author: User
Tags perl script automake

Wen/Yu Tao
  
As a Linux program developers, we must have encountered makefile, using make command to compile their own written program is indeed very convenient. In general, everyone is writing a simple makefile, if you want to write a free software practice of the makefile is not so easy.

In this article, we'll show you how to use the autoconf and automake two tools to help us automatically generate makefile that conform to free software practices, so that you can use "./configure", "make", just like a common GNU program. make instal can install the program into the Linux system. This will be especially good for developers who want to do open source software, or if you're just writing small toy programs, this article will help you a lot.

First, Makefile Introduction

Makefile is used for automatic compilation and linking, a project has a lot of file composition, each file changes will cause the project to relink, but not all the files need to recompile, makefile in the record of the file information, make determines which files need to be recompiled when linking.

Makefile's mission is to let the compiler know which files to build a file to rely on. When those dependent files are changed, the compiler automatically discovers that the final makefile is obsolete and compiles the corresponding module.

The basic structure of makefile is not very complex, but when a program developer begins to write makefile, it often doubts whether the writing is in accordance with the Convention, and the makefile that they write are often associated with their own development environment, and when the system environment variable or path changes, Makefile may also have to follow the modification. This creates a lot of problems with hand-written makefile, and Automake can help us solve these problems very well.

With Automake, the program developer only needs to write a simple file containing predefined macros, generated by autoconf based on a macro file configure, The makefile.in is generated by automake based on another macro file, and the Configure Basis makefile.in is used to generate a custom-compliant makefile. Below we will detail the Automake generation method of makefile.

Second, the use of the environment

The program mentioned in this article is based on the Linux distribution: Fedora Core Release 1, which contains the autoconf,automake we want to use.

Third, starting from HelloWorld

We start with the most commonly used example program HelloWorld.

The following procedure, if it is simply:

Create a new three file:

Helloworld.c
Configure.in
makefile.am

Then execute:

aclocal; autoconf; Automake--add-missing;/configure; make;./helloworld

You can see that the makefile is generated, and you can compile the HELLOWORLD.C.

Quite simply, a few commands can make a custom makefile, how it feels.

Now let's start with the detailed process:

1, the establishment of the directory

Build a HelloWorld directory under your working directory, which we use to store HelloWorld programs and related documents, such as under/home/my/build:

$ mkdir Helloword
$ CD HelloWorld

2, HELLOWORLD.C

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

int main (int argc, char** argv)
{
printf ("Hello, Linux world!\n");
return 0;
}

Save exit after completion.

Now in the HelloWorld directory there should be a helloworld.c you write yourself.

3. Generate Configure

We use the AutoScan command to help us 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 is generated in the Hellowrold directory: Configure.scan, we can use it as a blueprint for configure.in.

Now rename Configure.scan to configure.in, edit it, and modify it with the following content, and remove irrelevant statements:

============================configure.in Content started =========================================
#-*-autoconf-*-
# Process This file and 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 =========================================

Then execute the command aclocal and autoconf, respectively, will produce ACLOCAL.M4 and configure two files:

$ aclocal
$ls
ACLOCAL.M4 configure.in HELLOWORLD.C
$ autoconf
$ ls
ACLOCAL.M4 Autom4te.cache Configure configure.in HELLOWORLD.C


You can see that the configure.in content is a macro definition that, after being processed by autoconf, becomes a shell script that examines system features, environment variables, and software-required parameters.

Autoconf is a tool used to generate automatic configuration software source code scripts (configure). Configure scripts can be run independently of autoconf, and in the process of running, no user intervention is required.

To generate configure files, you must tell autoconf how to find the macros you are using. The way is to use the Aclocal program to generate your ACLOCAL.M4.

Aclocal automatically generates ACLOCAL.M4 files based on the contents of the Configure.in file. Aclocal is a Perl script that is defined as "Aclocal-create aclocal.m4 by scanning configure.ac".

Autoconf creates configure from the Configure.in template file that enumerates the various parameters needed to compile the software.

Autoconf requires the GNU M4 macro processor to process the ACLOCAL.M4 and generate configure scripts.

M4 is a macro processor. Copies the input to the output while the macro expands. Macros can be either inline or user-defined. In addition to expanding macros, M4 also has built-in functions for referencing files, executing commands, integer operations, text manipulation, loops, and so on. M4 can be either a front-end to the compiler or as a macro processor alone.

4. New makefile.am

New makefile.am file, command:


$ VI makefile.am


The contents are as follows:


Automake_options=foreign
Bin_programs=helloworld
Helloworld_sources=helloworld.c


Automake will generate makefile.in based on the makefile.am you write.

The macros and targets defined in makefile.am instruct Automake to generate the specified code. For example, macro bin_programs will cause the target of compilation and connection to be generated.

5. Operation Automake

Command:


$ automake--add-missing
Configure.in:installing './install-sh '
Configure.in:installing './mkinstalldirs '
Configure.in:installing './missing '
Makefile.am:installing './depcomp '


Automake will produce some files based on the makefile.am file, including the most important makefile.in.

6. Perform Configure generation 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, 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-rw-r--1 Yutao yutao 15035 Oct 10:40 Makefile


As you can see, the makefile has been produced.

7. Compile code using Makefile

$ 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 HELLOWORLD.O


Run HelloWorld

$./helloworld
Hello, Linux world!


So HelloWorld is compiled, if you follow the steps above, you should also be able to easily compile the correct HelloWorld file. You can also try to use some other make commands, such as made Clean,make Install,make Dist, and see what they can do to you. How do you feel? I can write such a professional makefile, the boss will be impressed with you.

Iv. Simple and simple

For the various commands mentioned above, we will make a few more detailed introductions.

1, AutoScan

AutoScan is used to scan the source code directory to generate Configure.scan files. AutoScan can be used as a parameter, but if you do not use parameters, then AutoScan will assume that the current directory is used. AutoScan will scan the source files in the directory you specify and create Configure.scan files.

2, Configure.scan

The Configure.scan contains basic options for system configuration, all of which are macro definitions. We need to name it configure.in.

3, Aclocal

Aclocal is a Perl scripting program. Aclocal automatically generates ACLOCAL.M4 files based on the contents of the Configure.in file. The definition of aclocal is: "Aclocal-create aclocal.m4 by scanning configure.ac".

4, autoconf

Autoconf is used to produce configure files. Configure is a script that sets up the source program to accommodate a variety of operating system platforms, and generates the appropriate makefile based on different systems so that your source code can be compiled on different operating system platforms.

The contents of the Configure.in file are macros that, after autoconf processing, become shell scripts that check system characteristics, environment variables, and software-required parameters. The order of the macros in the configure.in file is not specified, but you must add Ac_init macros and Ac_output macros to the front and end of all macros respectively.

In the Configure.ini:

#号表示注释, the content behind this macro will be ignored.

Ac_init (FILE)

This macro is used to check the path where the source code resides.

Am_init_automake (PACKAGE, VERSION)

This macro is required, which describes the name of the package we are going to generate and its version number: package is the name of the package, and version is the edition number. When you use the Make Dist command, it generates a software release package similar to the helloworld-1.0.tar.gz, with the name and version number of the corresponding package.

Ac_prog_cc

This macro will check the C compiler used by the system.

Ac_output (FILE)

This macro is the name of the makefile we want to export.

When we use automake, we actually need to use some other macros, but we can use aclocal to help us generate them automatically. We will get the Aclocal.m4 file after executing aclocal.

After producing the configure.in and ACLOCAL.M4 two macro files, we can use autoconf to produce configure files.

5, makefile.am

Makefile.am is used to generate makefile.in and requires you to write manually. Some of the content is defined in makefile.am:

Automake_options

This is the Automake option. When Automake is executed, it checks to see if there are any files in the standard GNU package that are available in the directory, such as authors, Changelog, news, and so on. When we set it to foreign, Automake will use the standard software package to check it.

Bin_programs

This is the filename that specifies the executable file that we want to produce. If you want to produce multiple executables, separate the names with spaces.

Helloworld_sources

This is the source code that specifies the "HelloWorld" to be generated. If it uses multiple source files, use the spaces number to separate them. For example need HELLOWORLD.H,HELLOWORLD.C then please write helloworld_sources= helloworld.h helloworld.c.

If you define more than one executable file in Bin_programs, each executable file should have a relative filename_sources defined.

6, Automake

We use Automake--add-missing to produce makefile.in.

Option--add-missing is defined as "add missing standard files to package", which allows Automake to add some files necessary for a standard package.

The makefile.in file we produced with Automake is in accordance with the GNU Makefile Convention, and then we can create the appropriate Makefile file by just executing the Configure Shell script.

7, Makefile

In the makefile, which complies with the GNU Makefiel Convention, some basic predefined actions are included:

Make

Compile source code according to makefile, connect, generate target file, executable file.

Make clean

Clears the object file (the suffix ". O" file) and the executable file produced by the last make command.

Make install

Installs the successful executable file into the system directory, typically the/usr/local/bin directory.

Make Dist

Generates a publishing package file (that is, distribution package). This command will package the executable file and related files into a tar.gz compressed file for the software package.

It generates a file with a name similar to "package-version.tar.gz" in the current directory. PACKAGE and version are the Am_init_automake (PACKAGE, version) that we defined in configure.in.

Make Distcheck

Build and test the publishing package to determine the correctness of the release package. This operation will automatically unlock the package file, then execute the Configure command, and execute make to confirm that the compilation is not an error, and finally prompts you that the package is ready to be released.

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

Similar to make-clean, but also deletes all configure-generated files, including makefile.

V. Concluding remarks

With the above introduction, you should be able to easily generate a makefile file of your own that conforms to the GNU Convention and the corresponding project file.

If you want to write more complex and routine makefile, you can refer to the configure.in and makefile.am files in some open code projects, 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.