automake构建makefile文件

1. 安装automake

sevan@sevan-vm:MakefileLearn$ sudo apt install automake

2. 测试程序

建立两个目录:mkdir include source
进入include目录编辑sum.h文件内容如下:

sevan@sevan-vm:include$ cat sum.h 
/*
 * @Descripttion: 
 * @Author: Jaylen
 * @version: 
 * @Date: 2022-10-24 17:04:54
 * @LastEditors: Jaylen
 * @LastEditTime: 2022-10-24 17:58:39
 */
#ifndef _SUM_H_
#define _SUM_H_

int sum(int a, int b);

#endif

进入src目录编辑sum.c和main.c内容如下

sevan@sevan-vm:src$ cat sum.c 

#include "sum.h" 

int sum(int a, int b)
{  
    printf("\n----PRINT ARGS A: ----\n%d\n", a); 
    printf("\n----PRINT ARGS B: ----\n%d\n", b); 
    return (a + b);  
}

sevan@sevan-vm:src$ cat main.c 

#include "stdio.h"
#include "sum.h"

int main(int argc, char const *argv[])
{
    int a = 0, b = 0;  
    printf("Please INPUT ARGS A and B end by [ENTER]\n");  
    scanf("%d%d", &a, &b);
    int value = sum(a, b);
    printf("Execute SUM result: \n%d\n", value);  
    return 0;
}

3. 配置编译

3.1 autoscan扫描目录下的源文件

执行autoscan命令,会生成 autoscan.log 和 configure.scan 两个文件,如下:

sevan@sevan-vm:automake_demo$ tree 
.
├── include
│   └── sum.h
└── src
    ├── main.c
    └── sum.c

2 directories, 3 files
sevan@sevan-vm:automake_demo$ autoscan

sevan@sevan-vm:automake_demo$ tree
.
├── autoscan.log
├── configure.scan
├── include
│   └── sum.h
└── src
    ├── main.c
    └── sum.c

2 directories, 5 files

其中configure.scan为模版文件

sevan@sevan-vm:automake_demo$ cat configure.scan 
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.69])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([foo.h])
AC_CONFIG_HEADERS([config.h])

# Checks for programs.
AC_PROG_CC

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_OUTPUT

3.2 configure.ac配置

将configure.scan重命名为configure.ac

sevan@sevan-vm:automake_demo$ mv configure.scan configure.ac

修改configure.ac

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.69])
修改点[1]
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
修改后:
# FULL-PACKAGE-NAME 为程序名称,VERSION为当前版本, BUG-REPORT-ADDRESS为bug汇报地址
AC_INIT([sum], [1.0.1], [jaylensun@163.com])

AC_CONFIG_SRCDIR([src/sum.c])
AC_CONFIG_HEADERS([config.h])
# 修改点[2] 增加两行如下:
AM_INIT_AUTOMAKE([foreign]) # AM_INIT_AUTOMAKE代表调用automake函数进行生成,foreign 忽略生成Makefile时必须的Readme文件 
AC_CONFIG_FILES(Makefile)  # 配置生成文件名为Makefile

# Checks for programs.
AC_PROG_CC

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_OUTPUT

3.3 aclocal

利用 configure.ac 文件,执行命令 aclocal,会生成 aclocal.m4 和 autom4te.cache 两个文件,如下图:
在这里插入图片描述
重要的是aclocal.m4文件

3.4 autoheader

生产 config.h.in 文件
在这里插入图片描述

3.5 编写Makefile.am文件

vim Makefile.am
#Makefile.am  
bin_PROGRAMS = sum  
sum_SOURCES = include/sum.h src/sum.c src/main.c
sum_CPPFLAGS = -I include/ 

注意:sum为程序命名
如果将sum更改为demo,那么下面两个参数的前缀也需要对应更新,示例:
demo_SOURCES
demo_CPPFLAGS

3.6 autoconf

利用 aclocal.m4 文件,执行命令 autoconf ,会生成 configure 文件,如下图:
在这里插入图片描述

3.7 ./configure生成Makefile

执行 ./ configure 出现一下错误,缺少相应脚本文件
在这里插入图片描述
执行 autoreconf -i
在这里插入图片描述
再次执行 ./ configure 错误消失
在这里插入图片描述
生成Makefile文件如下
在这里插入图片描述

3.8 make

此时已经生成了sum(可执行文件名字在前面编写 Makefile.am 的参数时确定)这个,可以通过 ./sum 直接看到运行结果。
如下图:
在这里插入图片描述
不过linux系统中一般这里都会再做一步,把它安装到系统里面,这样我们只要在终端输入sum就可以运行程序。

3.9 make install

现在直接可以在终端运行程序。如下图:
在这里插入图片描述

4. 总结

step1: 执行autoscan,会生成configurae.scan,修改configure.scan内容之后,命名为configure.ac
step2: 配置configure.ac
step3: 执行aclocal,生成aclocal.m4
step4: 执行atuoheader,生成config.h.in
step5: 编写Makefile.am
step4: 执行autoconf,生成configure文件
step5: 执行automake --add-missing,生成Makefile.in文件
step6: 执行./configure --prefix=pwd,生成Makefile文件
step7: 执行make && make install,编译&安装二进制文件

警告信息

AM_INIT_AUTOMAKE调用是obsolete,您应该写AM_INIT_AUTOMAKE([foreign])。 
foreign告诉automake不要抱怨丢失README,ChangeLog等。
您的
Makefile.am与源文件位于不同的文件夹中。虽然没关系,但新的automake应该抱怨它是前向不兼容的,我在输出上得到了这个(automake 1.15):
Makefile.am:2: warning: source file 'src/main.c' is in a subdirectory,
Makefile.am:2: but option 'subdir-objects' is disabled
automake: warning: possible forward-incompatibility.
automake: At least a source file is in a subdirectory, but the 'subdir-objects'
automake: automake option hasn't been enabled.  For now, the corresponding output

这些只是警告。
无论如何,我没有只运行
autoconf,而是运行autoreconf -i(或-i -f以确保 - -f选项会覆盖所有文件工具生成)。 Autoreconf会以正确的顺序为您运行autoconf,automake和其他工具。

参考链接:https://www.gnu.org/software/automake/
https://zhuanlan.zhihu.com/p/77813702
https://www.gnu.org/software/automake/manual/html_node/Creating-amhello.html
https://www.cnblogs.com/Braveliu/p/11340132.html
https://www.bilibili.com/video/BV1oP4y1w7UH/?spm_id_from=333.880.my_history.page.click&vd_source=26ba94b64eab46ce6de368910d84a9ff

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Engineer-Jaylen_Sun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值