Using Linux under Autoconf and Automake

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

   作为Linux下的程序开发人员,一定都遇到过Makefile,用make命令来编译自己写的程序确实是很方便。一般情况下,大家都是手工写一个简单Makefile,如果要想写出一个符合自由软件惯例的Makefile就不那么容易了.   在本文中,将介绍如何使用autoconf和automake两个工具来帮助我们自动地生成符合自由软件惯例的Makefile,这样就可以象常见的GNU程序一样,只要使用“./configure”,“make”,“make instal”就可以把程序安装到Linux系统中去了。这将特别适合想做开放源代码软件的程序开发人员,又或如果你只是自己写些小的Toy程序,那么这个文章对你也会有很大的帮助。
I. INTRODUCTION of MAKEFILE

Makefile is used to automatically compile and link, a project has a lot of files, each file changes will lead to the project re-link, but not all the files need to be recompiled, makefile records the information of the file, When make determines which files need to be recompiled at the time of the link.

The purpose of makefile is to let the compiler know what other files you need to rely on to compile a file. When those dependent files are changed, the compiler automatically discovers that the final makefile is obsolete and compiles the appropriate modules.

The basic structure of makefile is not very complex, but when a program developer begins to write makefile, he often suspects that his writing is customary, and that his own makefile is often associated with his own development environment, and when the system environment variable or path has changed, Makefile may also have to follow the changes. This creates a lot of problems with manual writing makefile, Automake just helps us to solve these problems.

With Automake, the program developer only needs to write some simple files containing predefined macros, which are generated configure by autoconf based on a macro file. Generate makefile.in from another macro file by Automake, and then use configure to generate a custom makefile based on makefile.in. Below we will describe in 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 the HelloWorld

We start with the example program HelloWorld we use most often.

The following procedure, if simply speaking, is:

Create a new three file:

   <pre><code>    helloworld.c

Configure.in

makefile.am

</pre></code>

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 makefile, how do you feel.

Now let's start with the detailed procedure:

  

    1. Build Catalog

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

    $ mkdir helloword    $ cd helloworld    

2.helloworld.c

Then write a hellowrold.c file with your favorite editor, 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 exit when finished.

Now in the HelloWorld directory there should be a helloworld.c you wrote 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    

After execution, a file is generated in the Hellowrold directory: Configure.scan, we can take it as a blueprint for configure.in.

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

    ============================configure.in内容开始     =======================================    # -*- 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内容结束=========================================

 

Then execute the commands 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     

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

Autoconf is a tool used to generate automatic configuration software source code scripts (configure). The Configure script can run independently of the 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 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 configure.in, a template file that enumerates the various parameters required to compile the software.

Autoconf needs the GNU M4 macro processor to handle ACLOCAL.M4, generating configure scripts.

M4 is a macro processor. Copies the input to the output while expanding the macro. A macro can be either inline or user-defined. In addition to the ability to expand macros, M4 also has built-in functions for referencing files, executing commands, integer arithmetic, text manipulation, looping, and so on. M4 can be either the front end of the compiler or a single macro processor.

4. New makefile.am

To create a 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. Macros and targets defined in makefile.am instruct Automake to generate the specified code. For example, macro Bin_programs will cause the compilation and connection targets to be generated.

5. Running 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 build 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 Makefile    config.status: executing depfiles commands    $ ls -l Makefile    -rw-rw-r-- 1 yutao yutao 15035 Oct 15 10:40 Makefile     

As you can see, at this point the makefile has been produced.

7. Compiling code with 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!

This HelloWorld is compiled, if you follow the above steps to do, it should also be easy to compile the correct HelloWorld file. You can also try using some other make commands, such as make Clean,make Install,make Dist, to see what effects they will give you. How do you feel? I can write such a professional makefile, the boss will be very impressed with you.

Iv. in a comprehensible and understandable

For each of the above-mentioned commands, let's do some more detailed introductions.

1.autoscan

AutoScan is used to scan the source code directory for generating Configure.scan files. AutoScan can use directory names as parameters, but if you do not use parameters, then AutoScan will assume that the current directory is being used. AutoScan will scan the source files in the directory you have specified and create a Configure.scan file.

2.configure.scan

Configure.scan contains the basic options for system configuration, which are all macro definitions. We need to rename it to 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

The autoconf is used to generate configure files. Configure is a script that can set up the source program to accommodate a variety of operating system platforms, and generate 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 examine system features, environment variables, and the parameters that the software must have. The order of macros in the configure.in file is not specified, but you must add Ac_init macros and Ac_output macros to the front and last sides of all macros.

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, and it describes the name of the package we are going to generate and its version number: The package is the name of the bundle and version is the revision number. When you use the Make Dist command, it will give you a helloworld-1.0.tar.gz-like software release package 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 that we want to output.

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

After generating the configure.in and ACLOCAL.M4 two macro files, we can use the autoconf to generate the Configure file.

5.makefile.am

Makefile.am is used to generate makefile.in, which you need to write manually. Some of the content is defined in makefile.am:

Automake_options

This is the Automake option. When executing Automake, it checks the directory for the existence of various files in the standard GNU software package, such as authors, ChangeLog, news, and so on. When we set it to foreign, Automake will use the standard of the generic package to check.

Bin_programs

This is the file name that specifies the executable file that we want to produce. If you want to produce more than one executable file, separate the names with a space between them.

Helloworld_sources

This is the source code that is required to specify the "HelloWorld" to be generated. If it uses more than one source file, separate them with a space number. 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, you define a relative filename_sources for each executable file.

6.automake

We use automake–add-missing to generate makefile.in.

The definition of option –add-missing is "add missing standard files to the package", which will allow Automake to add some of the files that are necessary for a standards-only software bundle.

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

7.Makefile

In makefile, which complies with the GNU Makefiel Convention, contains some basic pre-defined actions:

make

According to makefile compile source code, connect, generate target file, executable file.

make clean

Clears the object file (the file with the suffix ". O") and the executable file that resulted from the last make command.

make install

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

make dist

Generates a release package file (that is, distribution packages). This command will package the executable and related files into a tar.gz compressed file to be used as the software package for the release.

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 release package to determine the correctness of the release package. This will automatically unpack the package file, execute the Configure command, and execute make to confirm that the compilation does not appear to be wrong, and that the package is ready to be released.

===============================================helloworld-1.0.tar.gz is ready for distribution===============================================make distclean

Like make clean, but it also removes all configure generated files, including makefile.

V. Concluding remarks

With the introduction above, you should be able to easily generate your own makefile files and corresponding project files that conform to the GNU Convention. If you want to write more complex and customary 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.

Using Linux under Autoconf and Automake

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.