使用autoscan自动生成makefile文件

本教程使用的是centos7,当前目录下有5个C文件,任务是将他们生成makefile文件

 文件内容如下:

// add.c
int myadd(int a, int b)
{
    return a+b;
}

// mul.c
int mymul(int a, int b)
{
    return a*b;
}

// div.c
int mydiv(int a, int b)
{
    return a/b;
}

// x.h
int myadd(int a, int b);
int mymul(int a, int b);
int mydiv(int a, int b);

// x.c
#include <stdio.h>
#include "x.h"

int main()
{
    int a = 100;
    int b = 12;
    int add, mul, div;
    add = myadd(a, b);
    mul = mymul(a, b);
    div = mydiv(a, b);
    
    printf("%d + %d = %d
", a, b, add);
    printf("%d * %d = %d
", a, b, mul);
    printf("%d / %d = %d
", a, b, div);
    
    return 0;
}

1. 安装相关软件

CentOS的安装命令

sudo yum install autoconf automake

Ubuntu的安装命令

sudo apt-get install autoconf

2. 使用autoscan命令

autoscan

这时目录产生了两个新文件:

 3. 把configuire.scan重命名为configure.ac

mv configure.scan configure.ac

4. 修改configure.ac的文件内容

vim configure.ac

5. 使用aclocal命令,产生了几个新文件

aclocal

6. 使用autoconf命令

autoconf

7. 使用autoheader命令

autoheader

8. 编写 Makefile.am文件,写入如下信息,保存。注意Makefile.am文件中第2行、第3行中的demo与上面第4步configure.ac文件中的demo名字要一致。

vim Makefile.am
Makefile.am文件内容:
AUTOMAKE_OPTIONS=foreign bin_PROGRAMS=demo demo_SOURCES= x.c add.c mul.c div.c

9. 使用 automake --add-missing

automake --add-missing

10. 执行configure文件,此时生成了Makefile文件

./configure

11. 执行make,此时生成了之前设置的可执行文件demo

make

12. 运行生成的可执行文件demo

./demo

完成!

参考:https://blog.csdn.net/stoneliul/article/details/49094523

原文地址:https://www.cnblogs.com/FengZeng666/p/12411223.html