川山甲

追求内心的非常平静!瞬间清空所有的杂念,达到物我两忘!

  博客园  :: 首页  ::  :: 联系 :: 订阅 订阅  :: 管理
 
 
   
  在linux下C或C++项目开发,Makefile是必备的力气,但是发现手写很麻烦。
  在百度有个comake2工具,用于自动生成Makefile工具,而在外边本想找一个同类工具,但发现很难做到,只发现有个类似的智能生成工具autotools,但是操作比较麻烦,奔着“一人学习,大家共享”的原则,手动写了一个工具类,帮助自己和大家生成现成的c或者cpp框架。
  代码比较简单,希望我们能一起改善下。
  
  
 
  我也希望大家提出更多好的思路(比如名字、功能),我们一起改进下这个工具,烦请大家留下评论,我收集下。
 
 
 
 
 
使用方式
  
部署:
[jack@localhost ~]$ wget https://github.com/chuanshanjia/ccpp/blob/master/frame.sh
[jack@localhost ccpp]$ ls 
frame.sh
[jack@localhost ccpp]$ pwd
/home/jack/tool/ccpp
[jack@localhost ccpp]$ sh frame.sh autotools
configure.ac:6: installing `./install-sh'
configure.ac:6: installing `./missing'
...
[jack@localhost ccpp]$ ls
aclocal.m4  autom4te.cache  ChangeLog  config.h.in  config.status  configure.ac  depcomp   include  install-sh  Makefile.am  missing  README  stamp-h1
AUTHORS     autoscan.log    config.h   config.log   configure      COPYING       frame.sh  INSTALL  Makefile    Makefile.in  NEWS     src

  

 清理:
[jack@localhost ccpp]$ sh frame.sh clear
[jack@localhost ccpp]$ ls
frame.sh
[jack@localhost ccpp]$ 

 

愉快的操作吧^_^

头文件位置:

[jack@localhost ccpp]$ ls
aclocal.m4  autom4te.cache  ChangeLog  config.h.in  config.status  configure.ac  depcomp   include  install-sh  Makefile.am  missing  README  stamp-h1
AUTHORS     autoscan.log    config.h   config.log   configure      COPYING       frame.sh  INSTALL  Makefile    Makefile.in  NEWS     src
[jack@localhost ccpp]$ vim include/demo.h 

main文件位置: 

[jack@localhost ccpp]$ vim src/demo.cpp

编译运行:

[jack@localhost ccpp]$ make
make  all-am
make[1]: Entering directory `/home/jack/tool/ccpp'
g++ -DHAVE_CONFIG_H -I. -I/home/jack/tool/ccpp/include -I/home/jack/tool/ccpp/src/include     -g -O2 -MT demo.o -MD -MP -MF .deps/demo.Tpo -c -o demo.o `test -f 'src/demo.cpp' || echo './'`src/demo.cpp
mv -f .deps/demo.Tpo .deps/demo.Po
g++  -g -O2   -o demo demo.o  
make[1]: Leaving directory `/home/jack/tool/ccpp'
[jack@localhost ccpp]$ ./demo 
hello,demo
[jack@localhost ccpp]$ 

  

  

  

以下内容可作为参考学习,帮助理解工具产生的过程,如非必要,可不进行阅读。网上有很多类似内容,可以在百度搜索“autotools"就能看到整个过程。  

 
 
工具准备(参考)
  
autoscan
aclocal
autoheader 
automake
autoconf
automake

 

 

 

 

准备Makefile.am文件

INCLUDES=-I./include -I./src/include
UTOMAKE_OPTIONS=foreign
bin_PROGRAMS=test
test_SOURCES=src/test.cpp 

  

 

  

使用智能化工具(参考)
  

autoscan

 当我们利用autoscan工具生成confiugre.scan文件时,我们需要将confiugre.scan重命名为confiugre.ac文件。confiugre.in调用一系列autoconf宏来测试程序需要的或用到的特性是否存在,以及这些特性的功能。

具体使用如下:

 

[jack@localhost tool]$ autoscan 
[jack@localhost tool]$ ls
autoscan.log  configure.scan  include  src
[jack@localhost tool]$ mv configure.scan configure.ac
[jack@localhost tool]$ ls
autoscan.log  configure.in  include  src [jack@localhost tool]$ vim configure.ac

  

修改点:

 

 

一气呵成

aclocal
autoconf
autoheader
automake --add-missing
automake --add-missing
touch NEWS README  AUTHORS  ChangeLog
automake --add-missing
./configure

  

 

 

 

 
参考手册
   
confiugre.in文件的一般布局:
 
AC_INIT
 测试程序
 测试函数库
 测试头文件
 测试类型定义
 测试结构
 测试编译器特性
 测试库函数
 测试系统调用
AC_OUTPUT

  

 
表 1Makefile.am一般格式
表 1Makefile.am一般格式
 
表 2 Makefile.am中可用的全局变量
表 2 Makefile.am中可用的全局变量

在Makefile.am中尽量使用相对路径,系统预定义了两个基本路径:

表 3Makefile.am中可用的路径变量
表 3Makefile.am中可用的路径变量
 
 
参考文献
   
1、http://www.ibm.com/developerworks/cn/linux/l-makefile/
2、https://www.gnu.org/software/autoconf/manual/autoconf-2.61/html_node/Autoconf-Language.html#Autoconf-Language
3、http://www.gnu.org/software/automake/manual/automake.pdf
 

 

推荐
 

 

 

 

posted on 2016-03-09 15:16  川山甲  阅读(11590)  评论(5编辑  收藏  举报