Autoconf 学习笔记(2)

Autoconf 学习笔记(1) 里给出了一个最简单的例子说明使用autoconf的步骤. 现在以上次的configure.ac为基础, 添加几个与Unix网络编程(socket)有关的宏.

Makefile.am

AUTOMAKE_OPTIONS  =  foreign
SUBDIRS 
=  src

configure.ac

#                                                -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

#
# File: configure.ac
# Date: 2008-03-07
# Auth: mymtom@hotmail.com
#


AC_INIT(hello
,   1.0 . 2 ,  mymtom @hotmail . com)
AM_INIT_AUTOMAKE
AC_CONFIG_SRCDIR([src
/ hello . c])
AC_CONFIG_HEADER([config
. h])

#  Checks for programs.
AC_PROG_CC

#  Checks for libraries.

# Checks for header files.

#
# Checks for header resolv.h, checking for prerequisites first.
#
# XXX: why not work?
# AC_HEADER_RESOLV
#

AC_CHECK_HEADERS([sys / types .
                  sys
/ socket .
                  netinet
/ in .
                  apra
/ nameser .
                  netdb
. h])

AC_CHECK_HEADERS([resolv
. h] ,  [] ,  [] ,  [
# ifdef HAVE_SYS_TYPES_H
#  include <sys/types.h>
#endif
#ifdef HAVE_NETINET_IN_H
#  include <netinet/in.h>   /* inet_ functions / structs */
#endif
#ifdef HAVE_ARPA_NAMESER_H
#  include <arpa/nameser.h> /* DNS HEADER struct */
#endif
#ifdef HAVE_NETDB_H
#  include <netdb.h>
#endif

])

#  Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_SEARCH_LIBS( socket ,   socket )
AC_SEARCH_LIBS(
gethostbyname ,  nsl  socket )
AC_SEARCH_LIBS(hstrerror
,  resolv)

AC_CONFIG_FILES([Makefile
                 src
/ Makefile])
AC_OUTPUT

 src/Makefile.am

AUTOMAKE_OPTIONS  =  foreign
bin_PROGRAMS 
=  hello
hello_SOURCES 
=  hello . c

src/hello.c

/*
 * File: src/hello.c
 * Date: 2008-03-08
 * Auth: mymtom@hotmail.com
 
*/

#include 
" config.h "

/*
 * Macro: AC_HEADER_RESOLV
 *
 * Checks for header resolv.h, checking for prerequisites first. To
 * properly use resolv.h, your code should contain something like the
 * following: 
 
*/
#ifdef HAVE_SYS_TYPES_H
#  include 
< sys / types.h >
#endif
#ifdef HAVE_NETINET_IN_H
#  include 
< netinet / in .h >     /*  inet_ functions / structs  */
#endif
#ifdef HAVE_ARPA_NAMESER_H
#  include 
< arpa / nameser.h >   /*  DNS HEADER struct  */
#endif
#ifdef HAVE_NETDB_H
#  include 
< netdb.h >
#endif

#ifdef HAVE_RESOLV_H
#  include 
< resolv.h >
#endif

#include 
< stdio.h >
#include <stdlib.h>
#include 
< string .h >

#define  DAYTIME_PORT    8013
#define  MAXBUF          80
typedef 
struct  sockaddr SA;

static   void
usage(
void )
{
        (
void )fprintf(stderr,  " usage: hello host " );
        exit(
1 );
}

int
main(
int  argc,  char   * argv[])
{
        
int  s;
        ssize_t n;
        
char  buf[MAXBUF];
        
struct  sockaddr_in name;

        
if  (argc  !=   2 )
                usage();

        s 
=  socket(PF_INET, SOCK_STREAM,  0 );
        memset(
& name,  0 sizeof (name));
        name.sin_family 
=  AF_INET;
        name.sin_port 
=  htons(DAYTIME_PORT);
        inet_pton(AF_INET, argv[
1 ],  & name.sin_addr);
        connect(s, (SA 
* ) & name,  sizeof (name));
        
while  ( (n  =  read(s, buf,  sizeof (buf)))  >   0 ) {
                fwrite(buf, n, 
1 , stdout);
        }
        close(s);
        
return   0 ;
}

在FreeBSD6.2上configure和make的输出:

mymtom@freebsd $   ./ configure
checking 
for  a BSD-compatible install ...   / usr / bin / install -c
checking whether build environment is sane
...  yes
checking 
for  gawk ...  no
checking 
for  mawk ...  no
checking 
for  nawk ...  nawk
checking whether make sets 
$( MAKE )...  yes
checking 
for  gcc ...  gcc
checking 
for  C compiler default output file name ...  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
checking how to 
run  the C preprocessor ...  gcc -E
checking 
for  egrep ...  grep -E
checking 
for   ANSI  C header  files ...  yes
checking 
for  sys / types . h ...  yes
checking 
for  sys / stat . h ...  yes
checking 
for  stdlib . h ...  yes
checking 
for  string . h ...  yes
checking 
for  memory . h ...  yes
checking 
for  strings . h ...  yes
checking 
for  inttypes . h ...  yes
checking 
for  stdint . h ...  yes
checking 
for  unistd . h ...  yes
checking 
for  sys / types . h ...   ( cached )  yes
checking sys
/ socket . h usability ...  yes
checking sys
/ socket . h presence ...  yes
checking 
for  sys / socket . h ...  yes
checking netinet
/ in . h usability ...  yes
checking netinet
/ in . h presence ...  yes
checking 
for  netinet / in . h ...  yes
checking apra
/ nameser . h usability ...  no
checking apra
/ nameser . h presence ...  no
checking 
for  apra / nameser . h ...  no
checking netdb
. h usability ...  yes
checking netdb
. h presence ...  yes
checking 
for  netdb . h ...  yes
checking 
for  resolv . h ...  yes
checking 
for  library containing socket ...  none required
checking 
for  library containing gethostbyname ...  none required
checking 
for  library containing hstrerror ...  none required
configure: creating 
./ config . status
config
. status: creating Makefile
config
. status: creating src / Makefile
config
. status: creating config . h
config
. status: config . h is unchanged
config
. status: executing depfiles commands
mymtom@freebsd
$  make
make  all-recursive
Making all in src
if  gcc -DHAVE_CONFIG_H -I .  -I .  -I ..       -g -O2 -MT hello . o - MD  -MP -MF  " .deps/hello.Tpo "  -c -o hello . o hello . c ;    then  mv -f  " .deps/hello.Tpo "   " .deps/hello.Po " ;   else  rm -f  " .deps/hello.Tpo " ;   exit   1 ;  fi
gcc  -g -O2   -o hello  hello
. o  

在Solaris 10上configure和make的输出

mymtom@t1000 $  uname -a
SunOS t1000 
5.10  Generic_118833- 33  sun4v sparc SUNW , Sun-Fire-T1000 Solaris
mymtom@t1000
$   ./ configure
checking 
for  a BSD-compatible install ...   / usr / local / bin / install -c
checking whether build environment is sane
...  yes
checking 
for  gawk ...  no
checking 
for  mawk ...  no
checking 
for  nawk ...  nawk
checking whether make sets 
$( MAKE )...  yes
checking 
for  gcc ...  gcc
checking 
for  C compiler default output file name ...  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
checking how to 
run  the C preprocessor ...  gcc -E
checking 
for  egrep ...  egrep
checking 
for   ANSI  C header  files ...  yes
checking 
for  sys / types . h ...  yes
checking 
for  sys / stat . h ...  yes
checking 
for  stdlib . h ...  yes
checking 
for  string . h ...  yes
checking 
for  memory . h ...  yes
checking 
for  strings . h ...  yes
checking 
for  inttypes . h ...  yes
checking 
for  stdint . h ...  yes
checking 
for  unistd . h ...  yes
checking 
for  sys / types . h ...   ( cached )  yes
checking sys
/ socket . h usability ...  yes
checking sys
/ socket . h presence ...  yes
checking 
for  sys / socket . h ...  yes
checking netinet
/ in . h usability ...  yes
checking netinet
/ in . h presence ...  yes
checking 
for  netinet / in . h ...  yes
checking apra
/ nameser . h usability ...  no
checking apra
/ nameser . h presence ...  no
checking 
for  apra / nameser . h ...  no
checking netdb
. h usability ...  yes
checking netdb
. h presence ...  yes
checking 
for  netdb . h ...  yes
checking 
for  resolv . h ...  yes
checking 
for  library containing socket ...  -lsocket
checking 
for  library containing gethostbyname ...  -lnsl
checking 
for  library containing hstrerror ...  -lresolv
configure: creating 
./ config . status
config
. status: creating Makefile
config
. status: creating src / Makefile
config
. status: creating config . h
config
. status: executing depfiles commands
mymtom@t1000
$  make
make  all-recursive
Making all in src
if  gcc -DHAVE_CONFIG_H -I .  -I .  -I ..       -g -O2 -MT hello . o - MD  -MP -MF  " .deps/hello.Tpo "  -c -o hello . o hello . c ;  
then  mv -f  " .deps/hello.Tpo "   " .deps/hello.Po " ;   else  rm -f  " .deps/hello.Tpo " ;   exit   1 ;  fi
gcc  -g -O2   -o hello  hello
. o  -lresolv -lnsl -lsocket
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值