zoukankan      html  css  js  c++  java
  • automake---让Makefile变得更专业一点儿

    一般我们装软件时,都要运行

    ./configure  --prefix=/usr/local
    make
    make install

    看着不断刷新的屏幕,总感觉真得好高深呀,其实我们的程序也可以这样子。

    下面,我将自己以前编写的一个程序改造成很专业的样子。

    下面的Makefile是我自己写的:

    objects=allotter/array.o config/config.o rbtree/rbtree.o thread_pool/thread_pool.o workers/workers.o hash_map/hash_map.o tool/tool.o
    CC=gcc -g -lstdc++
    
    server/server : $(objects) server/server.c
            $(CC) -o server/server -lpthread -levent $(objects) server/server.c 
    
    allotter/array.o : allotter/array.h allotter/array.c
            $(CC) -c -o allotter/array.o allotter/array.c
    
    config/config.o : config/config_file.h config/config_file.c
            $(CC) -c -o config/config.o config/config_file.c
    
    rbtree/rbtree.o : rbtree/rbtree.h rbtree/rbtree.c
            $(CC) -c -o rbtree/rbtree.o rbtree/rbtree.c
    
    thread_pool/thread_pool.o : thread_pool/thread_pool.h thread_pool/thread_pool.c
            $(CC) -c -o thread_pool/thread_pool.o thread_pool/thread_pool.c
    
    workers/workers.o : workers/workers.h workers/workers.c
            $(CC) -c -o workers/workers.o workers/workers.c
    
    hash_map/hash_map.o : hash_map/hash_map.h hash_map/hash_map.c
            $(CC) -c -o hash_map/hash_map.o hash_map/hash_map.c
    
    tool/tool.o : tool/tool.h tool/tool.c
            $(CC) -c -o tool/tool.o tool/tool.c
    
    clean:
            -rm -f server/server $(objects)

    总感觉不够专业,于是就研究了专业的Makefile的写法。

    首先对程序进行一下简介:

    1,程序用C语言写成,用到了C++的unordered_map,所以为了方便编译,统一改成.cc文件,用g++编译

    2,所有程序文件有十几个,如下, 所有的程序放在一直,没有子目录,程序依赖pthread和libevent两个库。

    array.h array.cc
    rbtree.h rbtree.cc
    workers.h workers.cc
    tool.h tool.cc
    hash_map.h hash_map.cc
    config_file.h config_file.cc
    thread_pool.h thread_pool.cc
    
    node.h
    server.cc

    开始步入正题,一步一步生成Makefile

    1,在目录中运行autoscan,会生成configure.scan文件,改名为configure.in,打开编辑,如下,红色的部分是我改的,或我添加的

    检查libevent和pthread库的两句是自动生成的,如果没有生成,我们也可以自己添加

    #                                               -*- Autoconf -*-
    # Process this file with autoconf to produce a configure script.
    
    AC_PREREQ(2.62)
    #对我们的程序起一个名字(不是最后可执行文件的名字),表示版本号为1.o,名字叫server1
    AC_INIT(server1,
    1.0, BUG-REPORT-ADDRESS) AC_CONFIG_SRCDIR([hash_map.cc]) AC_CONFIG_HEADERS([config.h])
    #表示我们要生成一个makefile文件
    AM_INIT_AUTOMAKE(server1,
    1.0) # Checks for programs. AC_PROG_CXX AC_PROG_CC # Checks for libraries. # FIXME: Replace `main' with a function in `-levent': #检查libevent库
    AC_CHECK_LIB([event], [main]) # FIXME: Replace `main
    ' with a function in `-lpthread': #检查pthread库


    AC_CHECK_LIB([pthread], [main]) # Checks
    for header files. AC_CHECK_HEADERS([arpa/inet.h fcntl.h netinet/in.h stdint.h stdlib.h string.h sys/socket.h syslog.h unistd.h]) # Checks for typedefs, structures, and compiler characteristics. AC_TYPE_SIZE_T # Checks for library functions. AC_FUNC_MALLOC AC_CHECK_FUNCS([bzero ftruncate gettimeofday memset socket strcasecmp strchr strerror strrchr]) AC_CONFIG_FILES([Makefile]) AC_OUTPUT

    2,运行aclocal,生成一个aclocal.m4文件,我们不用去管它

    上一步生成的文件中有很多宏,所以需要解释这些宏,于是aclocal就生成这些宏的解释程序,供下面使用.

    3,运行autoheader生成一个头文件

    4, 运行autoconf生成configure

    5,建一个名字叫Makefile.am的文件,内容如下: 

    AUTOMAKE_OPTIONS=foreign
    bin_PROGRAMS=server1
    server1_SOURCES=server.cc array.h array.cc config_file.h config_file.cc rbtree.h rbtree.cc thread_pool.h thread_pool.cc workers.h workers.cc hash_map.h hash_map.cc tool.h tool.cc
    server1_LDFLAGS= -lpthread -levent

    第二行表示生成的可执行程序的名称

    第三行列出整个项目所有的源文件,automake会根据我们的源文件分别进行编译,链接

    第四行加一些链接标记

    6,运行automake,如果有问题,根据提示运行automake --add-missing

    7,其实到现在已经完成了,看是不是生成了个Makefile.in文件,有了这个文件,可以通过运行./configure 来生成 Makefile文件了

    8,运行make,是不是屏幕就开始刷新了,然后我们可以运行make dist来打包一个部署压缩包,就可以发布了。

    总结:

    整个过程需要我们介入的就是第一步和第五步,第一步只是细小的改动,第五步需要我们整个编写Makefile.am文件。

    下面的文章介绍的比较好,推荐一下:

    http://www.ibm.com/developerworks/cn/linux/l-makefile/

    以上过程都亲自操作过,生成程序可以正常运行,代码可下载:

    github下载:https://github.com/hxdoit/real_time_rank/tree/master/server1-1.0

    百度网盘下载:http://pan.baidu.com/s/1pFoEB



     

  • 相关阅读:
    [原]将Oracle 中的blob导出到文件中
    [原]unique index和non unique index的区别
    lsattr/chattr
    [原]说不清楚是Oracle的Bug还是TSM的Bug
    [摘]Oracle限制某个数据库帐号只能在特定机器上连入数据库
    [原]复制Oracle Home所需要注意的几个问题
    [原]Oracle Data Guard 折腾记(二)
    配置vsFTP的虚拟用户认证
    [原]给Oracle 11g Interval分区进行重命名
    [摘]如何抓住蝴蝶效应中的那只蝴蝶
  • 原文地址:https://www.cnblogs.com/hxdoit/p/3434688.html
Copyright © 2011-2022 走看看