The whole process of automatically generating Makefile is detailed.

  

automake/autoconf Getting Started As a program developer under Linux, you must have encountered Makefile. It is very convenient to use the make command to compile your own program. In general, everyone writes a simple Makefile by hand. It is not so easy to write a Makefile that conforms to free software conventions.

In this article, we will show you how to use autoconf and automake to help us automatically generate Makefiles that conform to free software conventions, so that you can use the same GNU program as you would with “. /configure”,“make”,“make instal” can install the program into the Linux system. This will be especially useful for programmers who want to be open source software, or if you are just writing small Toys yourself, this article will be of great help to you.

First, the introduction of Makefile

Makefile is used for automatic compilation and linking. A project has many files. Each file change will cause the project to be relinked, but not all files. All need to be recompiled. The Makefile records the information of the file. When making, it will decide which files need to be recompiled when linking.

The purpose of the Makefile is to let the compiler know which files to rely 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 recompile the corresponding module.

The basic structure of the Makefile is not very complicated, but when a programmer starts writing a Makefile, he often wonders if he or she writes in accordance with the convention, and the Makefile he writes is often associated with his own development environment. After the system environment variable or path has changed, the Makefile may have to be modified. This has caused many problems with hand-written Makefiles, and automake just helps us solve these problems.

With automake, the programmer only needs to write some simple files containing predefined macros. The autoconf generates a configure based on a macro file. The automake generates Makefile.in based on another macro file, and then uses configure to use Makefile.in to generate a custom-made Makefile. Below we will detail the Makefile automake generation method.

Second, the environment used

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.

Three, starting with helloworld

We started with the example program helloworld that we use most often.

The following process is simply to say:

Create three new files:

helloworld.cconfigure.inMakefile.am

Then execute:< Br>

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

You can see that the Makefile is generated, and you can compile helloworld.c.

It's very simple, a few commands can make a custom Makefile, how it feels.

Now I will introduce the detailed process:

1.Build a directory

Build a helloworld directory in your working directory, we use it to store the helloworld program and related files. For example, under /home/my/build:

$ mkdir helloword$ cd helloworld

2, helloworld.c

and then write with your favorite editor A hellowrold.c file, such as the command: vi helloworld.c. Use the code below as the content of helloworld.c.

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

Save and exit after completion.

There should be a helloworld.c written by yourself in the helloworld directory.

3. Generate configure

We use the autoscan command to help us generate a template file for configure.in based on the source code in the directory.

Command:

$ autoscan$ lsconfigure.scan helloworld.c

A file will be generated in the hellowrold directory after execution: configure.scan, we can use it as configure The blueprint for .in.

Now rename configure.scan to configure.in and edit it, modify it as follows, and remove irrelevant statements:

============ ================configure.inContent 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)============================================================ End ================================================================ The commands aclocal and autoconf will generate two files aclocal.m4 and configure respectively:

$ aclocal$lsaclocal.m4 configure.in helloworld.c$ autoconf$ lsaclocal.m4 autom4te.cache configure configure.in helloworld. c

You can see that the configure.in content is some macro definitions, these macros are autocon After f processing, it becomes a shell script that checks system characteristics, environment variables, and parameters necessary for the software.

autoconf is a tool for generating automatic configuration software source code scripts (configure). The configure script can be run independently of autoconf and does not require user intervention during the run.

To generate the configure file, you must tell autoconf how to find the macro you are using. The way is to use the aclocal program to generate your aclocal.m4.

aclocal automatically generates an aclocal.m4 file 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 file that lists the various parameters required to compile the software.

autoconf requires the GNU m4 macro processor to process aclocal.m4 and generate a configure script.

m4 is a macro processor. Copy the input to the output and expand the macro. Macros can be either inline or user-defined. In addition to the ability to expand macros, m4 has some built-in functions for referencing files, executing commands, integer operations, text operations, loops, and more. M4 can be used either as a front end to the compiler or as a macro processor alone.

4, New Makefile.am

New Makefile.am file, command:

$ vi Makefile.am

Contents are as follows:

AUTOMAKE_OPTIONS=foreign

Copyright © Windows knowledge All Rights Reserved