使用autotools编译发布hello.c

发布于:2025-03-02 ⋅ 阅读:(128) ⋅ 点赞:(0)

1、autotools简介

GNU Autotools 是一套用于创建可移植和易于维护的软件项目的工具集。它们主要用于生成配置脚本 (configure) 和 Makefiles,使得软件能够在不同的 Unix-like 系统上顺利编译和安装。

其中可移植,为在不同的Unix-like系统上。

官方文档:Autoconf

2、组件的使用流程

其中带星号的为使用组件。

3、示例:编译发布hello.c

  1. 创建简单的hello.c
    #include<stdio.h>
    
    int main()
    {
            printf("hello world!\n");
            return 0;
    }
    
  2. 使用autoscan,产生configure.scan,将configure.scan更改后缀名configure.ac(用于环境检测),此时在更改configure.ac文件内容。
    #                                               -*- Autoconf -*-
    # Process this file with autoconf to produce a configure script.
    
    AC_PREREQ([2.71])
    #初始化:包名,版本,出错发送邮箱
    AC_INIT([hello], [1.0], [1919441076@qq.com])
    #检查源代码目录是否正确
    AC_CONFIG_SRCDIR([hello.c])
    #配置头文件
    AC_CONFIG_HEADERS([config.h])
    #编译的选项和特性,如开启编译器警告、是否遵循GUN标准
    AM_INIT_AUTOMAKE(-Wall -Werror foreign)
    #指定生成的配置文件
    AC_CONFIG_FILES([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. 使用指令aclocal,生成aclocal.m4文件(宏展开,支持自定义,如交叉编译)。
  4. 使用指令autoheader生成config.h.in文件(in后缀的皆为中间文件,为后面文件的输入)。
  5. 使用指令autoconf生成configure可执行文件。
  6. 创建Makefile.am并编写(构建逻辑)
    #用于指定需要编译并安装到系统 bin 目录
    bin_PROGRAMS = hello
    #指定生成某个目标文件所需源文件列表的变量命名约定
    hello_SOURCES = hello.c
  7. 使用指令automake生成Makefile.in文件
  8. 运行前面生成的脚本configure指令生成Makefile文件和config.h文件。
  9. 使用make生成可执行文件,使用make dist生成源码包。