Linux automake 命令及实用示例

LinuxLinuxBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

介绍

在本实验中,我们将学习 Automake 工具,它用于为软件项目生成 Makefile。Automake 是 GNU 构建系统的一部分,通常用于开源软件开发。我们将从安装使用 Automake 所需的软件包开始,然后创建一个基本的 Automake 项目并生成 Makefile。最后,我们将探索如何自定义 Automake 配置以满足项目的需求。

本实验的步骤涵盖了 Automake 的介绍、创建基本的 Automake 项目以及自定义 Automake 配置。实验旨在提供使用 Automake 管理软件构建过程的实践经验。

Linux 命令速查表


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) linux/BasicFileOperationsGroup -.-> linux/touch("`File Creating/Updating`") linux/PackagesandSoftwaresGroup -.-> linux/apt("`Package Handling`") subgraph Lab Skills linux/touch -.-> lab-422564{{"`Linux automake 命令及实用示例`"}} linux/apt -.-> lab-422564{{"`Linux automake 命令及实用示例`"}} end

Automake 介绍

在这一步中,我们将学习 Automake 工具,它用于为软件项目生成 Makefile。Automake 是 GNU 构建系统的一部分,通常用于开源软件开发。

Automake 是一个工具,它可以从一个名为 Makefile.am 的高级描述文件中自动生成 Makefile。该文件包含了构建、安装和分发软件的指令。Automake 负责生成 Makefile 的细节,使开发者能够专注于项目结构和构建过程。

让我们从安装使用 Automake 所需的软件包开始:

sudo apt-get update
sudo apt-get install -y automake

示例输出:

Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [114 kB]
Get:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease [99.8 kB]
Get:5 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages [588 kB]
Get:6 http://archive.ubuntu.com/ubuntu jammy/main amd64 Packages [1792 kB]
Get:7 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [748 kB]
Fetched 3451 kB in 2s (2032 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
automake is already the newest version (1:1.16.5-1.1ubuntu1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

现在我们已经安装了 Automake,接下来可以开始创建一个基本的 Automake 项目。

创建一个基本的 Automake 项目

在这一步中,我们将创建一个基本的 Automake 项目并生成 Makefile。

首先,让我们为项目创建一个新目录并进入该目录:

mkdir ~/project/automate-demo
cd ~/project/automate-demo

接下来,我们需要创建初始的 Automake 文件。我们将从 configure.ac 文件开始,该文件包含项目的高级配置:

touch configure.ac

在文本编辑器中打开 configure.ac 文件并添加以下内容:

AC_INIT([automate-demo], [1.0], [[email protected]])
AM_INIT_AUTOMAKE
AC_PROG_CC
AC_OUTPUT

此配置设置了项目名称、版本和维护者电子邮件地址,初始化了 Automake,并指定我们希望使用 C 编译器。

现在,让我们创建 Makefile.am 文件,该文件包含构建项目的指令:

touch Makefile.am

打开 Makefile.am 文件并添加以下内容:

AUTOMAKE_OPTIONS = foreign
bin_PROGRAMS = hello
hello_SOURCES = main.c

Makefile.am 文件指定我们希望从 main.c 源文件构建一个名为 "hello" 的程序。

最后一步是从 Automake 文件生成 Makefile:

autoreconf -i

此命令将生成 Makefile 以及项目所需的其他文件。

示例输出:

autoreconf: Entering directory '.'
autoreconf: configure.ac: not using Gettext
autoreconf: running: aclocal --force
autoreconf: configure.ac: tracing
autoreconf: running: libtoolize --copy --force
libtoolize: putting auxiliary files in AC_CONFIG_AUX_DIR, 'config'.
libtoolize: copying file 'config/ltmain.sh'
libtoolize: putting macros in AC_CONFIG_MACRO_DIRS, 'config'.
autoreconf: running: /usr/bin/autoconf --force
autoreconf: running: /usr/bin/autoheader --force
autoreconf: running: automake --add-missing --copy --force-missing
autoreconf: Leaving directory '.'

现在,我们已经设置了一个基本的 Automake 项目,可以继续下一步以自定义配置。

自定义 Automake 配置

在这一步中,我们将自定义 Automake 配置,为项目添加更多功能。

首先,让我们创建一个简单的 C 程序,我们将使用 Automake 构建它。在 ~/project/automate-demo 目录中创建一个名为 main.c 的新文件,内容如下:

#include <stdio.h>

int main() {
    printf("Hello, Automake!\n");
    return 0;
}

现在,让我们更新 Makefile.am 文件以包含新的 main.c 文件,并添加一个自定义目标:

cat << EOF > ~/project/automate-demo/Makefile.am
AUTOMAKE_OPTIONS = foreign
bin_PROGRAMS = hello
hello_SOURCES = main.c
install-data-local:
    @echo "Custom install step"
EOF

新的 install-data-local 目标将在 make install 步骤中执行。

接下来,让我们更新 configure.ac 文件以添加一个自定义配置选项:

cat << EOF > ~/project/automate-demo/configure.ac
AC_INIT([automate-demo], [1.0], [[email protected]])
AM_INIT_AUTOMAKE
AC_PROG_CC
AC_ARG_ENABLE([debug],
              [AS_HELP_STRING([--enable-debug],
                             [enable debugging])],
              [debug=yes], [debug=no])
AC_MSG_CHECKING([whether to enable debugging])
AC_MSG_RESULT([$debug])
AM_CONDITIONAL([DEBUG], [test "$debug" = yes])
AC_OUTPUT
EOF

此配置添加了一个 --enable-debug 选项,可用于在构建项目时启用调试。

现在,让我们生成 Makefile 并构建项目:

cd ~/project/automate-demo
autoreconf -i
./configure
make

示例输出:

checking whether to enable debugging... no
make  all-am
make[1]: Entering directory '/home/labex/project/automate-demo'
gcc -DPACKAGE_NAME=\"automate-demo\" -DPACKAGE_TARNAME=\"automate-demo\" -DPACKAGE_VERSION=\"1.0\" -DPACKAGE_STRING=\"automate-demo\ 1.0\" -DPACKAGE_BUGREPORT=\"[email protected]\" -DPACKAGE_URL=\"\" -DSTDC_HEADERS=1 -I. -g -O2 -MT hello-main.o -MD -MP -MF .deps/hello-main.Tpo -c -o hello-main.o main.c
mv -f .deps/hello-main.Tpo .deps/hello-main.Po
gcc -g -O2 -o hello hello-main.o
make[1]: Leaving directory '/home/labex/project/automate-demo'

现在我们可以安装项目:

sudo make install

示例输出:

make[1]: Entering directory '/home/labex/project/automate-demo'
 /usr/bin/mkdir -p '/usr/local/bin'
 /usr/bin/install -c 'hello' '/usr/local/bin/hello'
Custom install step
make[1]: Leaving directory '/home/labex/project/automate-demo'

在安装过程中,自定义的 install-data-local 目标已被执行。

总结

在本实验中,我们学习了 Automake 工具,它用于为软件项目生成 Makefile。我们从安装使用 Automake 所需的软件包开始,然后通过生成初始的 Automake 文件(包括 configure.acMakefile.am 文件)创建了一个基本的 Automake 项目。我们还学习了如何自定义 Automake 配置以满足项目的需求,例如添加源文件、库和安装目标。

Linux 命令速查表

您可能感兴趣的其他 Linux 教程