linux下Makefile自动生成工具-autotools的使用

xingyun86 2019-6-17 1765

每个学习Linux的人都知道Makefile,这是一个很有用的东西,但是编写它是比较复杂,今天介绍一个它的自动生成工具,autotools的使用。很多GNULinux的的软件都是用它生成Makefile的,包括我们非常熟悉的Linux内核源代码。

 1、准备:

  需要工具

  autoscan

  aclocal

  autoheader 

  automake

  autoconf

  auto make 

  在终端敲入命令,哪个没有安装哪个,一般是第一个autoscan没有。

2、测试程序编写:
     建立目录:mkdir common/http common/json common/reids common/tcp common/rmq module

     编写程序:./main.cpp common/common.h common/http/http.h common/http.cpp common/json/json.h common/json/json.cpp common/redis/redis.h common/redis/redis.cpp common/rmq/rmq.h common/rmq.cpp

3、生成configure.ac

    configure.ac是automake的输入文件,所以必须先生成该文件。

    执行命令:

[root@localhost test_server]# ls
main.cpp common module verndor

修改 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_CONFIG_SRCDIR([include/str.h])
AC_CONFIG_HEADER([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

修改

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

AC_INIT(test_server,0.0.1, [bug@test.org])

其中:FULL-PACKAGE-NAME 为程序名称,VERSION为当前版本, BUG-REPORT-ADDRESS为bug报告地址

然后添加两句话:

    AM_INIT_AUTOMAKE

    AC_CONFIG_FILES([Makefile])

结果如下:(两句话不是在一起的

 -*- 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_server, 0.0.1, [bug@test.org])
AM_INIT_AUTOMAKE
AC_CONFIG_SRCDIR([include/str.h])
AC_CONFIG_HEADER([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

4、执行aclocal

[root@localhost test_server]# aclocal

5、制作Makefile.am

[root@localhost test_server]# vi Makefile.am
#Makefile.am
bin_PROGRAMS    = test_server
test_server_SOURCES     = ./main.cpp common/common.h common/http/http.h common/http.cpp common/json/json.h common/json/json.cpp common/redis/redis.h common/redis/redis.cpp common/rmq/rmq.h common/rmq.cpp
test_server_CPPFLAGS    = -I ./ -I common/http -I common/json -I common/reids -I common/tcp -I common/rmq module

automake 这个命令需要用到这个配置文件。各个选项意思比较直观

6、autoheader

[root@localhost test_server]# autoheader

7、automake必须文件:

 * install-sh
 * missing
 * INSTALL
 * NEWS
 * README
 * AUTHORS
 * ChangeLog
 * COPYING
 * depcomp

其中,以下文件在执行automake -a的时候会自动生成

* install-sh
* missing
* INSTALL
* COPYING
* depcomp

接下来手动生成剩下的文件

[root@localhost test_server]# touch NEWS README AUTHORS ChangeLog

8、执行automake -a

[root@localhost test_server]# automake -a
configure.ac: installing `./install-sh'
configure.ac: installing `./missing'
Makefile.am: installing `./INSTALL'
Makefile.am: installing `./COPYING'
Makefile.am: installing `./compile'
Makefile.am: installing `./depcomp'

9、autoconf

[root@localhost test_server]# autoconf
[root@localhost test_server]# ls
aclocal.m4      autoscan.log  config.h.in   configure.scan  include     Makefile.am  NEWS
AUTHORS         ChangeLog     configure     COPYING         INSTALL     Makefile.in  README
autom4te.cache  compile       configure.ac  depcomp         install-sh  missing      src
main.cpp	verndor	      module	    module

10、执行测试

[root@localhost test_server]#./configure --prefix=$PWD/output CXXFLAGS=-std=c++11
[root@localhost test_server]#make LIBS='-lpthread -lrabbitmq -lhiredis'
[root@localhost test_server]#make install
[root@localhost test_server]#out/bin/minik_server 
78 56 34 12
little endian
ok

完成

×
打赏作者
最新回复 (0)
只看楼主
全部楼主
返回