Autotools is a series of tools. It mainly consists of Autoconf, automake, Perl language environment, and M4. It contains five commands:
(1) aclocal
(2) autoscan
(3) Autoconf
(4) autoheader
(5) automake
I. Prepare source code
This project has three source files: score. cpp, Sum. cpp, and average. cpp.
The content is as follows:
Score. cpp:
#include <iostream>float Sum(float var[], int sum);float Average(float var[], int sum);using namespace std;int main(){float score[5] = {91, 95, 100, 98, 92};float sum, average;sum = Sum(score, 5);average = Average(score, 5);cout << "The sum score is " << sum << endl;cout << "The average score is " << average << endl;cout << "H" << endl;return 0;}
Sum. cpp:
float Sum(float var[], int num){float sum = 0.0;for(int i=0; i<num; i++)sum += var[i];return sum;}
Average. cpp:
float Average(float var[], int num){float average = 0.0;for(int i=0; i<num; i++)average += var[i];average /= num;return average;}
Ii. Steps for using autotools
2.1 use the autoscan command to scan the working directory and generate the configure. Scan file.
[email protected]:~/score$ lsaverage.cpp score.cpp sum.cpp[email protected]:~/score$ autoscan [email protected]:~/score$ lsautoscan.log average.cpp configure.scan score.cpp sum.cpp
2.2 rename the configure. Scan file to configure. In, and make appropriate modifications. In Configure. In, the line starting with # Is the annotation, and the other is the M4 macro command. The macro in Configure. AC is mainly used to detect the system.
<span style="font-size:18px;">[email protected]:~/score$ mv configure.scan configure.in[email protected]:~/score$ lsautoscan.log average.cpp configure.in score.cpp sum.cpp</span>
Before modification, the content in the configure. In file is:
<span style="font-size:18px;"># -*- Autoconf -*-# Process this file with autoconf to produce a configure script.AC_PREREQ([2.68])AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])AC_CONFIG_SRCDIR([sum.cpp])AC_CONFIG_HEADERS([config.h])# Checks for programs.AC_PROG_CXX# Checks for libraries.# Checks for header files.# Checks for typedefs, structures, and compiler characteristics.# Checks for library functions.AC_OUTPUT</span>
The content in the configure. In file after modification:
<span style="font-size:18px;"># -*- Autoconf -*-# Process this file with autoconf to produce a configure script.AC_PREREQ([2.68])AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])AM_INIT_AUTOMAKE(score, 1.0)AC_CONFIG_SRCDIR([sum.cpp])AC_CONFIG_HEADERS([config.h])# Checks for programs.AC_PROG_CXX# Checks for libraries.# Checks for header files.# Checks for typedefs, structures, and compiler characteristics.# Checks for library functions.AC_CONFIG_FILES([Makefile])AC_OUTPUT</span>
The modified file contains two more lines than the modified file:
AM_INIT_AUTOMAKE(score, 1.0)AC_CONFIG_FILES([Makefile])</span>
Note:
(1) All rows starting with "#" are comment rows.
(2) ac_prereq macro specifies the Autoconf version required in this article, such as version 2.59 in this example.
(3) The ac_init macro is used to define the software name, version, and other information, as well as the author's e-mail.
(4) am_init_automake is manually added. It is a required macro for automake. Full-package-name is the software name, and version is the software version number.
(5) The ac_config_scrdir macro is used to detect the existence of the specified source code file and determine the validity of the source code directory .. Main. C in the current directory.
(6) The ac_config_header macro is used to generate the config. h file for use by the autoheader command.
(7) ac_prog_cc is used to specify the compiler. If not specified, GCC is used by default.
(8) ac_output is used to set the file to be generated by Configure. If it is makefile, configure will bring the result it checks into the makefile. In file to generate a suitable makefile. Some other parameters are required when automake is used. These additional macros are generated using aclocal tools.
(9) The ac_config_files macro is used to generate the corresponding MAKEFILE file.
2.3 run the aclocal command to scan configure. in file generation aclocal. m4 file. This file mainly processes the macro definition locally. It is based on the installed macros, user-defined macros, and acinclude. the macro In the M4 file will be configure. the macro set required by the in file is defined in the file aclocal. m4.
[email protected]:~/score$ aclocal[email protected]:~/score$ lsaclocal.m4 autoscan.log configure.in sum.cppautom4te.cache average.cpp score.cpp
2.4 use the Autoconf command to generate the configure file. This command expands the macro In the configure. In file to generate the configure script. The macro defined in aclocal. M4 may be used in this process.
[email protected]:~/score$ lsaclocal.m4 autoscan.log configure.in sum.cppautom4te.cache average.cpp score.cpp[email protected]:~/score$ autoconf [email protected]:~/score$ lsaclocal.m4 autoscan.log configure score.cppautom4te.cache average.cpp configure.in sum.cpp
2.5 use the autoheader command to generate the config. H. In file.
[email protected]:~/score$ lsaclocal.m4 autoscan.log configure score.cppautom4te.cache average.cpp configure.in sum.cpp[email protected]:~/score$ autoheader [email protected]:~/score$ lsaclocal.m4 autoscan.log config.h.in configure.in sum.cppautom4te.cache average.cpp configure score.cpp
2.6 create a makefile. Am file manually. The automake tool converts makefile. Am To The makefile. In file based on the parameters in Configure. In.
[email protected]:~/score$ vim Makefile.am[email protected]:~/score$ cat Makefile.am AUTOMAKE_OPTIONS = foreignbin_PROGRAMS = scorescore_SOURCES = score.cpp sum.cpp average.cpp
Note:
(1) automake_options indicates the option to set automake. GNU has strict regulations on software released by itself, such as the license declaration file copying. Otherwise, an error is reported during automake execution. automake provides three software levels: Foreign, GNU, and gnits for users to choose from. The default level is GNU. In this example, the foreign level is used to detect only required files.
(2) 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 space.
(3) hello_sources defines the raw file required by the executable program "hello. If the "hello" program is generated by multiple source files, all the source files used by the program must be listed and separated by spaces. To define multiple executable programs, you need to create a corresponding file_sources for each executable program.
2.7 run automake with the following prompt:
[email protected]:~/score$ automakeconfigure.in:6: required file `./install-sh' not foundconfigure.in:6: `automake --add-missing' can install `install-sh'configure.in:6: required file `./missing' not foundconfigure.in:6: `automake --add-missing' can install `missing'Makefile.am: required file `./depcomp' not foundMakefile.am: `automake --add-missing' can install `depcomp'
Then execute automake -- add-missing to generate several necessary files.
[email protected]:~/score$ lsaclocal.m4 autoscan.log config.h.in configure.in score.cppautom4te.cache average.cpp configure Makefile.am sum.cpp[email protected]:~/score$ automake --add-missing configure.in:6: installing `./install-sh'configure.in:6: installing `./missing'Makefile.am: installing `./depcomp'[email protected]:~/score$ lsaclocal.m4 average.cpp configure.in Makefile.am score.cppautom4te.cache config.h.in depcomp Makefile.in sum.cppautoscan.log configure install-sh missing
After automake -- add-missing is executed, You can execute automake again.
2.8 run./configure to convert makefile. In into the final MAKEFILE file.
[email protected]:~/score$ ./configure
At this time, ls
[email protected]:~/score$ lsaclocal.m4 config.h configure Makefile score.cppautom4te.cache config.h.in configure.in Makefile.am stamp-h1autoscan.log config.log depcomp Makefile.in sum.cppaverage.cpp config.status install-sh missing
Makefile is successfully generated.
Iii. makefile usage
The 3.1 make command is used to compile the code. By default, the "make all" command is executed. You can see that an executable file of "score" is generated,
[email protected]:~/score$ make
At this time, ls
[email protected]:~/score$ lsaclocal.m4 average.o config.status install-sh missing stamp-h1autom4te.cache config.h configure Makefile score sum.cppautoscan.log config.h.in configure.in Makefile.am score.cpp sum.oaverage.cpp config.log depcomp Makefile.in score.o
3.2 execute./score to run the program.
[email protected]:~/score$ ./scoreThe sum score is 476The average score is 95.2H
The 3.3 make clean command clears the OBJ file during compilation. It corresponds to the make command. One is compile and the other is clear.
[email protected]:~/score$ make cleantest -z "score" || rm -f scorerm -f *.o[email protected]:~/score$ lsaclocal.m4 config.h configure Makefile score.cppautom4te.cache config.h.in configure.in Makefile.am stamp-h1autoscan.log config.log depcomp Makefile.in sum.cppaverage.cpp config.status install-sh missing
When you use make clean to generate a score executable program, the LS result does not contain the. O target file.
3.4 run the make install command to install the target file to the system. Then, run score directly.
[Email protected]: ~ /Score $ sudo make install [Sudo] password for Liuwei: Make [1]: entering the '/home/Liuwei/score' test-z "/usr/local/bin" |/bin/mkdir-P "/usr/local/bin"/usr directory /bin/install-C score '/usr/local/bin' make [1]: nothing can be done for 'Install-data-am '. Make [1]: leaving the directory '/home/Liuwei/score' [email protected]: ~ /Score $ scorethe sum score is 476the average score is 95.2 H
3.5 run the make uninstall command to detach the target file from the system.
Detailed use of autotools