Use Autoconf and automake to generate makefile (recommended)

Source: Internet
Author: User
Tags automake
This article describes how to generate makefile using GNU Autoconf and automake in Linux. This article mainly discusses the ins and outs of generating makefile and Its Mechanism. Then it introduces in detail the configure. In method and its rules.

Introduction

Make is a very important compilation command in both Linux and UNIX environments. Make or make install is often used for project development and application software installation. With the make tool, we can break down large development projects into multiple modules that are easier to manage. For an application that includes hundreds of source filesProgramBy using make and makefile, you can easily straighten out the complex relationships between various source files.

However, it is a challenge for any programmer to manually compile makefile by reading the help documentation of make. Fortunately, the Autoconf and automake tools provided by GNU make makefile compilation no longer a challenge.

This article will introduce how to use the GNU Autoconf and automake tools to help us automatically generate makefile files, and make the developed software as needed in most source code packages ". /configure "," make "," make install "to install the program to the system.


Simulation requirements

Assume that the source files are stored in the following directory: 1. Use Autoconf and automake to generate the MAKEFILE file.

Figure 1 file directory structure

Suppose SRC is our source file directory, include directory stores the header files of other libraries, lib directory stores the library files used, and then start to store by module, each module has a corresponding directory, module, such as Apple and orange. Each sub-directory contains three directories: Core, include, and shell. The core and shell directories store. c files and include. H files.

Example program function: multi-threaded data read and write protection (contact the author to obtain the entire Autoconf and automake generated makefile project and source code, E-mail: normalnotebook@126.com ).


Tool Introduction

Required Software: Autoconf/automake/M4/perl/libtool (libtool is not required ).

Autoconf is a shell script tool used to generate software source code packages that can be automatically configured to adapt to a variety of UNIX systems. Autoconf needs to use M4 to generate scripts. Automake is a tool that automatically generates makefile. In from the makefile. Am file. To generate makefile. In, automake also requires Perl. Since the release created by automake fully complies with the GNU standard, Perl is not required during creation. Libtool is a tool that facilitates the generation of various libraries.

Currently, automake supports three directory levels: flat, shallow, and deep.

1) flat indicates that all files are located in the same directory.

That is, all source files, header files, and other library files are located in the current directory, and there are no subdirectories. Termutils is in this category.

2) shallow refers to the mainSource codeAll are stored in the top-level directory, and other parts are stored in sub-directories.

The main source file is in the current directory, while other source files that implement various functions are located in different directories. Automake itself is in this category.

3) Deep refers to all sources.CodeAre stored in subdirectories. The top-level directory mainly contains configuration information.

That is, all source files and their own header files are located in a sub-directory of the current directory, and there is no source file in the current directory. GNU cpio and GNU tar are in this category.

The flat type is the simplest, and the deep type is the most complex. It is not hard to see that our simulation requirements are based on the third type of deep, that is, we need to do challenging things :). Note: our testing program is a simple program based on multithreading.


Generate the ins and outs of makefile

First, go to the project directory, run a series of commands in the directory, and create and modify several files to generate makefile files that match the platform. The procedure is as follows:

1) run the autoscan command

2) Rename Configure. Scan to configure. In and modify the configure. In file.

3) create a makefile. Am file in the project directory, and create a makefile. Am file in the core and shell directories.

4) create news, README, changelog, and authors files in the project directory.

5) copy the depcomp and complie files under the/usr/share/automake-1.X/directory to the local directory

6) run the aclocal command

7) run the Autoconf command

8) run the automake-a command.

9) run the./confiugre script.

You can see in Figure 2 the process of generating makefile ,:

Figure 2 generate makefile Flowchart


Configure. In eight documents

When we use autoscan to generate the confiugre. Scan file, we need to rename confiugre. Scan to the confiugre. In file. Confiugre. In calls a series of Autoconf macros to test whether the features required or used by the program exist and the functions of these features.

Let's see the true face of confiugre. Scan:

# Process this file with Autoconf to produce a configure script. ac_prereq (2.59) ac_init (full-package-name, version, bug-Report-address) ac_config_srcdir ([config. h. in]) ac_config_header ([config. h]) # checks for programs. ac_prog_cc # checks for libraries. # fixme: replace 'main' with a function in '-lpthread': ac_check_lib ([pthread], [main]) # checks for header files. # checks for typedefs, structures, and compiler characteristics. # checks for library functions. ac_output

Each Configure. Scan file starts with ac_init and ends with ac_output. The general layout of the confiugre. In file is as follows:

Ac_init test program test function library test header file test Type Definition test structure test compiler features Test Library Function Test System Call ac_output

The preceding call order is only recommended, but we strongly recommend that you do not change the macro call order at will.

Modify the file now:

$ MV Configure. Scan configure. In $ Vim Configure. In

The modified result is as follows:

 #-*-Autoconf-*-# process this file with Autoconf to produce a configure script. ac_prereq (2.59) ac_init (test, 1.0, normalnotebook@126.com) ac_config_srcdir ([src/modulea/Apple/CORE/test. c]) am_config_header (config. h) am_init_automake (test, 1.0) # checks for programs. ac_prog_cc # checks for libraries. # fixme: replace 'main' with a function in '-lpthread': ac_check_lib ([pthread], [pthread_rwlock_init]) ac_prog_ranlib # checks for header files. # checks for typedefs, structures, and compiler characteristics. # checks for library functions. ac_output ([makefile src/lib/makefile src/modulea/Apple/CORE/makefile src/modulea/Apple/Shell/makefile]) 

Change ac_config_header ([config. H]) to am_config_header (config. h) and add am_init_automake (test, 1.0 ). Because our testing program is a multi-threaded program, we need to add ac_prog_ranlib. Otherwise, an error will occur when running the automake command. Enter the name of The makefile to be created in ac_output.

Because we use the read/write lock in the program, we need to check the library file, that is, ac_check_lib ([pthread], [main]). The macro has the following meanings:

Here, Libs is an option of link. For details, see the MAKEFILE file. Because we use the read/write lock in the program, we test whether the pthread_rwlock_init function exists in the pthread library.

Since we create makefile files based on the deep type, we need to create makefile files everywhere. That is, under the project directory, lib directory, and core and shell directory.

Autoconf provides many built-in macros for relevant testing. Due to the length relationship, we will not explain other macros here. For more information, see References 1 and 2, for more information, see the Autoconf page.


Makefile. AM

Makefile. am is a higher level rule than makefile. You only need to specify the target to be generated, the source file to be generated, and the directory to be installed.

Table 1 lists executable files, static libraries, header files, and data files. Four General formats are used to write makefile. Am files.

Table 1makefile. am General Format

For executable files and static library types, if you only want to compile and do not want to install them in the system, you can use noinst_programs instead of bin_programs, and noinst_libraries instead of lib_libraries.

Makefile. am also provides some global variables for all target bodies:

Table 2 Global variables available in makefile. AM

Use relative paths in makefile. am as much as possible. The system predefines two basic paths:

Available PATH variables in Table 3makefile. AM

We mentioned the installation path above. automake sets the default installation path:

1) Standard installation path

The default installation path is $ (prefix) =/usr/local, which can be overwritten by./configure -- prefix = <new_path>.

Other pre-defined directories include: bindir = $ (prefix)/bin, libdir = $ (prefix)/lib, datadir = $ (prefix)/share, sysconfdir = $ (prefix)/etc.

2) define a new installation path

For example, test can define testdir = $ (prefix)/test, and then test_data = test1 Test2. Then test1 and Test2 will be installed as data files to the $ (prefix) // test directory.

First, create a makefile. AM in the top-level directory of the Project (Project/) to specify the subdirectories:

Subdirs = src/lib src/modulea/Apple/Shell src/modulea/Apple/Core currentpath = $ (shell/bin/pwd) mongodes =-I $ (currentpath) /src/include-I $ (currentpath)/src/modulea/Apple/include export between des

Because the same header file is used for each source file, the top-level makefile. Am contains the header file used for compiling the source file and exports it. See the blue part of the code.

We compile the swap. c file under the lib directory into the libswap. A file, which is called by the apple/Shell/Apple. c file. The makefile. AM in the lib directory is as follows:

Noinst_libraries = libswap. A libswap_a_sources = swap. c Primary des =-I $ (top_srcdir)/src/includ

Careful readers may ask: how does table 1 show bin_libraries, and noinst_libraries here? This is because if you only want to compile and do not want to install it in the system, use noinst_libraries instead of bin_libraries. For executable files, use noinst_programs instead of bin_programs. The library will be installed in the $ (prefix)/lib directory, and the executable file will be installed in $ {prefix}/bin. If you want to install this library, the makefile. Am example is as follows:

Bin_libraries = libswap. A libswap_a_sources = swap. c Primary des =-I $ (top_srcdir)/src/include swapincludedir = $ (includedir)/swap swapinclude_headers = $ (top_srcdir)/src/include/swap. h

The last two lines mean to install swap. H to the $ {prefix}/include/swap directory.

Next, we will discuss how to write makefile. am for the type of executable files? For compiling files in the Apple/Core directory, we write makefile. am as follows:

Noinst_programs = test test_sources = test. c test_ldadd = $ (top_srcdir)/src/modulea/Apple/Shell/Apple. o $ (top_srcdir)/src/lib/libswap. A test_ldflags =-d_gnu_source defs + =-d_gnu_source # libs =-lpthread

Because our test. c file requires the apple. O and libswap. A files during the link, we need to include these two files in test_ldadd. To compile the semaphore/read/write lock file in Linux, you must specify-d_gnu_source in the compilation options. So it is specified in test_ldflags. Test_ldflags is only the link option. This option must be specified during compilation. Therefore, defs is required to specify the compilation option. Because defs already has an initial value, it is specified in the form of + =. The syntax in makefile. am is the same as that in makefile. conditional expressions can also be used. If your program contains other libraries, you can use libs to specify them in addition to the ac_check_lib macro.

If you only want to compile a file, how do you write makefile. Am? This file is also very simple, similar to the executable file, as shown in the following example:

Noinst_programs = Apple apple_sources = apple. c defs + =-d_gnu_source

Here we are just deceiving automake, pretending to generate an apple file, letting it generate dependencies and execute commands for us. After running the automake command, modify the makefile. In file under Apple/Shell/and delete the link statement directly, that is:

....... Clean-noinstprograms:-test-z "$ (noinst_programs)" | RM-F $ (noinst_programs) Apple $ (exeext): $ (apple_objects) $ (apple_dependencies) @ rM-F Apple $ (exeext) # $ (Link) $ (apple_ldflags) $ (apple_objects) $ (apple_ldadd) $ (libs ).......

Through the above processing, we can achieve our goal. From figure 1, it is not difficult to see why makefile. In is to be modified, rather than modifying other files.


Download

Name Size Download Method
Project.rar HTTP
Information about the Download Method

References

    1. Kurt wall, translated by Zhang Hui, GNU/Linux programming guide, Tsinghua University Press
    2. Robert Mecklenburg, GNU make project management (Third edition), Southeast University Press, 2006
    3. Http://www.cngnu.org/technology/index.html

Author Profile

 

Yang Xiaohua, who is currently engaged in Linux kernel research, is familiar with Linux interrupt systems. You can get in touch with him through the normalnotebook@126.com.

 

Su Chunyan: a graduate student, mainly engaged in Embedded Development in Linux.

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.