Makefile management tool-Automake and Autoconf

Source: Internet
Author: User
Tags automake

The project download path: http://files.cnblogs.com/iTsihang/hello-2.0.zip

Automake reference: http://www.linuxforum.net/books/automake.html

Autoconf references: http://www.linuxforum.net/books/autoconf.html

Automake source code download: ftp://ftp.gnu.org/gnu/automake/

Autoconf source code download: ftp://ftp.gnu.org/gnu/autoconf/

 

Directory structure supported by Automake

CurrentlyAutomakeThree directory levels are supported: flat, shallow, and deep.

  • Flat (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.

  • Shallow (shallow) indicates that the main source code is stored in the top-level directory, and other parts are stored in the subdirectory.

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.

  • Deep means that all source code is stored in sub-directories. The top-level directory mainly contains configuration information.

In this example, the directory structure belongs to the deep class:

 1 hello-2.0 2 ├── aclocal.m4 3 ├── AUTHORS 4 ├── autom4te.cache 5 │   ├── output.0 6 │   ├── output.1 7 │   ├── requests 8 │   ├── traces.0 9 │   └── traces.110 ├── autoscan.log11 ├── ChangeLog12 ├── configure13 ├── configure.in14 ├── COPYING -> /usr/share/automake-1.11/COPYING15 ├── depcomp -> /usr/share/automake-1.11/depcomp16 ├── home17 │   ├── li18 │   │   ├── inc19 │   │   │   └── li.h20 │   │   └── src21 │   │       ├── li.c22 │   │       ├── Makefile.am23 │   │       └── Makefile.in24 │   ├── wang25 │   │   ├── inc26 │   │   │   └── wang.h27 │   │   └── src28 │   │       ├── Makefile.am29 │   │       ├── Makefile.in30 │   │       └── wang.c31 │   └── zhang32 │       ├── inc33 │       │   └── zhang.h34 │       └── src35 │           ├── Makefile.am36 │           ├── Makefile.in37 │           └── zhang.c38 ├── INSTALL -> /usr/share/automake-1.11/INSTALL39 ├── install-sh -> /usr/share/automake-1.11/install-sh40 ├── main41 │   ├── main.c42 │   ├── Makefile.am43 │   └── Makefile.in44 ├── Makefile.am45 ├── Makefile.in46 ├── missing -> /usr/share/automake-1.11/missing47 ├── NEWS48 ├── park49 │   ├── Makefile.am50 │   ├── Makefile.in51 │   ├── public.c52 │   └── public.h53 └── README54 55 13 directories, 39 files

This is a basic architecture and can form a mature software framework.

 

Example

Shows the structural hierarchy of the example, including the main directory, home directory, and park directory part. Home is divided into three parts based on the last name: li, wang, and zhang. Each of them has the same structure: src and inc; part are the public directories shared by the three, it contains three shared interfaces. The main directory is our main directory, which stores the execution entry. Every person (Program) does one thing, which is to say hello in the main.

 1 int main(void) 2 { 3  4       /* access home li */ 5       lisayhello(); 6  7       /* access home wang */ 8       wangsayhello(); 9 10       /* access home zhang */11       zhangsayhello();12 13       return 0;14 }
Procedure

Create Makefile. am files in each directory and edit

When you use autoconf and automake automatic tools to generate Makefile, the tool searches for the Makefile. am file in the project directory, and generates the corresponding Makefile file in the directory where the file exists. You can also manually add them later. The general generation sequence is:

1> Create the Makefile. am file in the directory where Makefile needs to be generated based on the project structure.

The Makefile directories include home/li/src/, home/wang/src/, home/zhang/src/, and main/. Therefore, we need to create four makefiles. am file.

2> edit the Makefile. am file to indicate the work to be done in the directory, such as compiling some source files and linking some library files;

There are three types of Makefile. am. Now we will introduce their functions in the entire project directory.

Makefile. am in the home directory is mainly used to compile source files in the same directory. For example, edit home/li/src/Makefile. am as follows:

1 noinst_PROGRAMS = li # The generated Target 2 li_SOURCES = li. c # generate source 3 4 li_LDADD = # other target 5 li_LDFLAGS that may need to be loaded = # configure parameter 6 li_a_LIBADD = # other libraries that may need to be loaded 7 8 DEFS + =-D_GNU_SOURCE

In the same way, edit the other two surnames directory and the Makefile. am file under the park directory, and replace the corresponding fields.

Makefile. am in the main directory is mainly used to generate the final executable target program. It requires the last name in the home directory to provide the method. Edit as follows:

 1 # executable file last created 2  3 noinst_PROGRAMS= hello 4  5 # main entry file 6  7 hello_SOURCES= main.c 8  9 # object file that needed10 11 OBJECTS= $(top_srcdir)/home/li/src/li.o\12 13          $(top_srcdir)/home/zhang/src/zhang.o\14 15          $(top_srcdir)/home/wang/src/wang.o16 17 # load object file when linking18 19 hello_LDADD= $(OBJECTS)20 21 # flags of libs22 23 hello_LDFLAGS= -D_GNU_SOURCE24 25 DEFS += -D_GNU_SOURCE26 27 #LIBS= -lpthread

The length is limited. For the specific meaning of each line, please check the relevant documentation or the link I provide may be helpful to you.

The top-level directory Makefile. am provides the automake tool with the path of each subdirectory in the project. When making, it tells automake which directories should be used to execute makefile, And the execution process follows the sequence defined by SUBDIRS. Edit as follows:

 1 SUBDIRS= home/li/src\ 2  3                  home/zhang/src\ 4  5                  home/wang/src\ 6  7                  park\ 8  9                  main10 11 # automake need known where it is12 13 CURRENTPATH= $(shell pwd)14 15 # head files while linking16 17 INCLUDES= -I$(CURRENTPATH)/park/include -I$(CURRENTPATH)/home/li/inc -I$(CURRENTPATH)/home/wang/inc -I$(CURRENTPATH)/home/zhang/inc18 19 export INCLUDES

 

Run the autoscan command to generate the configure. scan file.

 1 #                                               -*- Autoconf -*- 2  3 # Process this file with autoconf to produce a configure script. 4  5 AC_PREREQ([2.68]) 6  7 AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS]) 8  9 AC_CONFIG_SRCDIR([home/zhang/src/zhang.c])10 11 AC_CONFIG_HEADERS([config.h])12 13 # Checks for programs.14 15 AC_PROG_CC16 17 # Checks for libraries.18 19 # Checks for header files.20 21 # Checks for typedefs, structures, and compiler characteristics.22 23 # Checks for library functions.24 25 AC_CONFIG_FILES([Makefile26 27                  home/li/src/Makefile28 29                  home/wang/src/Makefile30 31                  home/zhang/src/Makefile32 33                  park/Makefile34 35                  main/Makefile])36 37 AC_OUTPUT

The preceding steps show that the autoscan tool automatically searches for the project directory, including subdirectories, and specifies the project path for the Makefile to be created in the directory where Makefile. am is found. Of course, you can specify the file as needed, as shown below.

 

Modify the configure. scan File

$ Mv configure. scan configure. in (earlier versions support configure. ac, which I have never used)

Modify the following parts:

1 AC_CONFIG_SRCDIR ([main/main. c]) # specify the directory source file, especially the source file 2 3 # AC_CONFIG_HEADERS ([config. h]) 4 5 AM_INIT_AUTOMAKE ("hello", "2.0.0") # The final generated target file and version. The version is usually packaged with 6 7 # Checks for programs. 8 9 AC_PROG_CC10 11 # Checks for libraries.12 13 # Checks for header files.14 15 # Checks for typedefs, structures, and compiler characteristics.16 17 # Checks for library functions.18 19 AC_OUTPUT ([Makefile20 21 home/li/src/Makefile22 23 home/wang/src/Makefile 24 25 home/zhang/src/Makefile26 27 park/Makefile28 29 main/Makefile])

 

Create NEWS, README, ChangeLog, and AUTHORS under the project directory.

touch NEWS README ChangeLog AUTHORS

 

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

1 cp /usr/share/automake-1.x/depcomp   .2 cp /usr/share/automake-1.x/compile  .

 

Run the aclocal command

Generate the aclocal. m4 file and the autom4te. cache directory according to the configure. in configuration.

 

Run autoconf

Generate the configuration script-configure Based on configure. in configuration.

 

Run automake -- add-missing

After automake is run, the macro definition and target or more parameters of make are read from Makefile. am in each directory, and a new file-Makefile. in is automatically generated based on these configurations. For example, in Makefile. am defines the target file to be generated, and may also define the source files required to generate the target, dependent files-including dependent header files, target files, target libraries, etc, it also specifies the software loading flag.

Of course, to comply with the GNU release software standards, the first time you run this command, you will be prompted that AUTHORS, ChangeLog, NEWS, and README are missing. You can manually create these files without editing them. The install-sh, missing, INSTALL, and COPYING links are generated. These links point to the default installation path of the auto tool, respectively: /usr/share/automake-1.XX/the corresponding file under the directory.

In addition, the most important file is the generated Makefile. in file, and the configure file we need.

 

Configuration

Configure the installation and running environment of the project. You can pass the configuration and installation information to the project and generate the Makefile.

 

Compile

Execute Makefile, compile the project, link the file, and generate the target execution program.

 

Problem Discussion

The link to the specified install-sh, missing, INSTALL, and COPYING files is incorrect.

Depending on the tool currently installed in your system, you can direct these links to the corresponding files in the automake installation directory, or copy the files in the automake installation directory to the project directory. However, considering the GNU-style integrity, we recommend that you perform the copy step, which saves the trouble of re-Linking. What's more, the dependency on the installation path and version is not as high as the link, copy will be done once and for all.

 

If configure. in is modified for any reason, the new configure will not be generated if the autoconf command is not executed after the modification.

An error similar to "In function '_ start' occurs during compilation.

We know that automake requires that Makefile. am be created in each directory with the source program. However, after automake is compiled, the tool automatically executes the link process, which means that the compiled target file is automatically linked to an executable program with the same name, however, there is no specified entry in the source program (the "main" method in Linux), so the "Link error" is generated because the entry cannot be found during the link ".

Modify method: Makefile. in has been generated in this directory. Edit and find the link and run it. For example, in this example, line 189 of home/li/src/Makefile. in is blocked:

# $ (Li_LINK) $ (li_OBJECTS) $ (li_LDADD) $ (LIBS)

Other similar problems are handled in the same way.

 

Dependency processing between targets

During compilation, you may encounter dependency problems. The so-called dependency problem is as follows:

A → B

B → C

......

A depends on B and B on C. When generating the target A, both B and C must be notified to. In this example, the li. o, wang. o, and zhang. o targets are required to generate the target hello. Therefore, these targets need to be linked in the final link.

I have not solved the problem. If the above dependency exists during compilation of the target file (rather than the final executable file), how can this problem be solved? In the park/public. c file of the project in this example, if the broadcast method in the file is called by one of them, how can I write Makefile. am in this project (I am not sure )?

Finally, if Makefile. am is modified as needed or for any reason, run automake to update makefile. in.


Reprinted from: http://www.cnblogs.com/iTsihang

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.