autotools工具的使用

本文介绍如何使用Autoconf和Automake工具构建C语言项目。从配置configure.ac文件开始,通过aclocal、autoconf等命令生成配置脚本,并利用Automake创建Makefile.am,最终形成可配置的Makefile。此过程适用于需要跨平台编译和安装的软件项目。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

root@jdqiu:/home/qjd/project# ls
include  lib  main.c
root@jdqiu:/home/qjd/project#
root@jdqiu:/home/qjd/project# ls include/
test.h
root@jdqiu:/home/qjd/project# ls lib/
test.c
root@jdqiu:/home/qjd/project#


/* project/main.c */
#include
#include "include/test.h"
int main()
{
    printf("main entrance./n");
    test_method();
    return 0;
}

/* project/lib/test.c */
#include
#include "../include/test.h"
void test_method()
{
    printf("test method./n");
}

/* project/include/test.h*/
void test_method();

/project# autoscan

root@jdqiu:/home/qjd/project# ls

autoscan.log  configure.scan  include  lib  main.c

root@jdqiu:/home/qjd/project# mv configure.scan configure.ac

root@jdqiu:/home/qjd/project# vi configure.ac

#                                               -*- Autoconf -*-

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

 

AC_PREREQ([2.67])

#AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])

AC_INIT(hello, 1.0, 921751743@qq.com)

AM_INIT_AUTOMAKE(hello,1.0)

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_CONFIG_FILES([Makefile])

 

AC_OUTPUT

root@jdqiu:/home/qjd/project# aclocal

root@jdqiu:/home/qjd/project# ls

aclocal.m4  autom4te.cache  configure.ac  include  lib  main.c  

root@jdqiu:/home/qjd/project# autoconf

root@jdqiu:/home/qjd/project# ls

aclocal.m4  autom4te.cache  configure  configure.ac  include  lib  main.c 

root@jdqiu:/home/qjd/project# autoheader

root@jdqiu:/home/qjd/project# ls

aclocal.m4      config.h.in  configure.ac  lib

autom4te.cache  configure    include       main.c 

手工创建Makefile.am文件。Automake工具会根据 configure.in 中的参量把 Makefile.am 转换成 Makefile.in 文件。

root@jdqiu:/home/qjd/project# vi Makefile.am  

AUTOMAKE_OPTIONS = foreign

bin_PROGRAMS = hello

hello_SOURCES = main.c include/test.h lib/test.c

 

root@jdqiu:/home/qjd/project# automake --add-missing

configure.ac:7: installing `./install-sh'

configure.ac:7: installing `./missing'

Makefile.am: installing `./depcomp'

root@jdqiu:/home/qjd/project# automake

root@jdqiu:/home/qjd/project# ls

aclocal.m4      configure     include     main.c       missing

autom4te.cache  configure.ac  install-sh  Makefile.am

config.h.in     depcomp       lib         Makefile.in

root@jdqiu:/home/qjd/project# ./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... /bin/mkdir -p

checking for gawk... no

checking for mawk... mawk

checking whether make sets $(MAKE)... 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

configure: creating ./config.status

config.status: creating Makefile

config.status: creating config.h

config.status: executing depfiles commands

root@jdqiu:/home/qjd/project# ls

aclocal.m4      config.log     depcomp     main.c       missing

autom4te.cache  config.status  include     Makefile     stamp-h1

config.h        configure      install-sh  Makefile.am

config.h.in     configure.ac   lib         Makefile.in

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/30108475/viewspace-1710835/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/30108475/viewspace-1710835/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值