关于Makefile,Makefile.in,Makefile.am的问题

本文介绍如何使用autoconf、automake等工具自动生成Makefile文件,简化跨平台编译过程。通过具体步骤演示从创建源文件到生成可执行文件的全过程。

最近在研究开源的东西,发现有很多makefile.am,makefile.in,makefile文件,它们之间究竟是什么关系呢?能可以用什么工具生成吗?

 
   无论对于一个初学者还是一个资深的Linux程序员,编写Makefile文件都是一件很麻烦的事;再者,开发人员应该把主要的精力放在程序代码的编写上,而在Makefile文件花费太多的精力显然是不明智的;还有,对于不同的处理器架构,往往编译器不同,环境不同,特别是一些嵌入式系统中的各种程序的编译,于是移植问题也使Makefile文件编写趋于复杂,也显得这个问题很重要。对于这些问题Linux的高手们早已想到了,所以他们开发了一些能够自动生成Makefile文件的工具。他们就是下面这些工具:
〉GNU Automake
〉GNU Autoconf
〉GNU m4
〉perl
〉GNU Libtool
因此您的操作系统必须安装以上软件,否则就不能够自动生成Makefile文件或者在生成的过程中出现各种各样的问题。用 autoconf/automake/autoheader工具来处理各种移植性的问题,用这些工具完成系统配置信息的收集,制作Makefile文件。然后在打算编译源码时只需要通过 "./configure; make"这样简单的命令就可以得到干净利落的编译。

制作Makefile文件需要以下几步:
1〉建立编译目录和源代码目录及源代码文件(即要编译的源文件)
[root@localhost leaf]#mkdir testmk
[root@localhost leaf]#cd testmk
[root@localhost testmk]#vi hello.c
编辑hello.c文件如下内容:
/*filename:hello.c*/
#include <stdio.h>

int main(int argc,char **argv)
{
printf("%s/n","Hello, World!")
return 0;
}
2〉利用autoscan工具产生一个configure.in文件的模板文件configure.scan文件:
[root@localhost testmk]#autoscan
[root@localhost testmk]#ls
configure.scan hello.c
3〉把configure.scan文件更名为configure.in文件,并编译其内容如下:
[root@localhost testmk]#mv configure.scan configure.in
[root@localhost testmk]#vi configure.in
dnl Process this file with autoconf to produce a configure script.
AC_INIT(hello.c)

dnl Add the file by leaf
AM_INIT_AUTOMAKE(hello,1.0)

dnl Checks for programs.
AC_PROG_CC

dnl Checks for libraries.

dnl Checks for header files.

dnl Checks for typedefs, structures, and compiler characteristics.

dnl Checks for library functions.

AC_OUTPUT(Makefile)

4〉执行aclocal,会产生aclocal.m4文件
[root@localhost testmk]#aclocal
[root@localhost testmk]#ls
aclocal.m4 configure.in hello.c
5〉执行autoconf,会产生confiure文件
[root@localhost testmk]#autoconf
[root@localhost testmk]#ls
aclocal.m4 [autom4te.cache] configure configure.in hello.c
6〉创建文件Makefile.am并编辑其内容如下:
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=hello
hello_SOURCES=hello.c
其中,hello为编译后产生的可执行文件名称,而第三行等号后面为源文件列表
7〉执行automake程序,automake程序会根据Makefile.am产生一些文件,其中最重要的是Makefile.in文件:
[root@localhost testmk]#automake --add-missing
configure.in: installing `./install-sh'
configure.in: installing `./mkinstalldirs'
configure.in: installing `./missing'
Makefile.am: installing `./depcomp'
[root@localhost testmk]#ls
aclocal.m4 [autom4te.cache] configure configure.in depcomp
hello.c install-sh Makefile.am Makefile.in missing
mkinstalldirs
8〉执行configure脚本,生成我们需要的Makefile文件。
[root@localhost testmk]#./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for gcc... gcc
checking for C compiler default output... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables...
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ANSI C... none needed
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
configure: creating ./config.status
config.status: creating Makefile
config.status: executing depfiles commands
9〉最后只执行make就大功告成了:
[root@localhost testmk]#make
source='hello.c' object='hello.o' libtool=no /
depfile='.deps/hello.Po' tmpdepfile='.deps/hello.TPo' /
depmode=gcc3 /bin/sh ./depcomp /
gcc -DPACKAGE_NAME="" -DPACKAGE_TARNAME="" -DPACKAGE_VERSION="" -DPACKAGE_STRING="" -DPACKAGE_BUGREPORT="" -DPACKAGE="hello" -DVERSION="1.0" -I. -I. -g -O2 -c `test -f 'hello.c' || echo './'`hello.c
gcc -g -O2 -o hello hello.o

备注:
1.以上内容均在RedHat Linux 9.0环境下测试通过。
2.参考书目《Linux 程序设计权威指南》于明俭、陈向阳、方汉编著
3.其它国内外网站资料
4.RedHat 9.0下带的程序文件及版本
autoconf-2.57-3.noarch.rpm
automake-1.6.3-5.noarch.rpm
gcc-3.2.2-5.i386.rpm
<think>我们正在解决编译时出现的`libguile.h`缺失问题,并探讨`Makefile.am`和`Makefile.in`在自动生成流程中的作用。用户的问题集中在如何通过安装开发包、配置构建系统来解决头文件缺失,以及理解Autotools(automake)的工作流程。###解决`fatalerror:libguile.h:Nosuchfileordirectory`####1.**安装Guile开发包**必须安装GNUGuile的开发包(包含头文件和库)。根据系统选择:-**Debian/Ubuntu**:```bashsudoapt-getinstalllibguile-3.0-dev#安装Guile3.0开发包```-**RHEL/CentOS**:```bashsudoyuminstallguile-devel```-**Fedora**:```bashsudodnfinstallguile-devel```安装后,头文件通常位于`/usr/include/guile/3.0/`(版本号可能不同)[^1]。####2.**验证头文件路径**检查`libguile.h`是否存在:```bashfind/usr-name'libguile.h'2>/dev/null```####3.**配置构建环境**如果使用Autotools(`configure`脚本)构建项目:-确保开发包安装后重新运行`configure`:```bash./configure```它会自动检测Guile并设置正确的头文件路径[^2]。####4.**手动指定头文件路径(备选)**如果构建系统未自动处理,可手动添加编译标志:```bashCFLAGS="-I/usr/include/guile/3.0"./configure```---###理解`Makefile.am`和`Makefile.in`的自动生成流程Autotools(automake+autoconf)用于生成可移植的构建系统。流程如下:####1.**编写`Makefile.am`**-开发者定义项目构建规则(例如编译目标、依赖):```makefilebin_PROGRAMS=my_programmy_program_SOURCES=main.cmy_program_CPPFLAGS=$(GUILE_CFLAGS)#头文件搜索路径my_program_LDADD=$(GUILE_LIBS)#链接库```-`GUILE_CFLAGS`和`GUILE_LIBS`由`configure`脚本通过`pkg-config`填充[^3]。####2.**生成`Makefile.in`**-运行`automake`将`Makefile.am`转换为模板`Makefile.in`:```bashautomake--add-missing#通常与autoreconf一起使用```-`Makefile.in`包含占位符(如`@GUILE_CFLAGS@`),将在`configure`运行时被替换。####3.**生成`configure`脚本**-使用`autoconf`处理`configure.ac`生成`configure`:```bashautoconf```-`configure.ac`中需包含Guile依赖检查:```m4PKG_CHECK_MODULES([GUILE],[guile-3.0])#检查Guile并设置GUILE_CFLAGS/GUILE_LIBS```####4.**用户构建流程**用户执行标准步骤:```bash./configure#检测系统环境,生成Makefilemake#使用Makefile编译sudomakeinstall#安装```####依赖关系图```mermaidgraphLRA[Makefile.am]-->|automake|B[Makefile.in]C[configure.ac]-->|autoconf|D[configure]D-->|运行|E[Makefile]B-->E```>**关键点**:>-`Makefile.am`是开发者编写的抽象规则;>-`Makefile.in`是Autotools生成的中间模板;>-`configure`根据系统配置将`Makefile.in`转为最终`Makefile`[^4]。§§相关问题§§1.Autotools中`configure.ac`如何检查第三方库的存在?2.如何为项目添加自定义的编译选项(如启用调试模式)?3.当`./configure`报告“Guilenotfound”时如何解决?4.`pkg-config`在构建过程中起什么作用?[^1]:开发包提供头文件和链接库,编译器需要它们完成预处理和链接。[^2]:`configure`脚本通过环境变量或`pkg-config`探测依赖库路径。[^3]:`pkg-config`提供标准化的编译和链接标志查询接口。[^4]:Autotools通过生成脚本适配不同Unix系统,解决兼容性问题。</think>### 解决 "fatal error: libguile.h: No such file or directory" 及 Makefile 自动生成流程 #### 一、问题根本原因 该错误表明编译时缺少 GNU Guile 库的开发文件(头文件和链接库)。解决方案需同时处理**依赖安装**和**构建系统配置**。 #### 二、完整解决步骤 1. **安装 Guile 开发包** ```bash # Debian/Ubuntu sudo apt-get install libguile-3.0-dev # 或 libguile-2.2-dev # RHEL/CentOS sudo yum install guile-devel # Fedora sudo dnf install guile-devel ``` 验证安装: ```bash find /usr -name 'libguile.h' 2>/dev/null # 典型输出: /usr/include/guile/3.0/libguile.h ``` 2. **配置构建系统(Autotools 流程)** Autotools 构建链的关系: ```mermaid graph LR A[configure.ac] -->|autoconf| B[configure] C[Makefile.am] -->|automake| D[Makefile.in] B -->|运行| E[Makefile] D --> E ``` **关键文件作用**: - `configure.ac`:定义项目依赖和系统检查 - `Makefile.am`:声明构建规则 - `Makefile.in`:Autotools 生成的模板 - `Makefile`:最终构建文件 3. **添加 Guile 依赖检测** 在 `configure.ac` 中添加: ```m4 PKG_CHECK_MODULES([GUILE], [guile-3.0]) AC_SUBST(GUILE_CFLAGS) AC_SUBST(GUILE_LIBS) ``` 4. **配置 Makefile.am** ```makefile bin_PROGRAMS = your_program your_program_SOURCES = main.c your_program_CPPFLAGS = $(GUILE_CFLAGS) # 头文件路径 your_program_LDADD = $(GUILE_LIBS) # 链接库 ``` 5. **重新生成构建系统** ```bash autoreconf -fiv # 重新生成 configure 和 Makefile.in ./configure # 检测依赖并生成 Makefile make # 正常编译 ``` #### 三、工作流程详解 1. **开发者编写**: - `configure.ac`:使用 M4 宏定义依赖检查 - `Makefile.am`:声明目标、源文件和编译规则 2. **生成中间文件**: ```bash aclocal # 收集宏 autoconf # 生成 configure 脚本 automake --add-missing # 生成 Makefile.in ``` 3. **用户构建**: ```bash ./configure # 扫描系统环境,替换 Makefile.in 中的变量 make # 使用最终 Makefile 编译 ``` > **关键点**: > `PKG_CHECK_MODULES` 会通过 `pkg-config` 查询 `guile-3.0.pc` 文件,自动设置 `GUILE_CFLAGS` 和 `GUILE_LIBS`[^1]。 > `Makefile.in` 中的占位符如 `@GUILE_CFLAGS@` 在 `./configure` 阶段被替换为实际路径[^2]。 #### 四、验证配置结果 检查生成的 `Makefile` 中是否包含正确路径: ```makefile # 应包含类似内容: your_program_CPPFLAGS = -I/usr/include/guile/3.0 your_program_LDADD = -lguile-3.0 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值