Use Autoconf and automake to automatically generate makefile instances

Source: Internet
Author: User
Tags automake

Reference Source: http://zhoulifa.bokee.com/3408349.html

--------

Use Autoconf and automake to automatically generate makefile instances

For a C programmer in Unix/Linux, a troublesome job is to write your own makefile.

You may have the following experience: To write a simple C program, write a few more lines of GCC commands to turn the program into executable; To write a slightly complex program, the number of source files may be about 30, or write one
Line-based GCC commands are troublesome. You may want to write a makefile, and you may be doing the same. But one day you will find that the makefile you write may not be all
Makefile is a common makefile in Unix/Linux operating systems. For example, if someone downloads your program to his own computer, he may not be able to make it.

In this way, you need to understand and learn how to use Autoconf and automake.

Autoconf
Is a tool used to generate shell scripts that can automatically configure the software source code package to adapt to a variety of UNIX systems. The configuration script generated by Autoconf does not need to be run by the user.
Manual intervention; usually they do not even need to manually give parameters to determine the type of the system. Instead, they perform independent tests on various features that may be required by the software package. Before each test, they print a single row
Messages to indicate that they are being detected, so that users are not anxious to wait for the script to be executed. Therefore, they work very well in a hybrid system or a system customized from a variety of common UNIX variants.
Okay. You do not need to maintain files to store a list of features supported by various unix variants and release versions.

Automake is a tool that automatically generates makefile. In from the makefile. Am file. Each makefile. Am is basically a series of makefile. In generated by the make macro definition (The make rule will occasionally appear), which complies with the GNU makefile standard.

Automake requires Perl to generate makefile. In. However, the release created by automake fully complies with the GNU standard, and Perl is not required during creation.

Before using Autoconf and automake, first confirm that your system has installed the following GNU software:

1. automake

2. Autoconf

3. M4

4. Perl

5. If you need to generate a shared library, you also need the GNU libtool

Before introducing the method, let's take a look at the figure below the idea (see the attachment) and write down the steps for Autoconf and automake:

The steps are as follows:

1. Use the autoscan command to generate the configure. Scan file from your source file, modify the configure. Scan file, and rename it Configure. In.

2. The aclocal command generates aclocal. M4.

3. Generate configure using the Autoconf command

4. edit a makefile. Am file and generate the makefile. In file by using the automake command.

5. Run the configure command to generate makefile

Automake supports three directory levels: flat, shallow, and deep.

A flat Package refers to a package in which all files are stored. Makefile. Am provided for such packages does not require the subdirs macro. An example of this type of package is termutils. Corresponding to our programmers: all source files and their own header files are located in the current directory, and there are no subdirectories.

A deep Package refers to a package where all source code is stored in a subdirectory. The top-level directory mainly contains configuration information. GNU cpio is a good example of such packages.
Tar is also. The top-level makefile. am of the deep package will include the macro subdirs, but there is no macro that defines the object to be created. Corresponding to our programmers: all source files and
All header files written by yourself are located in a sub-directory of the current directory, and there are no source files in the current directory.

A shallow Package refers to the main source code stored in the top-level directory, while each part (typically a library) is stored in a subdirectory package. Automake itself is such a package
(GNU
So does make. automake is no longer used ). Corresponding to our programmers: The main source file is in the current directory, while some other source files that implement various functions are not
Same directory.

The method for editing programs at the first two levels is very simple. Follow these steps step by step. The third layer, shallow, is a little more complex, but this is the structure we often use to write programs. The following example shows how to automatically generate a makefile for the source file of the shallow hierarchy.

The example source program structure is as follows:

Hello
Is our working directory. The Hello directory contains the main. C source file and five directories, including comm, tools, DB, network, and interface. Comm directory
Source files and header files include comm. C and comm. h. The tools directory contains tools. C and tools. H, and other directories include dB. C, DB. H, and,
Network. C, network. H, interface. C, interface. H, and other source files.

Follow these steps to automatically generate makefile:

1. Enter the hello directory and run the autoscan command. The command is as follows:

CD hello

Autoscan

2,
Ls will find an additional Configure. Scan file. Modify this file and add am_init_automake (hello,
1.0), here hello is your software name, 1.0 is the version number, that is, your source code compilation will generate a software version hello-1.0. Then
The last line of the configure. Scan file's ac_output macro is filled with ac_output (makefile), indicating that Autoconf and
Automake will generate the MAKEFILE file. Finally, change the configure. Scan file to configure. In. Final
The Configure. In file content is as follows:

DNL process this file with Autoconf to produce a configure script.

Ac_init (target. c)

Am_init_automake (hello, 1.0)

DNL checks for programs.

Ac_prog_cc

DNL checks for libraries.

DNL checks for header files.

DNL checks for typedefs, structures, and compiler characteristics.

DNL checks for library functions.

Ac_output (makefile)

3. Run the aclocal command. ls will find an aclocal. M4 file is added.

4. Run the Autoconf command. ls will generate an executable configure command.

5. edit a makefile. Am file with the following content:

Automake_options = foreign

Bin_programs = Hello

Hello_sources = Main. c comm/COMM. c comm/COMM. h tools/tools. c
Tools/tools. h db/db. c db/db. h Network/network. C Network/network. h
Interface/interface. C interface/interface. h

This indicates that you will generate a hello program using the preceding hello_sources source file using the make command.

6. Run the automake -- add-missing command. The screen prompt is as follows:

Automake: Configure. In: Installing './install-Sh'

Automake: Configure. In: Installing './mkinstalldirs'

Automake: Configure. In: Installing './missing'

7. Then you can run the configure command generated earlier to generate a MAKEFILE file and enter the./configure command.

8. Edit the MAKEFILE file and find the line where $ (Link) is located. The content of the generated file is as follows:

@ RM-F hello

$ (Link) $ (hello_ldflags) $ (hello_objects) $ (hello_ldadd) $ (libs)

Add several rows between the two rows:

@ RM-F hello

@ MV-F comm. O comm

@ MV-F tools. O tools

@ MV-F dB. O DB

@ MV-F network. O network

@ MV-F interface. O interface

$ (Link) $ (hello_ldflags) $ (hello_objects) $ (hello_ldadd) $ (libs)

This is because the default makefile will put all the target files in the current directory after compilation, and the corresponding target files will be found in each subdirectory during link.

Of course, for completeness, we recommend that you add the following lines in the clean section:

@ RM-F comm/COMM. o

@ RM-F tools/tools. o

@ RM-F db/db. o

@ RM-F Network/network. o

@ RM-F interface/interface. o

Now, after completing these steps, you can compile your own executable program. Enter make all, and then you can run./hello to see if your program is running.

The biggest advantage of using Autoconf and automake is that after your program is published as a source program, all others only need to input

./Configure

Make

Make install

Command to install your program on your computer. All Unix/Linux operating systems that comply with the GNU standard do not need to modify any characters in the makefile.

<! --
TD. attachrow {Font: normal 11px verdana, Arial, Helvetica, sans-serif; color: #4c5d77; border-color: #4c5d77 ;}
TD. attachheader {Font: normal 11px verdana, Arial, Helvetica, sans-serif; color: #4c5d77; border-color: #4c5d77; Background-color: # d6e2eb ;}
Table. attachtable {Font: normal 12px verdana, Arial, Helvetica, sans-serif; color: #4c5d77; border-color: #4c5d77; border-collapse: collapse ;}
-->

Auto.gif
Description:
File Size: 4.46 KB
Read: File downloaded or viewed 12 times

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.