Cases Urealyticum autoconf and automake generating Makefile files

Source: Internet
Author: User
Tags automake

Turn from: http://www.ibm.com/developerworks/cn/linux/l-makefile/intro

Make is a very important compilation command, both in Linux and in the UNIX environment. Whether you are developing your own projects or installing applications, we often use make or makes install. With make tools, we can break down large-scale development projects into more manageable modules, and for an application that includes hundreds of source files, using make and makefile tools makes it easy to straighten out the intricacies of each source file.

But it is a challenge for any programmer to write makefile manually by looking at make's help documentation. Fortunately, the GNU-provided autoconf and Automake are two sets of tools that make writing makefile no longer a challenge.

This article will show you how to use the GNU Autoconf and Automake tools to help us automate the generation of makefile files, and let the software be developed like most source packages, just "./configure", "make", "made Install the program to the system.

Back to top of page

Simulation requirements

Assume that the source file is stored in the following directory, as shown in 1, using autoconf and automake to generate the makefile file.

Figure 1 File directory structure

Assuming that SRC is our source file directory, the Include directory stores the header files of other libraries, the Lib directory to store the library files, and then start to store by the module, each module has a corresponding directory, the module under the molecular module, such as Apple, Orange. Core,include,shell three directories under each subdirectory, where the core and shell directories hold. c files, including the. h file, and others similar.

Sample program function: Data read and write protection based on multithreading (contact the author to obtain the entire autoconf and Automake generated makefile engineering and source code, e-mail:[email protected]).

Back to top of page

Introduction to Tools

Required Software: Autoconf/automake/m4/perl/libtool (where Libtool is not required).

Autoconf is a shell scripting tool for generating automated software source packages that can be used to adapt to a variety of Unix class systems, where autoconf needs to M4 to generate scripts. Automake is a tool that automatically generates makefile.in from a makefile.am file. Perl is also needed in order to generate Makefile.in,automake, since the publication created by Automake fully adheres to the GNU standard, so Perl is not required for creation. The Libtool is a convenient tool for generating a variety of library applications.

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

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

Is that all source files, header files, and other library files are in the current directory and have no subdirectories. Termutils is this category.

2) shallow refers to the main source code is stored in the top-level directory, the other parts are stored in subdirectories.

is the main source file in the current directory, while some other implementation of the various parts of the source files are located in their different directories. Automake itself is this category.

3) deep means that all the source code is stored in subdirectories; The top-level directory contains configuration information primarily.

Is that all source files and their own header files are located in a subdirectory of the current directory, and the current directory does not have any source files. The 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 difficult to see that our simulation needs are based on the third kind of deep type, that is, we have to do challenging things:). Note: Our test program is a simple multi-threaded program.

Back to top of page

Generate the ins and outs of Makefile

First go to the project directory, run a series of commands in this directory, create and modify several files, you can generate a makefile file that conforms to the platform, the operation process is as follows:

1) Run the AutoScan command

2) Rename the Configure.scan file to configure.in and modify the configure.in file

3) Create a new makefile.am file in the project directory and create a new makefile.am file in the core and shell directories

4) Create new news, README, ChangeLog, authors files in the project directory

5) will be/usr/share/automake-1. Copy the Depcomp and Complie files in the x/directory to this directory

6) Run the aclocal command

7) Run the autoconf command

8) Run the AUTOMAKE-A command

9) Run the./confiugre script

The process of generating makefile can be seen in Figure 2:

Figure 2 Generating a makefile flowchart

Back to top of page

Configure.in's stereotyped writing

When we use the AutoScan tool to generate the Confiugre.scan file, we need to rename the Confiugre.scan to confiugre.in file. Confiugre.in calls a series of autoconf macros to test for the existence of the required or used features of the program, and the functionality of those features.

Here we will witness the 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]) # C Hecks for headers 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. It's not hard to see the general layout of the Confiugre.in file from the file:

Ac_init test program test function library test head file test type definition Test structure Test compiler feature test library function test system call Ac_output

The above call order is only recommended, but we strongly recommend that you do not arbitrarily change the order of the macro calls.

Start modifying the file now:

$MV Configure.scan Configure.in$vim configure.in

The result of the modification is as follows:

#                                -*-Autoconf-*-# Process This file with Autoconf to produce a configure script. Ac_prereq (2.59) ac_init (test, 1.0, [email protected]) 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_r Wlock_init]) ac_prog_ranlib# Checks for headers files.# Checks for typedefs, structures, and compiler characteristics.# CHEC KS for library functions. Ac_output ([Makefilesrc/lib/makefilesrc/modulea/apple/core/makefilesrc/modulea/apple/shell/makefile])

To modify Ac_config_header ([Config.h]) to: Am_config_header (Config.h), and add Am_init_automake (test,1.0). Since our test program is a multithreaded program, it is necessary to add ac_prog_ranlib, otherwise the Automake command will be run in error. Enter the makefile file name you want to create in Ac_output.

Since we used a read-write lock in the program, we need to check the library file, that is, ac_check_lib ([Pthread], [main]), the meaning of the macro is as follows:

Among them, Libs is an option for link, please refer to the following makefile file for details. Since we used a read-write lock in our program, we tested for the existence of the Pthread_rwlock_init function in the Pthread library.

Since we created the makefile file based on the deep type, we need to create the makefile file everywhere. That is, under the project directory, under the Lib directory, the core and the Shell directory.

Autoconf provides a lot of built-in macros to do related testing, confined to the length of the relationship, we do not do a detailed explanation of other macros here, please refer to reference 1 and reference 2, also see the autoconf information page.

Back to top of page

Combat makefile.am

Makefile.am is a higher-level rule than Makefile. Just specify what target to generate, what source file it is generated from, what directory to install to, and so on.

Table One lists the executable files, static libraries, header files, and data files, and four kinds of writing makefile.am files in a general format.

Table 1makefile.am General format

For executables and Static library types, if you want to compile only, do not want to install into the system, you can use Noinst_programs instead of bin_programs,noinst_libraries instead of lib_libraries.

Makefile.am also provides a number of global variables for use by all target bodies:

Table 2 Global variables available in makefile.am

Using relative paths as much as possible in makefile.am, the system has predefined two basic paths:

Path variables available in table 3makefile.am

In the above we mentioned the installation path, Automake set the default installation path:

1) Standard installation path

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

Other predefined directories include: Bindir = $ (prefix)/bin, Libdir = $ (prefix)/lib, DataDir = $ (prefix)/share, Sysconfdir = $ (prefix)/etc, and so on.

2) define a new installation path

For example, test, you can define TestDir = $ (prefix)/test, and then Test_data =test1 test2, TEST1,TEST2 will be installed as a data file under the $ (prefix)//test directory.

We first need to create a makefile.am in the top-level directory of the project (that is, project/) to indicate the included subdirectories:

Subdirs=src/lib Src/modulea/apple/shell src/modulea/apple/core currentpath=$ (shell/bin/pwd) INCLUDES=-I$ ( Currentpath)/src/include-i$ (currentpath)/src/modulea/apple/include export includes

Since each source file will use the same header file, we include the header file used to compile the source file in the top-level makefile.am, and export it, see the Blue Section code.

We compile the swap.c file in the Lib directory into a libswap.a file, which is called by the apple/shell/apple.c file, then the makefile.am under the Lib directory is as follows:

noinst_libraries=libswap.alibswap_a_sources=swap.cincludes=-i$ (Top_srcdir)/src/includ

The attentive reader may ask: How is bin_libraries given in table 1, and this is noinst_libraries? This is because if you want to compile only, but do not want to install into the system, use noinst_libraries instead of bin_libraries, the executable file with Noinst_programs instead of Bin_programs. For installation, the library will be installed into the prefix/lib directory and the executable will be installed to ${prefix}/bin. If you want to install the library, the makefile.am example is as follows:

bin_libraries=libswap.alibswap_a_sources=swap.cincludes=-i$ (Top_srcdir)/src/includeswapincludedir=$ (includedir )/swapswapinclude_headers=$ (Top_srcdir)/src/include/swap.h

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

Next, for the case of the executable file type, we will discuss how to write makefile.am? For compiling files under the Apple/core directory, the makefile.am we write is as follows:

Noinst_programs=testtest_sources=test.c test_ldadd=$ (Top_srcdir)/SRC/MODULEA/APPLE/SHELL/APPLE.O $ (top_srcdir)/ Src/lib/libswap.a Test_ldflags=-d_gnu_sourcedefs+=-d_gnu_source#libs=-lpthread

Since our test.c files require APPLE.O and Libswap.a files when they are linked, we need to include these two files in Test_ldadd. For compiling the semaphore/read-write lock file under Linux, you need to specify-d_gnu_source in the compilation options. So it is indicated in the test_ldflags. While Test_ldflags is only the option when linking, compile the same need to indicate this option, so you need to defs to indicate the compilation options, because Defs already has the initial value, so this is indicated by the form of + =. It can be seen from here that the syntax in makefile.am is consistent with the Makefile syntax, and conditional expressions can also be used. If your program also contains other libraries, you can use Libs to specify them in addition to the Ac_check_lib macros.

If you just want to compile a file, how does makefile.am write it? This file is also very simple, similar to the executable file, as shown in the following example:

Noinst_programs=appleapple_sources=apple.cdefs+=-d_gnu_source

We're just cheating automake, pretending to build an Apple file, and let it generate dependencies and execute commands for us. So when you finish running the Automake command, and then modify the Makefile.in file under apple/shell/, delete the link statement directly, namely:

... 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 treatment, we can achieve our goal. From Figure 1 It is not difficult to see why you should modify the makefile.in, instead of modifying the other files.

Cases Urealyticum autoconf and automake generating Makefile files

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.