Makefile Automatic Generation Tool ----- use of autotools (details)

Source: Internet
Author: User
Tags automake

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
  1. # Include <stdio. h>
  2. Int STR (char * string );


Program: src/Str. c

[CPP]View plaincopy
  1. # Include "str. H"
  2. // Print string
  3. Int STR (char * string ){
  4. Printf ("\ n ---- print string ---- \ n \" % s \ "\ n", string );
  5. Return 0;
  6. }
  7. // Interface of this program
  8. Int main (INT argc, char ** argv ){
  9. Char str_read [1024];
  10. Printf ("Please input something end by [enter] \ n ");
  11. Scanf ("% s", str_read );
  12. Return STR (str_read );
  13. }


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
  1. [[Email protected] STR] # ls
  2. Include SRC
  3. [[Email protected] STR] # autoscan
  4. Autom4te: Configure. AC: no such file or directory
  5. Autoscan:/usr/bin/autom4te failed with exit status: 1
  6. [[Email protected] STR] # ls
  7. Autoscan. Log Configure. Scan include SRC
  8. [[Email protected] STR] # cp Configure. Scan Configure. AC


Modify Configure. AC

 

 

 

[CPP]View plaincopy
  1. #-*-Autoconf -*-
  2. # Process this file with Autoconf to produce a configure script.
  3. Ac_prereq (2.59)
  4. Ac_init (full-package-name, version, bug-Report-address)
  5. Ac_config_srcdir ([INCLUDE/Str. H])
  6. Ac_config_header ([config. H])
  7. # Checks for programs.
  8. Ac_prog_cc
  9. # Checks for libraries.
  10. # Checks for header files.
  11. # Checks for typedefs, structures, and compiler characteristics.
  12. # Checks for library functions.
  13. Ac_output




Modify

[CPP]View plaincopy
  1. Ac_init (full-package-name, version, bug-Report-address)


Changed:

 

 

 

[CPP]View plaincopy
  1. 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
  1. #-*-Autoconf -*-
  2. # Process this file with Autoconf to produce a configure script.
  3. Ac_prereq (2.59)
  4. # Ac_init (full-package-name, version, bug-Report-address)
  5. Ac_init (STR, 0.0.1, [[email protected])
  6. Am_init_automake
  7. Ac_config_srcdir ([INCLUDE/Str. H])
  8. Ac_config_header ([config. H])
  9. # Checks for programs.
  10. Ac_prog_cc
  11. # Checks for libraries.
  12. # Checks for header files.
  13. # Checks for typedefs, structures, and compiler characteristics.
  14. # Checks for library functions.
  15. Ac_config_files ([makefile])
  16. Ac_output



4. Execute aclocal

[CPP]View plaincopy
  1. [[Email protected] STR] # aclocal
  2. /Usr/share/aclocal/libfame. M4: 6: Warning: underquoted definition of am_path_libfame
  3. Run info' (automake) extending aclocal'
  4. Or see http://sources.redhat.com/automake/automake.html#Extending-aclocal


5. Create makefile. AM

[CPP]View plaincopy
  1. [[Email protected] STR] # vi makefile. AM
  2. # Makefile. AM
  3. Bin_programs = Str
  4. Str_sources = include/Str. h src/Str. c
  5. 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
  1. [[Email protected] STR] # autoheader


7. automake files are required:

[CPP]View plaincopy
  1. * Install-SH
  2. * Missing
  3. * Install
  4. * News
  5. * Readme
  6. * Authors
  7. * Changelog
  8. * Copying
  9. * Depcomp


The following files are automatically generated when automake-A is executed.

 

 

 

[CPP]View plaincopy
  1. * Install-SH
  2. * Missing
  3. * Install
  4. * Copying
  5. * Depcomp


Therefore, manually generate the remaining files.

 

 

 

[CPP]View plaincopy
  1. [[Email protected] STR] # Touch news readme authors changelog


8. Execute automake-.

 

 

 

[CPP]View plaincopy
  1. [[Email protected] STR] # automake-
  2. Configure. AC: Installing './install-Sh'
  3. Configure. AC: Installing './missing'
  4. Makefile. AM: Installing './install'
  5. Makefile. AM: Installing './copying'
  6. Makefile. AM: Installing './compile'
  7. Makefile. AM: Installing './depcomp'


9. Autoconf

[CPP]View plaincopy
  1. [[Email protected] STR] # Autoconf
  2. [[Email protected] STR] # ls
  3. Aclocal. M4 autoscan. Log config. H. In Configure. Scan include makefile. Am news
  4. Authors changelog configure copying install makefile. In readme
  5. Autom4te. cache compile Configure. AC depcomp install-SH missing SRC


10. perform the test:
Execute./configure

[CPP]View plaincopy
  1. [[Email protected] STR] #./configure -- prefix =/u
  2. Checking for a BSD-compatible install.../usr/bin/install-C
  3. Checking whether build environment is sane... yes
  4. Checking for gawk... gawk
  5. Checking whether make sets $ (make)... yes
  6. Checking for GCC... gcc
  7. Checking for C compiler default output file name... A. Out
  8. Checking whether the C compiler works... yes
  9. Checking whether we are cross compiling... no
  10. Checking for Suffix of executables...
  11. Checking for Suffix of object files... o
  12. Checking whether we are using the gnu c compiler... yes
  13. Checking whether GCC accepts-G... yes
  14. Checking for GCC option to accept ansi c... None needed
  15. Checking for style of include used by make... GNU
  16. Checking dependency style of GCC... gcc3
  17. Configure: Creating./config. Status
  18. Config. Status: Creating makefile
  19. Config. Status: Creating config. h
  20. Config. Status: config. H is unchanged
  21. Config. Status: executing depfiles commands


Execute make

[CPP]View plaincopy
  1. [[Email protected] STR] # Make
  2. Make all-AM
  3. Make [1]: Entering directory '/data/devel/C/str'
  4. 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 ;\
  5. Then MV-F ". deps/str-str.Tpo" ". deps/str-str.Po"; else Rm-F ". deps/str-str.Tpo"; Exit 1; FI
  6. Gcc-g-O2-o str str-str.o
  7. 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
  1. [[Email protected] calhost STR] #./Str
  2. Please input something end by [enter]
  3. Abcksdhfklsdklfdjlkfd
  4. ---- Print string ----
  5. "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
  1. [[Email protected] STR] # make install
  2. Make [1]: Entering directory '/data/devel/C/str'
  3. Test-z "/u/bin" | mkdir-p -- "/u/bin"
  4. /Usr/bin/install-C 'str''/u/bin/str'
  5. Make [1]: Nothing to be done for 'Install-data-am '.
  6. 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.

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.