linux下autoconf命令实战

  • linux下autoconf命令实战已关闭评论
  • 221 views
  • A+
所属分类:linux命令

它其实是一个工具,

安装

ubuntu下:

apt-get install autoconf

centos下:

yum install autoconf

我们先看看官网给的一个流程

your source files --> [autoscan*] --> [configure.scan] --> configure.ac
 
 configure.ac --.
 | .------> autoconf* -----> configure
 [aclocal.m4] --+---+
 | `-----> [autoheader*] --> [config.h.in]
 [acsite.m4] ---'
 
 Makefile.in

实例

打包

首先我们写一个.c文件,用于下面的测试,内容如下:

[root@localhost xubo]# vi max.c
#include <stdio.h>
int main()
{
 int a = 100;
 int b = 200;
 int ret;
 ret = max(a, b);
 return ret;
}
int max(int num1, int num2)
{
 /* 局部变量声明 */
 int result;

if (num1 > num2)
 result = num1;
 else
 result = num2;
return result;
}

autoscan

下来跟着图走,使用autoscan生成 configure.scan 文件:,如下:

[root@localhost xubo]# ls
max.c
[root@localhost xubo]# autoscan 
[root@localhost xubo]# ls
autoscan.log configure.scan max.c

生成了两个文件,一个日子,一个是我们想要的configure.scan

configure.ac

下一步,修改configure.scan为configure.ac,然后修改文件内容,如下:

[root@localhost xubo]# more configure.ac 
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.69])
#添加版本相关信息
AC_INIT(mymax, 1.0,xubogood@163.com)
AC_CONFIG_SRCDIR([max.c])
#中括号去掉
AC_CONFIG_HEADERS(config.h)
#添加如下内容,后面要用
AM_INIT_AUTOMAKE(mymax,1.0)
# Checks for programs.
AC_PROG_CC

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

#输出文件指定为 Makefile
AC_OUTPUT(Makefile)

aclocal

下一步生成使用aclocal生成.m4,如下:

[root@localhost xubo]# aclocal
[root@localhost xubo]# ls
aclocal.m4 autom4te.cache autoscan.log configure.ac max.c

autoconf

下一步使用autoconf生成configure,如下

[root@localhost xubo]# autoconf 
[root@localhost xubo]# ls
aclocal.m4 autom4te.cache autoscan.log configure configure.ac max.c

 autoheader

下一步使用autoheader,这个看你的configure.ac怎么配置了,如果有AC_CONFIG_HEADERS(config.h),就得执行,没有就不用执行了,执行完成后,如下:

[root@localhost xubo]# autoheader
[root@localhost xubo]# ls
aclocal.m4 autom4te.cache autoscan.log configure configure.ac max.c config.h.in config.log

下一步创建配置 Makefile.am

Makefile.am

关于语法和用法,请看另一篇文章 实战Makefile.am

新建NEWS、 README、 ChangeLog 、AUTHORS文件,不然使用automake会报错

[root@localhost xubo]#touch NEWS README AUTHORS ChangeLog

增加Makefile.am,内容如下:

[root@localhost xubo]# more Makefile.am 
bin_PROGRAMS = max
max_SOURCES = max.c

automake

下来执行automake,如下:

[root@localhost xubo]# automake
configure.ac:9: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated. For more info, see:
configure.ac:9: http://www.gnu.org/software/automake/manual/automake.html#Modernize-AM_005fINIT_005fAUTOMAKE-invocation
Makefile.am:2: warning: variable 'max_SOURCES' is defined but no program or
Makefile.am:2: library has 'max' as canonical name (possible typo)

执行成功了,就是有些警告,如果你不想看警告的话,直接带上--add-missing参数就行了

打包

生成Makefile文件,执行configure,如下:

[root@localhost xubo]# ls
aclocal.m4 autom4te.cache ChangeLog config.log configure.ac depcomp install-sh Makefile.in missing README
AUTHORS autoscan.log config.h.in configure COPYING INSTALL Makefile.am max.c NEWS

[root@localhost xubo]# ./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... gawk
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 for style of include used by make... GNU
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

[root@localhost xubo]# ls
aclocal.m4 autoscan.log config.h.in configure depcomp Makefile max.c README
AUTHORS ChangeLog config.log configure.ac INSTALL Makefile.am missing stamp-h1
autom4te.cache config.h config.status COPYING install-sh Makefile.in NEWS

生成好了后执行

make dist

打包,如下:

[root@localhost xubo]# make dist
make dist-gzip am__post_remove_distdir='@:'
make[1]: 进入目录“/xubo”
if test -d "mymax-1.0"; then find "mymax-1.0" -type d ! -perm -200 -exec chmod u+w {} ';' && rm -rf "mymax-1.0" || { sleep 5 && rm -rf "mymax-1.0"; }; else :; fi
test -d "mymax-1.0" || mkdir "mymax-1.0"
test -n "" \
|| find "mymax-1.0" -type d ! -perm -755 \
 -exec chmod u+rwx,go+rx {} \; -o \
 ! -type d ! -perm -444 -links 1 -exec chmod a+r {} \; -o \
 ! -type d ! -perm -400 -exec chmod a+r {} \; -o \
 ! -type d ! -perm -444 -exec /bin/sh /xubo/install-sh -c -m a+r {} {} \; \
|| chmod -R a+r "mymax-1.0"
tardir=mymax-1.0 && ${TAR-tar} chof - "$tardir" | GZIP=--best gzip -c >mymax-1.0.tar.gz
make[1]: 离开目录“/xubo”
if test -d "mymax-1.0"; then find "mymax-1.0" -type d ! -perm -200 -exec chmod u+w {} ';' && rm -rf "mymax-1.0" || { sleep 5 && rm -rf "mymax-1.0"; }; else :; fi

[root@localhost xubo]# ls

aclocal.m4 autoscan.log config.h.in configure COPYING install-sh Makefile.in mymax-1.0.tar.gz stamp-h1
AUTHORS ChangeLog config.log configure.ac depcomp Makefile max.c NEWS
autom4te.cache config.h config.status configure.scan INSTALL Makefile.am missing README

测试

[root@localhost xubo]# tar zxvf mymax-1.0.tar.gz 
mymax-1.0/
mymax-1.0/INSTALL
mymax-1.0/NEWS
mymax-1.0/README
mymax-1.0/AUTHORS
mymax-1.0/ChangeLog
mymax-1.0/COPYING
mymax-1.0/Makefile.in
mymax-1.0/Makefile.am
mymax-1.0/configure
mymax-1.0/configure.ac
mymax-1.0/aclocal.m4
mymax-1.0/config.h.in
mymax-1.0/depcomp
mymax-1.0/install-sh
mymax-1.0/missing
mymax-1.0/max.c

[root@localhost xubo]# cd mymax-1.0

[root@localhost mymax-1.0]# ./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... gawk
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 for style of include used by make... GNU
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

[root@localhost mymax-1.0]# make
make all-am
make[1]: 进入目录“/xubo/mymax-1.0”
gcc -DHAVE_CONFIG_H -I. -g -O2 -MT max.o -MD -MP -MF .deps/max.Tpo -c -o max.o max.c
mv -f .deps/max.Tpo .deps/max.Po
gcc -g -O2 -o max max.o 
make[1]: 离开目录“/xubo/mymax-1.0”

[root@localhost mymax-1.0]# make install
make[1]: 进入目录“/xubo/mymax-1.0”
 /usr/bin/mkdir -p '/usr/local/bin'
 /usr/bin/install -c max '/usr/local/bin'
make[1]: 对“install-data-am”无需做任何事。
make[1]: 离开目录“/xubo/mymax-1.0”

[root@localhost mymax-1.0]# max

[root@localhost mymax-1.0]# echo $?
200

 

ok了

  • 安卓客户端下载
  • 微信扫一扫
  • weinxin
  • 微信公众号
  • 微信公众号扫一扫
  • weinxin
avatar