• 建立一个hello world的C项目,目标执行程序为helloworld

    main.c

#include <stdio.h>
#include "myfunc.h"
int main(int argc, char** argv)
{
    int a = 1;
    printf ("%s %d\n","Hello World", a+func(10));
    return 0;
}

myfunc.c

#include <stdio.h>
#include "myfunc.h"
int func(int a)
{
    return a * a;
}

myfunc.h

int func(int a);
  • 在当前目录下执行autoscan,扫描目录下源码,生成configure.scan。重命名configure.scan为configure.in,并做部分修改。

    configure.scan

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.68])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([main.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_OUTPUT

修改为(红色部分):

configure.in

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.68])
AC_INIT([helloworld], [1.0], [xiangzengzhou@gmail.com])
AC_CONFIG_SRCDIR([main.c])
AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE(helloworld,1.0)
# Checks for programs.
AC_PROG_CC(gcc)
# Checks for libraries.
# Checks for header files.
# Checks for typedefs, structures, and compiler characteristics.
# Checks for library functions.
AC_OUTPUT(Makefile)
  • 执行aclocal生成aclocal.m4

  • 执行autoconf生成configure

  • 执行autoheader生成config.h.in

  • 手工编写Makefile.am,例如下面内容

Makefile.am

AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=helloworld
helloworld_SOURCES=main.c myfunc.c myfunc.h

执行automake –add-missing根据Makefile.am生成Makefile.in * 执行./configure生成makefile

  • 执行make,编译项目生成目标执行程序helloworld

  • 执行程序./helloworld

Alt text



blog comments powered by Disqus

Published

20 September 2013

Category

programming

Tags