I believe everyone who learns Linux knows makefile, which is a very useful thing, but writing it is complicated. Today we will introduce an automatic generation tool for it, the use of autotools. Many gnulinux software uses it to generate makefiles, including the Linux kernel source code that we are very familiar.
1. preparation:
Tools Required
Autoscan
Aclocal
Autoheader
Automake
Autoconf
Auto make
In the terminal, enter the command, which is not installed or which is usually the first autoscan. Other ubuntu10.04 files all have
2. Compile the test program:
Create directory: mkdir include SRC
Program writing: Include/Str. h
[CPP]View plaincopy
- # Include <stdio. h>
- Int STR (char * string );
Program: src/Str. c
[CPP]View plaincopy
- # Include "str. H"
- // Print string
- Int STR (char * string ){
- Printf ("\ n ---- print string ---- \ n \" % s \ "\ n", string );
- Return 0;
- }
- // Interface of this program
- Int main (INT argc, char ** argv ){
- Char str_read [1024];
- Printf ("Please input something end by [enter] \ n ");
- Scanf ("% s", str_read );
- Return STR (str_read );
- }
3. Generate Configure. AC
Configure. AC is the input file of automake, so it must be converted into this file.
Run the following command:
[CPP]View plaincopy
- [[Email protected] STR] # ls
- Include SRC
- [[Email protected] STR] # autoscan
- Autom4te: Configure. AC: no such file or directory
- Autoscan:/usr/bin/autom4te failed with exit status: 1
- [[Email protected] STR] # ls
- Autoscan. Log Configure. Scan include SRC
- [[Email protected] STR] # cp Configure. Scan Configure. AC
Modify Configure. AC
[CPP]View plaincopy
- #-*-Autoconf -*-
- # 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 ([INCLUDE/Str. H])
- Ac_config_header ([config. H])
- # Checks for programs.
- Ac_prog_cc
- # Checks for libraries.
- # Checks for header files.
- # Checks for typedefs, structures, and compiler characteristics.
- # Checks for library functions.
- Ac_output
Modify
[CPP]View plaincopy
- Ac_init (full-package-name, version, bug-Report-address)
Changed:
[CPP]View plaincopy
- Ac_init (STR, 0.0.1, [[email protected])
Full-package-name indicates the program name, version indicates the current version, and bug-Report-Address indicates the bug report address.
Then add two sentences:
Am_init_automake
Ac_config_files ([makefile])
The result is as follows)
[CPP]View plaincopy
- #-*-Autoconf -*-
- # Process this file with Autoconf to produce a configure script.
- Ac_prereq (2.59)
- # Ac_init (full-package-name, version, bug-Report-address)
- Ac_init (STR, 0.0.1, [[email protected])
- Am_init_automake
- Ac_config_srcdir ([INCLUDE/Str. H])
- Ac_config_header ([config. H])
- # Checks for programs.
- Ac_prog_cc
- # Checks for libraries.
- # Checks for header files.
- # Checks for typedefs, structures, and compiler characteristics.
- # Checks for library functions.
- Ac_config_files ([makefile])
- Ac_output
4. Execute aclocal
[CPP]View plaincopy
- [[Email protected] STR] # aclocal
- /Usr/share/aclocal/libfame. M4: 6: Warning: underquoted definition of am_path_libfame
- Run info' (automake) extending aclocal'
- Or see http://sources.redhat.com/automake/automake.html#Extending-aclocal
5. Create makefile. AM
[CPP]View plaincopy
- [[Email protected] STR] # vi makefile. AM
- # Makefile. AM
- Bin_programs = Str
- Str_sources = include/Str. h src/Str. c
- Str_cppflags =-I include/
This configuration file is required for the automake command. The meaning of each option is more intuitive, not to mention.
6. autoheader
[CPP]View plaincopy
- [[Email protected] STR] # autoheader
7. automake files are required:
[CPP]View plaincopy
- * Install-SH
- * Missing
- * Install
- * News
- * Readme
- * Authors
- * Changelog
- * Copying
- * Depcomp
The following files are automatically generated when automake-A is executed.
[CPP]View plaincopy
- * Install-SH
- * Missing
- * Install
- * Copying
- * Depcomp
Therefore, manually generate the remaining files.
[CPP]View plaincopy
- [[Email protected] STR] # Touch news readme authors changelog
8. Execute automake-.
[CPP]View plaincopy
- [[Email protected] STR] # automake-
- Configure. AC: Installing './install-Sh'
- Configure. AC: Installing './missing'
- Makefile. AM: Installing './install'
- Makefile. AM: Installing './copying'
- Makefile. AM: Installing './compile'
- Makefile. AM: Installing './depcomp'
9. Autoconf
[CPP]View plaincopy
- [[Email protected] STR] # Autoconf
- [[Email protected] STR] # ls
- Aclocal. M4 autoscan. Log config. H. In Configure. Scan include makefile. Am news
- Authors changelog configure copying install makefile. In readme
- Autom4te. cache compile Configure. AC depcomp install-SH missing SRC
10. perform the test:
Execute./configure
[CPP]View plaincopy
- [[Email protected] STR] #./configure -- prefix =/u
- Checking for a BSD-compatible install.../usr/bin/install-C
- Checking whether build environment is sane... yes
- Checking for gawk... gawk
- Checking whether make sets $ (make)... yes
- Checking for GCC... gcc
- Checking for C compiler default output file name... A. Out
- Checking whether the C compiler works... yes
- Checking whether we are cross compiling... no
- Checking for Suffix of executables...
- Checking for Suffix of object files... o
- Checking whether we are using the gnu c compiler... yes
- Checking whether GCC accepts-G... yes
- Checking for GCC option to accept ansi c... None needed
- Checking for style of include used by make... GNU
- Checking dependency style of GCC... gcc3
- Configure: Creating./config. Status
- Config. Status: Creating makefile
- Config. Status: Creating config. h
- Config. Status: config. H is unchanged
- Config. Status: executing depfiles commands
Execute make
[CPP]View plaincopy
- [[Email protected] STR] # Make
- Make all-AM
- Make [1]: Entering directory '/data/devel/C/str'
- If gcc-dhave_config_h-I. -I. -I. -I include/-g-O2-MT str-str.o-MD-MP-MF ". deps/str-str.Tpo "-c-o str-str.o 'test-F' src/Str. c' | echo '. /''src/Str. C ;\
- Then MV-F ". deps/str-str.Tpo" ". deps/str-str.Po"; else Rm-F ". deps/str-str.Tpo"; Exit 1; FI
- Gcc-g-O2-o str str-str.o
- Make [1]: Leaving directory '/data/devel/C/str'
At this time, STR has been generated (the executable file name goes to the top when the makefile. Am parameter is set before). You can directly view the running result through./Str.
[CPP]View plaincopy
- [[Email protected] calhost STR] #./Str
- Please input something end by [enter]
- Abcksdhfklsdklfdjlkfd
- ---- Print string ----
- "Abcksdhfklsdklfdjlkfd"
But here we do step by step, install it into the system, so that we only need to input Str on the terminal to run the program.
Run make install:
[CPP]View plaincopy
- [[Email protected] STR] # make install
- Make [1]: Entering directory '/data/devel/C/str'
- Test-z "/u/bin" | mkdir-p -- "/u/bin"
- /Usr/bin/install-C 'str''/u/bin/str'
- Make [1]: Nothing to be done for 'Install-data-am '.
- Make [1]: Leaving directory '/data/devel/C/str'
Next, you can make clean to clear the installed. O files.
In this way, an automatic makefile is generated.