Use commands such as automake to automatically generate Makefile

Source: Internet
Author: User
Tags automake

During programming in Linux, Makefile files are often used for automatic compilation to facilitate compilation. However, the writing of Makefile files is complicated and there are many rules. Fortunately, Linux provides us with the autoconf/automake tool that automatically generates fully functional Makefile files. This article describes how to use them to generate Makefile files. The tool groups to be used mainly include autoconf, automake, perl environment, and m4.

You can run the command rpm-qa | grep command_name to view their information. I checked the information under Ubuntu, but it is not installed by default, so I got it under Fedora.

The following is an example:

The workflow of the entire tool group is as follows:

The following example shows how to use automake to generate Makefile.

1. Write a simple program. In this example, we write a simple program for calculating the Fibonacci number, which consists of three files: main. c, fib. c, fib. h. The Code is as follows:

Main. c

  1 #include<stdio.h>
2 #include"fib.h"
3  int main()
4 {
5 int n=0;
6 printf("input n=\n");
7 scanf("%d",&n);
8 printf("fib(%d=%d\n",n,fib(n));
9
10 return 0;
11 }

Fib. h

 int fib(int n);

Fib. c

 1 #include"fib.h"
2  int fib(int n)
3 {
4 if(n==0)
5 return 0;
6 if(n==1||n==2)
7 return 1;
8 return fib(n-1)+fib(n-2);
9 }

Use the ls command to view the following information:

2. Use autoscan to generate the configure. scan file, rename it to configure. ac, and modify it as appropriate. As follows:

 

Configure. scan contains the following content:

Modify configure. ac as follows:

In AC_INIT (), enter the program name, version, and contact information of the author (generally by Email)

Add a line of AM_INIT_AUTOMAKE (), add the program name and version number to the parameter

The final file generated by AC_OUTPUT.

3. Use aclocal to generate aclocal. m4

4. Use autoconf to generate the configure file

5. Use the config. h. in file with autoheader

6. manually edit the Makefile. am file.

Item 1: software level. There are three candidates: foreign, gnu, and gnits.

Item 2: generated Executable File Name

Item 3: generate the original file required for the executable file. Separate multiple files with spaces. In this example

7. Use automake to generate the Makefile. in file with the following content:

8. Run configure configuration to generate the final Makefile file.

 

9 use Makefile to compile and run the program

You can also package and release the Code:

 

These commands are powerful and many parameters are not listed here. You can refer to relevant books and manuals. This article aims to show the entire process and the most common parameters. Will it be much easier to write programs, especially when there are too many files?

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.