GNU make and automake

Source: Internet
Author: User

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.

 

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.