Today I learned about the Boost file traversal feature, and at the same time found the GNU compiler has the-MM option. Being able to generate dependencies on its own initiative, I wrote a makefile generator using the above two points.
The ability to generate a generic makefile of a single executable file. The Windows+mingw+boost environment is used. Assuming that Linux is used, it is only necessary to change Del to RM related operations at the two system calls in the program and the clean tag generation.
Now changes to the Linux version number. The version number of the download is Windows.
The following is the source code:
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::p rogram_options; using namespace Boost::filesystem; using namespace boost; void getFiles (vector<string>& src); const string head = String ( "######################################################################\n") + "# This makefile are generated by makemake. & nbsp; #\n "+ " # by Eric brown. #\n "+ " # 2014/10/27 #\n "+ " ################################ ######################################\n "; int Main (int argc, char* argv[]) { vector<string> src; & nbsp;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.") & nbsp; ("Out,o", po::value<string> (), "the target file name."); po::p ositional_options_description p; p.add ("Out",-1); po::variables_map VMs; po::store (Po::command_line_parser (argc, argv). Options (DESC). &NBSP;&NBSP;&NBSp 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) & Nbsp;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 (); nbsp; 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); } } A running program can be downloaded here: http://download.csdn.net/detail/pdcxs007/8090981.
Makefile generator, implemented using C + + and boost