As we all know, writing a make file in C ++ in Linux requires a lot of small programs, but it is not good to directly write a large project by hand. the following example:
1. Test the Applet
Demo1
--------- Main. cpp
--------- Hello. h
--------- Hello. cpp
# Main. cpp
# Include "Hello. H" <br/> int main () <br/> {<br/> printhello (); <br/> return 0; <br/>}
# Hello. h
# Ifndef _ hello_h __< br/> # DEFINE _ hello_h __< br/> void printhello (); <br/> # endif
# Hello. cpp
# Include <stdio. h> <br/> # include "hello. H "<br/> void printhello () <br/> {<br/> printf (" % s/n "," Hello, world! "); <Br/> return; <br/>}< br/>
2. Manually write makefile
Touch a MAKEFILE file in the demo1 directory.
# Makefile
Hello: Hello. O main. O <br/> G ++ hello. O main. o-O hello <br/> RM hello. O main. O </P> <p> hello. o: Hello. h hello. CPP <br/> G ++-C hello. CPP-O hello. O <br/> main. o: Hello. h main. CPP <br/> G ++-C main. CPP-O main. O
Execute Command
Make
./Hello
The running result is displayed.
3. Use automake and autoscanf tools to automatically generate makefile files
3.1 Under demo1
Execute autoscan to rename the generated Configure. Scan to configure. In.
And modify
# Before Modification
#-*-Autoconf-*-<br/> # process this file with Autoconf to produce a configure script. <br/> ac_prereq (2.61) <br/> ac_init (full-package-name, version, bug-Report-address) <br/> ac_config_srcdir ([Hello. CPP]) <br/> ac_config_header ([config. h]) <br/> # checks for programs. <br/> ac_prog_cxx <br/> ac_prog_cc <br/> # checks for libraries. <br/> # checks for header files. <br/> # checks for typedefs, structures, and compiler characteristics. <br/> # checks for library functions. <br/> ac_config_files ([makefile]) <br/> ac_output <br/>
# Pay Attention to comparison after modification
#-*-Autoconf-*-<br/> # process this file with Autoconf to produce a configure script. <br/> ac_prereq (2.61) <br/> ac_init (testhello, 0.1, idehong@gmail.com) <br/> am_init_automake (testhello, 0.1) <br/> ac_config_srcdir ([Hello. CPP]) <br/> ac_config_header ([config. h]) <br/> # checks for programs. <br/> ac_prog_cxx <br/> ac_prog_cc <br/> # checks for libraries. <br/> # checks for header files. <br/> # checks for typedefs, structures, and compiler characteristics. <br/> # checks for library functions. <br/> ac_config_files ([makefile]) <br/> ac_output <br/>
3.2
Execute Command
Aclocal
The aclocal. M4 file is generated and used for subsequent commands.
Autoconf
The config. In file is generated.
Create makefile. Am. Each directory requires a new makefile. Am file.
# Makefile. AM
Automake_options = foreign <br/> bin_programs = demo1 <br/> demo1_sources = Main. cpp hello. cpp hello. H <br/>
Note that the makefile. Am file in the root folder must contain automake_options = foreign
Execute Command
Autoheader
Generate the config. H. In File
Automake -- add-Missing
3.3 Finally saw the configure file
Execute Command
./Configure
Make
The demo1 file is generated.
3.4
Execute Command
./Demo1
The running result is displayed.
Hello, world!
Manual makefile compilation for small projects is quite simple, and it is difficult to prepare complicated projects. We need to consider dependencies. Instead, we should hand over these troublesome tasks to autoscan and automake. They are better than doing these tasks.