Linux下的C编译工具链autotools的使用
  0eGysyk4Lrwg 2023年11月02日 63 0

(目录)


一、autotools的安装(Ubuntu环境)

apt install automake autoconf

参考网址:

  • 工具链软件 https://www.gnu.org/software/software.html
  • automake的使用 https://www.gnu.org/software/automake/manual/automake.html

二、使用步骤

image.png

只有configure.ac和Makefile.am这两个文件是需要手动配置的。一顿操作的最终目的是要形成Makefile文件,然后使用make来构建项目。

下图显示了文件及命令之间的生成关系: be65ab13cb1bb7efd1c00cd93190c48d_processing-diagram.png

Autoconf 2.5 以后是使用configure.ac,该版本之前使用configure.in文件名。

第1步:autoscan

在项目目录下执行autoscan命令扫描工作目录,生成configure.scan。 还生成了autoscan.log文件。

第2步:vim configure.ac

mv configure.scan configure.ac,并修改该文件的配置信息。

vim configure.ac

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

AC_PREREQ([2.69])
#AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_INIT([test], [1.0], [bug-automake@gnu.org]) # 给出你的值
AC_CONFIG_SRCDIR([test.c]) # 保持默认值即可
AC_CONFIG_HEADERS([config.h]) # 保持默认值即可
#AM_INIT_AUTOMAKE([-Wall -Werror foreign])
AM_INIT_AUTOMAKE([foreign]) # 不需要NEWS README AUTHORS ChangeLog文件的加入了
AC_CONFIG_FILES([Makefile]) # 指明Makefile的文件位置——项目目录(当前目录)

# 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文件(可以在多个目录下生成Makefile文件)
AC_OUTPUT
# AC_OUTPUT(Makefile doc/Makefile script/Makefile src/Makefile)

AC:AutoConf;AM:AutoMake

第3步:aclocal

在项目目录下执行aclocal命令,扫描configure.ac文件,生成aclocal.m4文件(m4是宏文件)。 还生成了autom4te.cache目录。

第4步:autoconf

在项目目录下执行autoconf命令,引用aclocal.m4文件的宏,将configure.ac文件中的宏展开,生成configure脚本文件。

第5步:autoheader

在项目目录下执行autoheader命令,独立生成config.h.in文件。

第6步:automake

在项目目录下创建Makefile.am文件,供automake工具根据configure.h.in中的参数将Makefile.am转换成Makefile.in文件。

vim Makefile.am

# automake默认读取src目录下的文件,如果要读取其他目录下的源文件,则要使用 foreign 变量
#AUTOMARK_OPTIONS = foreign
bin_PROGRAMS = test_112 # 定义可执行文件名。并形成下边的约定名称:test_112_SOURCES,注意名称的对应组合关系。
test_112_SOURCES= test.c compute.c input.c main.h input.h compute.h # 可以不给头文件.h

再执行

automake --add-missing
# touch NEWS README AUTHORS ChangeLog
# automake

此时将生成configure配置脚本文件。接下来可以执行./configure,以便生成Makefile文件。接下来可以编译安装C项目:

# ./configure --prefix=/opt
./configure
make
sudo make install
make clean
sudo make uninstall

# 打包成test_112-1.0.tar.gz
make dist
make distcheck
tar -tzvf cdf-1.0.tar.gz

完成make之后,*.o及二进制的可执行文件都位于项目目录下。

在执行./configure时如果出现

configure: error: cannot find install-sh, install.sh, or shtool in "." "./.." "./../.."

可执行一下命令:autoreconf -i 其实作用等同于命令:automake --add-missing

三、其他配置形式

  • Autoconf sample1
#				-*- Autoconf -*-
# Process this file with autoconfto produce a configure script.
AC_PREREQ([2.68])
#AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_INIT(./src/calc.c) # 程序的入口——main()
AM_INIT_AUTOMAKE(calc, 1.0)
#AC_CONFIG_SRCDIR([src/common.h])
#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(Makefile)
  • Makefile.am sample1
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=calc
# 多行可以使用\来续行
calc_SOURCES=\
     ./src/calc.c \
     ./src/add.c \
     ./src/sub.c \
     ./src/mul.c \
     ./src/div.c \
     ./src/common.h

clean-generic:
	rm -f ./obj/*.o
	rm -f ./bin/calc
install-am:
	mv *.o ./obj/
	mv calc ./bin/

make之后*.o和二进制的可执行文件都位于项目目录下。再运行make install将执行上边的install-am下边的命令。


  • Autoconf sample2
# Initialize AC: ACINIT( package_name, version, bug_report_address)
AC_INIT([my_program], [0.1])
# Initiglize Automgke
AM_INIT_AUTOMAKE
# AM stands for Automake commands, AC stands for Autoconf commands
# We use libraries
AC_PROG_RANLIB
# Let's check for a C++ compiler
AC_LAN(C++)
# Let's set a C++ compiler
AC_PROG_CXX
# Let's specify where the Makefiles should be produced
# These are the same locations as your Makefile.in's, but named as Makefile only.
# We need to do this because both Autoconf and Automake read this file and
# produce Makefiles from this list.
AC_CONFIG_FILES([Makefile my_inc/Makefile src/Makefile])
# Finally produce "configure" script
AC_OUTPUT
  • Makefile.am sample2
# We don't foliow GNU file setup(no README, no AUTHORS, etc)
AUTOMAKE_OPTIONS = foreign
SUBDIRS = my_inc src

# Our binary is produced here
bin_PROGRAMS = main
main_SOURCES = main.cpp
# main binary will be installed into the default directory for "bin"s (hence we used the bin prefix).

noinst_LIBRARIES = libhelper.a
libhelper_a_SOURCES = helper.cpp
# noinst_ : assures that this library is not installed to the user's computer.
#libhelper.a is a *nix convention. If you noticed, we replaced the . with _ in SOURCES line.

main_LDADD = libhelper.a ../my_inc/libmyadd.a
# LDADD specifies which libraries to compile into "main". We have two libraries.
# libhelper comes from this directory and libmyadd comes from my_inc directory.

# all we have to do is to specify a noinst_ library here.
noinst_LIBRARIES = 1ibmyadd.a
1ibmyadd_a_SOURCES = myadd.cpp

四、automake链接第三方库

只需修改Makefile.am文件即可。修改完要使用automake重新生成configure脚本文件,再通过./configure来重新生成Makefile文件,最后再make编译项目。

# 可以指定生成多个可执行文件,以空格分隔。
bin_PROGRAMS=cdf
cdf_SOURCES=cdf.c
# 指定头文件的位置
AM_CPPFLAGS=-I/usr/local/include
# 配置库的链接
cdf_LDADD=-lgsl -lgslcblas -lm -L/usr/local/lib

# 向gcc传递选项参数
#AM_CFLAGS = --pedantic -Wall -std=c99 -O2 -I/usr/local/include -L/usr/local/lib
#AM_LDFLAGS = -lgsl -lgslcblas -lm

也可以通过./configure生成Makefile的时候指定三个参数:-I(头文件路径),-L(库文件路径),-l(要链接的库名)

./configure --help
./configure CPPFLAGS=-I<include dir> LIBS=-l<library> LDFLAGS=-L<lib dir>

可以通过./configure的CFLAGS变量来去掉-g -O2选项:

# 去掉-g -O2选项
./configure CFLAGS=
# 去掉-g选项
./configure CFLAGS=-O2

五、配置configure向源代码传递参数

源代码

sudo apt install bat batcat example.h

#ifndef __LOG_H__
  #define __LOG_H__

  #ifdef HAVE_SYSLOG
    #define log syslog
  #else
    #define log printf
  #endif

#endif

configure.ac

# Checks for syslog
AC_ARG_WITH(syslog,
  AS_HELP_STRING([--with-syslog], [compile without syslog]),
  syslog=$withval, syslog=no)

if test x$syslog = xyes ; then
  AC_MSG_NOTICE([syslog usage enabled.])
  AC_DEFINE(HAVE_SYSLOG)
else
  AC_MSG_NOTICE([sysLog usage disabled.])
fi
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

0eGysyk4Lrwg