g++多檔案編譯
阿新 • • 發佈:2019-02-17
標頭檔案:A.h
void test();
原始檔:A.cpp
#include <iostream> #include<thread> #include<chrono> #include<clocale> #include "boost/date_time/gregorian/gregorian.hpp" #include "boost/date_time/posix_time/posix_time.hpp" using namespace std; using namespace boost; using namespace boost::gregorian; using namespace boost::posix_time; void test() { date d = day_clock::local_day(); date_facet* dfacet = new date_facet("%Y年%m月%d日"); cout.imbue(locale(cout.getloc(), dfacet)); cout << d << endl; ptime tp = microsec_clock::local_time(); time_facet* tfacet = new time_facet("%Y年%m月%d日%H點%M分%S%F秒"); cout.imbue(locale(cout.getloc(), tfacet)); cout << tp << endl; }
main函式所在檔案:test.cpp
#include "A.h"
int main()
{
test();
return 0;
}
手動編譯:
一步編譯:
g++ -o test.o A.cpp test.cpp -std=c++11
分開編譯:
g++ -c A.cpp -std=c++11
g++ -c test.cpp
g++ A.o test.o -o test.out
執行:./test.out
採用makefile編譯:
test:A.o test.o g++ -o test A.o test.o test.o:A.h test.cpp g++ -c test.cpp A.o:A.cpp g++ -c A.cpp -std=c++11 clean: rm test A.o test.o
引入變數:
objects=A.o test.o
test:$(objects)
g++ -o test $(objects)
test.o:A.h test.cpp
g++ -c test.cpp
A.o:A.cpp
g++ -c A.cpp -std=c++11
clean:
rm test $(objects)
自動推導:
objects=A.o test.o test:$(objects) g++ -o test $(objects) test.o:A.h //省略編譯命令和test.cpp A.o:A.cpp g++ -c A.cpp -std=c++11 //因為要使用c++11編譯,所以顯示給出,這兩句。否則,和上一行可以省略 clean: rm test $(objects)
1.附加庫在連結階段
2.不屬於專案的標頭檔案,如果沒在預設尋找目錄中,使用 -L指定,在依賴中不用寫
3.使用mysql的庫要apt-get install libmysql++-dev
objects=main.o parsexml.o tinyxml2.o opfile.o opmysql.o stringex.o pathx.o timex.o
logtoo_srvd_bin=../build/bin
logtoo_srvd:$(objects)
g++ -o $(logtoo_srvd_bin)/logtoo_srvd $(objects) -L /usr/local/lib -lmysqlclient
rm $(objects)
main.o:parsexml.h opmysql.h opfile.h stringex.h timex.h pathx.h main.cpp
g++ -c main.cpp -I /usr/local/include -std=c++11
parsexml.o:tinyxml2.h parsexml.cpp
g++ -c parsexml.cpp -std=c++11
tinyxml2.o:
opfile.o:stringex.h timex.h opfile.cpp
g++ -c opfile.cpp -std=c++11
opmysql.o:opmysql.cpp
g++ -c opmysql.cpp -I /usr/local/include -std=c++11
stringex.o:stringex.cpp
g++ -c stringex.cpp -std=c++11
pathx.o:pathx.cpp
g++ -c pathx.cpp -std=c++11
timex.o:
clean:
rm $(logtoo_srvd_bin)/logtoo_srvd