导航

g++多文件编译

Posted on 2015-01-04 15:05  ggzone  阅读(607)  评论(0编辑  收藏  举报

头文件: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)