C++ 代码生成器

来源:互联网 发布:绿色版软件制作工具 编辑:程序博客网 时间:2024/05/19 06:38

这里写图片描述

代码:

https://github.com/pangPython/cpp_code_gen

readme

  • 使用
    先编译:make
    再运行:./ctgen hello.cpp
    可以清除:make clean

  • 注释:作者、时间

  • demo

#include <iostream>/****************************** * author:py * time:2017-06-30 21:48:33 Friday ******************************/using namespace std;int main(){}

main.cpp

#include <iostream>#include <fstream>#include <ctime>#include <unistd.h>/************************** * *通过命令行生成cpp文件模板 * * * **************************/using namespace std;int main(int argc,char *argv[]){    //判断命令行参数个数    if(argc <= 1){        return 1;    }    //获取文件名    char *filename = argv[1];    //创建文件    ofstream out(filename);    //获取当前系统时间    time_t now_time = time(0);    char tmp[64];    strftime(tmp,sizeof(tmp),"%Y-%m-%d %X %A",localtime(&now_time));    //把代码写入文件    out << "#include <iostream>" << endl;    out << "/******************************" << endl;    out << " * author:" << getlogin() << endl;//获取当前*nix系统的当前用户名    out << " * time:"  << tmp << endl;    out << " *" << endl;    out << "*****************************/" << endl;    out << "using namespace std;" << endl;    out << "int main()" << endl;    out << "{" << endl;    out << endl;    out << "}" << endl;    return 0;}

makefile

SRC=./src/all:ctgenctgen:$(SRC)main.cpp    g++ $(SRC)main.cpp -o ctgenclean:    rm ctgen