Today I learned about the Boost file traversal feature, and found that the GNU compiler has the-MM option to automatically generate dependencies, so I wrote a makefile generator with the above two points. You can generate makefile for a generic single executable file, using the Windows+mingw+boost environment. If you're using Linux, you just need to change Del to RM related operations at the two system calls in the program and the Clean Tag generator.
Here 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 was 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---");d esc.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::p ositional_options_description P;p.add ("Out",-1);p O::variables_map vm;po::store (Po::command_line_parser (argc , argv). Options (DESC). Positional (P). Run (), VM);p o::notify (VM), if (Vm.count ("Help")) {cout << desc << Endl; return 0;} if (Vm.count ("gcc")) compiler = "GCC", if (Vm.count ("Out")) The 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 =" & lt;< compiler << Endl;make << "Flags ="; 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 << '. EXE: $ (OBJS) ' << endl;make << ' \t$ (CC) $ (Flag s) $ (OBJS)-O "<< target <<". exe "<< endl;make <<" $ (OBJS): $< "<< endl;make <<" \t$ (CC) $ (Flags) $<-c\n "<< Endl;ifstream in (". temp~ "); string Line;while (Getline (in), line) make << line & lt;< endl;make << endl;make << "clean:" << endl;make << "\tdel *.o" << endl;make << "Cleanbak:" << endl;make << "\tdel *~" << endl;in.close (); Std::system ("Del. TEmp~ "); Make.close (); return 0;} void GetFiles (vector<string>& src) {path p = current_path ();d Irectory_iterator Beg (p);d Irectory_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);}}
The executable program can be downloaded here: http://download.csdn.net/detail/pdcxs007/8090981.
Makefile generator, implemented using C + + and boost