代码:

https://github/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:ctgen
ctgen:$(SRC)main.cpp
    g++ $(SRC)main.cpp -o ctgen
clean:
    rm ctgen

更多推荐

C++ 代码生成器