How to use autoconf and automake to generate Makefile in linux

Source: Internet
Author: User
Tags manual writing perl script automake
As a program developer in Linux, you must have encountered Makefile. it is really convenient to compile your own programs by using the make command. Generally, you write a simple Makefile manually. if you want to write a Makefile that complies with the free software conventions, it is not so easy as a program developer in Linux. you must have encountered Makefile, it is really convenient to compile your own programs by using the make command. Generally, you write a simple Makefile manually. it is not easy to write a Makefile that complies with the free software conventions. in this article, we will introduce how to use autoconf and automake tools to automatically generate makefiles that conform to the free software conventions. in this way, you can 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 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 the following commands in sequence:

autoscan; 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! ");    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$ 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.

4. generate configure. in

Change configure. scan to configure. in and edit it. modify the following content to remove irrelevant statements:

Code ================================ 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 (Makefile) =========================== configure. in content end =================================================== ====

5. execute aclocal and autoconf.

Then execute the commands aclocal and autoconf to generate two files: aclocal. m4 and configure:

$ aclocal$lsaclocal.m4 configure.in helloworld.c$ autoconf$ lsaclocal.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.

6. create Makefile. am

Create a Makefile. am file. 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.

7. run automake

Command:

$ automake --add-missingconfigure.in: 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.

8. execute configure to generate Makefile

$ ./configurechecking 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 for C compiler default output a.outchecking whether the C compiler works yeschecking whether we are cross compiling nochecking for suffix of executableschecking 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-rw-r-- 1 yutao yutao 15035 Oct 15 10:40 Makefile

As you can see, Makefile has been generated.

9. use Makefile to compile the 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 helloworld.o

10. run helloworld.

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

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

Use autoconf to generate the configure file based on configure. in and aclocal. m4. 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 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 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:

AUTOMAKE_OPTIONS: 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.

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 to generate Makefile. in based on configure. in and Makefile. am.

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:

Make compiles the source code, connects to generate the target file, and runs the file according to Makefile.

Make clean clears the object files (files suffixed with ". o") and executable files generated by the previous make command.

Make install installs the compiled executable files to the system directory, which is generally the/usr/local/bin directory.

Make dist generates the release 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 generates and tests 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.
Make distclean is similar to make clean, but it also deletes all files generated by configure, including Makefile.

5. process diagram

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

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.