0
点赞
收藏
分享

微信扫一扫

Linux下的C编译工具链autotools的使用

(目录)

一、autotools的安装(Ubuntu环境)

apt install automake autoconf

参考网址:

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

二、使用步骤

第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

AC_OUTPUT

第3步:aclocal

在项目目录下执行aclocal命令,扫描configure.ac文件,生成aclocal.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

#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
#				-*- 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
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下边的命令。

四、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

也可以通过./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
举报

相关推荐

0 条评论