Makefile生成器,使用C++和Boost实现
今天学习了一下Boost的文件遍历功能,同一时候发现GNU编译器有-MM选项。能够自己主动生成依赖关系,于是利用以上两点写了一个Makefile生成器。
能够生成一般的单个可运行文件的Makefile。使用的是Windows+Mingw+boost环境。假设使用Linux,仅仅需在程序中的两个System系统调用处和clean标签生成处将del 改成rm相关操作就好了。
如今更改成了Linux版本号。下载的版本号是Windows的。
以下是源码:
makemake.cpp:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <string>
#include <exception>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/program_options.hpp> using namespace std;
namespace po = boost::program_options;
using namespace boost::filesystem;
using namespace boost; void getFiles(vector<string>& src); const string head = string(
"######################################################################\n")+
"# This makefile is generated by makemake. #\n"+
"# By Eric Brown. #\n"+
"# 2014/10/27 #\n"+
"######################################################################\n"; int main(int argc, char* argv[])
{
vector<string> src;
string compiler = "g++";
string target = "a";
vector<string> objs;
bool debug = false; try
{
po::options_description desc("---Help---");
desc.add_options()
("help,h", "print this message.")
("gcc,c", "use gcc compiler. Program uses g++ default.")
("debug,g", "use debug option(-g) in makefile.")
("out,o", po::value<string>(), "the target file name.");
po::positional_options_description p;
p.add("out", -1);
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(desc).
positional(p).run(), vm);
po::notify(vm); if (vm.count("help"))
{
cout << desc << endl;
return 0;
}
if (vm.count("gcc"))
compiler = "gcc";
if (vm.count("out"))
target = vm["out"].as<string>();
if (vm.count("debug"))
debug = true;
} catch(std::exception& e) {
cout << e.what() << endl;
return 1;
} getFiles(src); ofstream make;
make.open("Makefile", ios_base::out); make << head << endl;
make << "CC = " << compiler << endl;
make << "Flags = " << endl;
make << "LIBS = ";
if (debug)
make << "-g"; make << endl; make << "src = ";
for (int i = 0; i < src.size(); ++i)
{
make << src[i] << ' ';
std::system(string(compiler + " -MM \"" + src[i] + "\" >> .temp~").c_str());
} make << "\nObjs = ";
for (int i = 0; i < src.size(); ++i)
{
if (ends_with(src[i], ".cpp"))
objs.push_back(replace_last_copy(src[i], ".cpp", ".o"));
if (ends_with(src[i], ".c"))
objs.push_back(replace_last_copy(src[i], ".c", ".o"));
make << objs[i] << ' ';
}
make << endl; make << '\n' << target << ": $(Objs)" << endl;
make << "\t$(CC) $(Flags) $(Objs) -o " << target << " ${LIBS}" << endl;
make << "$(Objs): $<" << endl;
make << "\t$(CC) $(Flags) $< -c\n" << endl; ifstream in(".temp~");
string line;
while(getline(in, line))
make << line << endl;
make << endl; make << "clean:" << endl;
make << "\trm -f ${Objs}" << endl;
make << "cleanbak:" << endl;
make << "\trm -f *~" << endl;
in.close(); std::system("rm -f .temp~"); make.close(); return 0;
} void getFiles(vector<string>& src)
{
path p = current_path();
directory_iterator beg(p);
directory_iterator end;
for (; beg != end; beg++)
{
string name = beg->path().filename().string();
if (ends_with(name, ".cpp") ||
ends_with(name, ".c"))
src.push_back(name);
}
}
可运行程序能够在这里下载:http://download.csdn.net/detail/pdcxs007/8090981。
Makefile生成器,使用C++和Boost实现的更多相关文章
- boost 随机数发生器
Random 随机数 在很多应用中都需要使用随机数.本库力求提供一个高效的,通用的随机数库.boost库有多种随机数生成方式.先熟悉一下各种随机数生成器的概念. 数字生成器(Number Ge ...
- Android学习——windows下搭建NDK_r9环境
1. NDK(Native Development Kit) 1.1 NDK简介 Android NDK是一套允许开发人员使用本地代码(如C/C++)进行Android APP功能开发的工具,通过这个 ...
- NDK开发
1 CDT 是 Eclipse 插件,它将把 Eclipse 转换为功能强大的 C/C++ IDE. C/C++在Eclipse平台下的开发工具.它提供的功能包括:C/C++编辑器(一些基本的功能:语 ...
- Java开源项目(备查)
转自:http://www.blogjava.net/Carter0618/archive/2008/08/11/221222.html Spring Framework [Java开源 J2EE框 ...
- qmake和moc的功能(★firecat推荐★)
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://devbean.blog.51cto.com/448512/355100 前面我们 ...
- Qt核心剖析: moc
前面我们说过,Qt 不是使用的“标准的” C++ 语言,而是对其进行了一定程度的“扩展”.这里我们从Qt新增加的关键字就可以看出来:signals.slots 或者 emit.所以有人会觉得 Qt 的 ...
- Android下NDK开发环境搭建
Android下NDK开发环境搭建 1. AndroidNDK安装与配置 1.1 NDK简介 Android NDK是一套允许开发人员使用本地代码(如C/C++)进行Android APP部 ...
- Caffe源码中common文件分析
Caffe源码(caffe version:09868ac , date: 2015.08.15)中的一些重要头文件如caffe.hpp.blob.hpp等或者外部调用Caffe库使用时,一般都会in ...
- QObject 源代码阅读
我们进入 qt/src 文件夹.你可能对这里的目录名时曾相识,因为几乎这里的所有文件夹名都对应着 Qt 的模块的名字:gui,network,multimedia等等.我们从最核心的 QtCore 开 ...
随机推荐
- Fiddler2 中文手册
原文:http://blog.sina.com.cn/s/blog_66a13b8f0100vgfi.html 最近一阵研究 Fiddler2 的使用来着,一开始看起来有点找不着北,索性就根据官网资料 ...
- Cocos2d-x3.2 TextureCache类异步载入功能解说
本文TextureCache类异步载入功能的代码抽出,总共代码就200多行.感兴趣能够看看. 研究这个主要是由于项目中须要异步插入数据,但之前的方法在Android上总是崩溃所以想到TextureCa ...
- jQuery EasyUI 数据网格 - 条件设置行背景颜色
$('#tt').datagrid({ rowStyler:function(index,row){ if (row.listprice>50){ return 'color:blue;font ...
- [转]MySQL Explain
Mysql Explain 详解 一.语法 explain < table_name > 例如: explain select * from t3 where id=3952602; 二. ...
- ChartView与LineSeries搭配实现曲线局部缩放功能
效果图: 上一篇文章实现的时候还不知道有QtChart这个模块......好好看了下资料就想做个例子实现一下这功能,比较了下代码量...恩,直接看代码: Rectangle { id: view_re ...
- SVG 可伸缩矢量图形 简介 Path路径
w3school:http://www.w3school.com.cn/svg/svg_intro.asp SVG 意为可缩放矢量图形(Scalable Vector Graphics). SVG ...
- 学习笔记4-Action参数绑定
参数绑定功能默认是开启的,其原理是把URL中的参数(不包括模块.控制器和操作名)和操作方法中的参数进行绑定. 要启用参数绑定功能,首先确保你开启了URL_PARAMS_BIND设置: 'URL_PAR ...
- ssh 的安装
新安装的ubuntu 虚拟机,没有ssh时(ssh 连接不上),时ssh服务没装. 安装openssh-server,就可以. ------------------------------------ ...
- C# 使用int.TryParse,Convert.ToInt32,(int)将浮点类型转换整数时的区别
int.TryParse,Convert.ToInt32,(int) 这几种类型在将浮点类型转换整数时是有差别 Convert.ToInt32则会进行四舍五入 int.TryParse只能转换整数,即 ...
- Android选择/拍照 剪裁 base64/16进制/byte上传图片+PHP接收图片
转载请注明出处:http://blog.csdn.net/iwanghang/article/details/65633129认为博文实用,请点赞,请评论,请关注.谢谢! ~ 老规矩,先上GIF动态图 ...