文章目录

  • 总结
  • 1. 安装automake,创建hello.c文件
  • 2. 使用autoscan自动检查hello.c编译所需的文件等
  • 3. 执行aclocal,生成aclocal.m4 配置文件
  • 4. 执行autoconf,生成configure文件
  • 5. 执行autoheader,生成config.h.in
  • 6. 创建Makefile.am文件,写入相关内容
  • 7. 根据实际情况创建常用文档文件
  • 8. 执行automake,生成Makefile.in
  • 9. 执行./configure生成最终的Makefile
  • 10. 运行make和make install
  • 11. 一图流总结
  • 参考

总结

以hello.c为例介绍了C文件如何使用autoconf和automake工具自动生成Makefile

1. 安装automake,创建hello.c文件

sudo apt-get install automake

最好是在一个空文件夹下创建

kjay@kjay:~/Makefile_Tutorial/hello$ ls
hello.c
kjay@kjay:~/Makefile_Tutorial/hello$ cat hello.c
#include <stdio.h>
int main()
{printf("Hello, Linux World!\n");  return 0;
}

2. 使用autoscan自动检查hello.c编译所需的文件等

kjay@kjay:~/Makefile_Tutorial/hello$ autoscan
kjay@kjay:~/Makefile_Tutorial/hello$ ls
autoscan.log  configure.scan  hello.c

使用autoscan --help查看autoscan的作用

Examine source files in the directory tree rooted at SRCDIR, or the
current directory if none is given. Search the source files for
common portability problems, check for incompleteness of
“configure.ac”, and create a file “configure.scan” which is a
preliminary “configure.ac” for that package.

在源文件中检查有没有常见的可移植问题,检查configure.ac的完整性,如果没有configure.ac则创建configure.scan文件,configure.scan需要在之后重命名为configure.ac并做改动

kjay@kjay:~/Makefile_Tutorial/hello$ mv configure.scan configure.ac
kjay@kjay:~/Makefile_Tutorial/hello$ cat configure.ac
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.AC_PREREQ([2.69])
AC_INIT([hello], [1.0], [xxx@qq.com])
AM_INIT_AUTOMAKE # Add, automake
AC_CONFIG_SRCDIR([hello.c])
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_CONFIG_FILES([Makefile]) # Add, Create MakefileAC_OUTPUT

AM_INIT_AUTOMAKE提供automake所需参数,在GNU手册中,对于hello.c这样比较简单的情况,直接写在AC_INIT后就可以了

AC_OUTPUT的作用为

the AC_OUTPUT line is a closing command that actually produces the part of the script in charge of creating the files registered with AC_CONFIG_HEADERS and AC_CONFIG_FILES.

用于生成AC_CONFIG_HEADERS 和 AC_CONFIG_FILES定义的文件,这里定义Makefile,用于后续自动生成

3. 执行aclocal,生成aclocal.m4 配置文件

kjay@kjay:~/Makefile_Tutorial/hello$ aclocal
kjay@kjay:~/Makefile_Tutorial/hello$ ls
aclocal.m4  autom4te.cache  autoscan.log  configure.ac  hello.c

输入aclocal --help,可以看到它的作用:

Generate “aclocal.m4” by scanning “configure.ac” or “configure.in”

根据configure.ac或者configure.in生成aclocal.m4文件,它会扫描configure.ac文件中的宏定义,并加入aclocal.m4中

4. 执行autoconf,生成configure文件

kjay@kjay:~/Makefile_Tutorial/hello$ autoconf
kjay@kjay:~/Makefile_Tutorial/hello$ ls
aclocal.m4  autom4te.cache  autoscan.log  configure  configure.ac  hello.c

输入autoconf --help,可以看到它的作用

Generate a configuration script from a TEMPLATE-FILE if given, or configure.ac if present, or else configure.in. Output is sent to the standard output if TEMPLATE-FILE is given, else into configure.

根据configure.ac生成configure文件

5. 执行autoheader,生成config.h.in

config.h.in是config.h的前置文件

kjay@kjay:~/Makefile_Tutorial/hello$ autoheader
kjay@kjay:~/Makefile_Tutorial/hello$ ls
aclocal.m4  autom4te.cache  autoscan.log  config.h.in  configure  configure.ac  hello.c

输入autoheader --help,可以看到它的作用

Create a template file of C #define statements for configure to use. To this end, scan TEMPLATE-FILE, or configure.ac if present, or else configure.in.

扫描configure.ac生成config.h.in

6. 创建Makefile.am文件,写入相关内容

kjay@kjay:~/Makefile_Tutorial/hello$ cat Makefile.am
AUTOMAKE_OPTION=foreign
bin_PROGRAMS=hello
hello_SOURCES=hello.c

Makefile.am中存放着生成makefile的一系列指令

automake的严格等级为foreign,只检测必须的文件

以_PROGRAMS结尾的变量值是最后make时生成的可执行文件的名字,在这里是hello

bin_PROGRAMS代表最后可执行文件会被装在bin目录下,这里是/usr/local/bin,不同机器可能不一样

当执行automake时,会将.am文件的所有行复制到Makefile.in中,具体细节可以再查看GNU手册

7. 根据实际情况创建常用文档文件

kjay@kjay:~/Makefile_Tutorial/hello$ touch NEWS README AUTHORS ChangeLog
kjay@kjay:~/Makefile_Tutorial/hello$ ls
aclocal.m4  AUTHORS  autom4te.cache  autoscan.log  ChangeLog  config.h.in  configure  configure.ac  hello.c  Makefile.am  NEWS  README

不创建这些后续执行configure的时候会报错的 : )

8. 执行automake,生成Makefile.in

kjay@kjay:~/Makefile_Tutorial/hello$ automake --add-missing
configure.ac:11: installing './compile'
configure.ac:6: installing './install-sh'
configure.ac:6: installing './missing'
Makefile.am: installing './INSTALL'
Makefile.am: installing './COPYING' using GNU General Public License v3 file
Makefile.am:     Consider adding the COPYING file to the version control system
Makefile.am:     for your code, to avoid questions about which license your project uses
Makefile.am: installing './depcomp'

automake的功能是

Generate Makefile.in for configure from Makefile.am.

–add-missing参数的作用是

add missing standard files to package

9. 执行./configure生成最终的Makefile

kjay@kjay:~/Makefile_Tutorial/hello$ ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
checking for gawk... no
checking for mawk... mawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
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 ISO C89... none needed
checking whether gcc understands -c and -o together... yes
checking whether make supports the include directive... yes (GNU style)
checking dependency style of gcc... gcc3
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: executing depfiles commands
kjay@kjay:~/Makefile_Tutorial/hello$ ls
aclocal.m4  autom4te.cache  ChangeLog  config.h     config.log     configure     COPYING  hello.c  install-sh  Makefile.am  missing  README
AUTHORS     autoscan.log    compile    config.h.in  config.status  configure.ac  depcomp  INSTALL  Makefile    Makefile.in  NEWS     stamp-h1

PS: 使用**./configure --help**的时候可以看到自己设置的信息被添加在configure里了

10. 运行make和make install

kjay@kjay:~/Makefile_Tutorial/hello$ sudo make
make  all-am
make[1]: Entering directory '/home/kjay/Makefile_Tutorial/hello'
gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT hello.o -MD -MP -MF .deps/hello.Tpo -c -o hello.o hello.c
mv -f .deps/hello.Tpo .deps/hello.Po
gcc  -g -O2   -o hello hello.o
make[1]: Leaving directory '/home/kjay/Makefile_Tutorial/hello'
kjay@kjay:~/Makefile_Tutorial/hello$ ls
aclocal.m4  autom4te.cache  ChangeLog  config.h     config.log     configure     COPYING  hello    hello.o  install-sh  Makefile.am  missing  README
AUTHORS     autoscan.log    compile    config.h.in  config.status  configure.ac  depcomp  hello.c  INSTALL  Makefile    Makefile.in  NEWS     stamp-h1
kjay@kjay:~/Makefile_Tutorial/hello$ sudo make install
make[1]: Entering directory '/home/kjay/Makefile_Tutorial/hello'/usr/bin/mkdir -p '/usr/local/bin'/usr/bin/install -c hello '/usr/local/bin'
make[1]: Nothing to be done for 'install-data-am'.
make[1]: Leaving directory '/home/kjay/Makefile_Tutorial/hello'

make生成hello可执行文件,make install将hello可执行文件Install到/usr/local/bin中

kjay@kjay:~/Makefile_Tutorial/hello$ ./hello
Hello, Linux World!
kjay@kjay:~/Makefile_Tutorial/hello$ hello
Hello, Linux World!

11. 一图流总结

参考

GNU automake手册

GNU autoconf手册

autoconf automake使用

autoconf与automake使用简解相关推荐

  1. autoconf、automake详解

    创建工程的Makefile,编译工程 例解 autoconf 和 automake 生成 Makefile 文件 http://www.ibm.com/developerworks/cn/linux/ ...

  2. 例解 autoconf 和 automake 生成 Makefile 文件[转+个人修改]

    引子 无论是在Linux还是在Unix环境中,make都是一个非常重要的编译命令.不管是自己进行项目开发还是安装应用软件,我们都经常要用到make或 make install.利用make工具,我们可 ...

  3. CentOS下的Autoconf和AutoMake(实践篇) 2

    阅读过<Linux下的Autoconf和AutoMake(理论篇)>之后,进入到实践环节. 实验环境:CentOS release 6.7 (Final) x64 1.检查一下这4个工具是 ...

  4. 最新版freetextbox(版本3.1.6)在asp.net 2.0中使用简解

    最新版freetextbox(版本3.1.6)在asp.net 2.0中使用简解 2008-10-14 12:21 简介:对于FreeTextBox(版本3.1.6)在ASP.Net 2.0中使用,只 ...

  5. Linux下autoconf和automake使用

    Linux下autoconf和automake使用 转自:http://hi.baidu.com/liuyanqiong/blog/item/0a6f0ad9d28e1d3d32fa1c7b.html ...

  6. 最新版FreeTextBox(版本3.1.6)在ASP.Net 2.0中使用简解(提供博客园本地下载)

    来源:cleocn.com 最新版FreeTextBox(版本3.1.6)在ASP.Net 2.0中使用简解(提供博客园本地下载) 简介:对于FreeTextBox(版本3.1.6)在ASP.Net ...

  7. 爬虫5-BeautifulSoup模块简解2

    1.BeautifulSoup简解2 from bs4 import BeautifulSoup import re file = open("./baidu.html",'rb' ...

  8. github-markdown-css 使用简解,markdown文案格式优化(笔记)

    github-markdown-css 使用简解,markdown文案格式优化 1.npm 安装 $ npm install github-markdown-css 2.使用 导入github-mar ...

  9. Linux下autoconf与automake

    机器语言,汇编语言与高级语言 C语言的编译和链接 什么是Makefile? autoconf&automake 实例 机器语言,汇编语言与高级语言 机器语言是机器指令的集合.机器指令展开来讲就 ...

最新文章

  1. 让 PM 全面理解深度学习
  2. 返回浏览器或div 顶部
  3. python unicodeencodeerror_解决 Python UnicodeEncodeError 错误
  4. 学python要多少钱-参加python培训要多少钱?
  5. C#中的::运算符的作用
  6. Video : 将使用AD认证的SharePoint网站配置为表单方式登录
  7. 初一级模拟试题总结(2019.3.2)
  8. 函数式编程 -- 函数是一等公民、高阶函数、闭包
  9. python excel 提取特定行_Python之从Excel一列内提取数字
  10. idea2020新建一个jsp页面_有关idea2019版的jsp配置小教程
  11. Error while waiting for device: The emulator process for AVD Pixel_API_30 has terminated.
  12. SQL大圣之路笔记——SQL 行转列,列转行
  13. html状态码206,http状态码204/206/200理解
  14. android studio 使用lint工具优化app时全过程记录
  15. 多线程 ForkJoinPool
  16. 19-10-15(msgbox、inputbox、注释)
  17. 浅议极大似然估计(MLE)背后的思想原理
  18. 荷兰莱顿大学 计算机科学与技术,荷兰学校排名
  19. python2打包pyQT5
  20. 中国 Open Source Summit 演讲提案征集

热门文章

  1. 关于hi3516ev100调试zbar二维码和链接wifi问题
  2. python招聘现状-python招聘现状
  3. Git 报错:The ECDSA host key for gitlab.xx.net has changed, and the key for the corresponding.......
  4. http、https 等 常用默认端口号
  5. vscode运行python没有结果输出
  6. Linux部署lamp(centos 7),照做就行
  7. reverse()函数
  8. MongoDB索引原理及实践
  9. 手机上传文件到ftp服务器,上传文件到iPhone上的FTP服务器(Upload File to FTP Server on i...
  10. VC知识库之应用控制