Automake introduction, automake

Source: Internet
Author: User
Tags automake

Automake introduction, automake

People who have written programs on Unix, especially those who use C to develop programs, generally encounter Makefile. It is really convenient to use make to develop and compile programs, however, writing a Makefile is not that easy. GNU Make's hundreds of pages of files scared many people. Of course, there are many files about make, but writing a Makefile is always a very annoying thing. The GNU Autoconf and Automake software are used to help program developers easily generate Makefile files. Currently, GNU software, such as Apache and MySQL Minigui, uses Autoconf and Automake for automatic compilation. You only need to use "./configure", "make", and "make install" to install the program to the system.

Makefile is basically a series of rules composed of target, dependencies, and action. Make decides how to compile (compile) and link programs or other actions based on Makefile rules. Of course, make can not only compile and connect programs. For example, in the FreeBSD port collection, Makefile can also automatically download remote programs, extract (extract), patch (patch), and set, then compile and install it to the system. Although the basic structure of Makefile is very simple, using these rules can transform many different patterns. Because of this, many people will feel that there are no rules to follow when they first learn to write makefiles. The makefiles written by each person are not the same, and they do not know where to start, in addition, it is often restricted by the development environment. As long as the environment parameters are different or the path is changed, Makefile may have to be modified. Although GNU MakefileConventions (GNU Makefile Convention) has developed some standards and specifications for writing Makefile during GNU program design, its content is very long and complex and it is often adjusted, automake emerged to reduce the burden on program developers to maintain Makefile. With Automake, programmers only need to write some predefined macros (macro) and submit them to Automake for processing. A Makefile. in file that can be used by Autoconf will be generated. Then, use the automatic configuration file configure generated by Autoconf to generate a Makeifle that complies with the GNUMakefile conventions.

The following example describes how to use automake to compile a static library, dynamic library, and the entire project.

In this example, we simply compile a hello. c file into a hello executable file. The specific steps are as follows:

1. Run the autoscan command to generate the autoscan. log and configure. scan files.

2. mv configure. scan configure. ac and modify configure. ac (configure. in was used earlier)

-*-Autoconf-*-# Process this file with autoconf to produce a configure script. AC_PREREQ ([2.68]) AC_INIT (hello, 1.0, [email]) AM_INIT_AUTOMAKE (hello, 1.0) AC_CONFIG_SRCDIR ([hello. c]) # used to check the validity of the source code directory. You can select any source code file to AC_CONFIG_HEADERS ([config. h]) # used to generate config. h file for autoheader to use # Checks for programs. AC_PROG_CXX AC_PROG_CC # LT_INIT # compile the two lines that must be added to libtool. Here, it is directly compiled into so, so # AC_PROG_LIBTOOL # Checks for libraries is not required. # Checks for header files. # Checks for typedefs, structures, and compiler characteristics. # Checks for library functions. AC_CONFIG_FILES ([Makefile]) # All makefiles to be generated AC_OUTPUT

3. Use aclocal to generate the aclocal. m4 File

4. Run the autoconf command to generate the configure file.

5. Run the autoheader command to generate config. h and config. h. in.

6. Create a Makefile. am file with the following content:

AUTOMAKE_OPTIONS= foreignbin_PROGRAMS= hellohello_SOURCES= hello.c

7. touch news readme authors ChangeLog

8. Execute automake -- add-missing. Automake will generate some files based on the Makefile. am file, including the most important Makefile. in

9. Run the./configure command to generate the Makefile file.

Note:

The above process of generating Makefile is very different from the previous method of self-writing. To use Automake, you only need to use some defined macros. We will write the macro and target in Makefile. in the am file, Automake reads the Makefile. the am file expands the defined macro string and generates the corresponding Makefile. in file, and then the shell script configure according to Makefile. in to generate a suitable Makefile.

In the intermediate files generated above, developers need to set configure. ac and Makefile. am. The meanings of commonly used Macros in these two files are described below.

Configure. ac File: Autoconf is a tool used to generate the 'configure 'file. 'Configure 'is a shell script that automatically sets compilation parameters so that the program can be compiled with conditions to conform to Unix systems of different platforms. Autoconf reads the configure. ac file and generates the shell script 'configure. The configure. ac file contains a series of GNU m4 macros. These macros are processed by autoconf and become shell scripts that check system features. The order of Macros in the configure. ac file is not specified, but each configure. in file must add the AC_INIT macro before all other macros, and then add the AC_OUTPUT macro at the end of all other macros. Common macros are as follows:

Dnl and #: The content after this macro will not be processed. It can be considered as a comment.
AC_INIT (FILE): This macro is used to check the path of the source code. autoscan will automatically generate the Macro. Generally, you do not need to modify it.
AM_INIT_AUTOMAKE (PACKAGE, VERSION): This is a necessary macro to use Automake. PACKAGE is the name of the software to be generated, and VERSION is the VERSION number. Generally, autoscan is not generated and must be manually added.
AC_PROG_CC: Check the available C compiler. If the source code is written in C, this macro is required.
AC_OUTPUT (FILE): Set the file to be generated by configure. If it is Makefile, configure will fill in the result it checks into the Makefile. ac file and generate a suitable Makefile.
In fact, some other macros are needed when automake is used. We use aclocal to help generate these macros. Executing aclocal will generate the aclocal. m4 file. If there is no special purpose, you do not need to modify it. The macro generated by aclocal will tell automake how to act. With the configure. ac and aclocal. m4 files, you can execute autoconf to generate the configure file.

For specific references, see the official documentation: http://www.gnu.org/software/autoconf/manual/autoconf.html

Makefile. am File: Automake converts Makefile. am To The Makefile. in file with the help of perl Based on the Macros in configure. ac. The Makefile. am file defines the target to be generated. The following figure lists executable files, static libraries, header files, and data files. The general rules for writing Makefile. am files are as follows:


The meanings of global variables are as follows:


When compiling the Makefile. am file, try to use the relative path as much as possible. Otherwise, the installation may be in different directories,


The entire Makefile. am is written according to the preceding three tables, whether it is an executable file or a dynamic library. Compare the above table to explain the Makefile. am of the hello program:

AUTOMAKE_OPTIONS: Set the Automake option. Automake is mainly used to help GNU software developers maintain the software. Therefore, when executing Automake, it will check whether there are files in the standard GNU software in the directory, for example, files such as 'News', 'author', and 'changelog. When set to foreign, Automake will use the standard of general software to check, these files are not needed at this time
Bin_PROGRAMS: Defines the name of the execution file to be generated. If multiple execution files are to be generated, each file name is separated by a blank space.
Hello_SOURCES: Defines the original file required by the 'hello' program. If the 'hello' program is generated by multiple original files, you must list all the original files it uses and separate them with blank spaces. Suppose 'hello' still needs 'Hello. c ', 'main. c', 'Hello. h' defines hello_SOURCES = hello. c main. c hello. h. If multiple execution files are defined, the corresponding filename_SOURCES must be defined for each execution program.

LDADD: It is generally used to specify the library generated by the project.LDFLAGSGenerally, use-l to specify the third-party libraries that the project depends on, such as the opencv library. In addition, because of a Makefile. multiple dynamic libraries and executable files may be generated in am. Therefore, if LDADD is used, the dependent libraries are specified for all libraries or execution files, if hello_LDADD is used, the dependency library is specified for the hello application, which is similar to the relationship between global variables and local variables.

For usage of other macros, refer to the next automake to compile a complete project-dynamic library + executable file.

For more information, see automake official documentation: http://www.gnu.org/software/automake/manual/automake.html



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.