所以我想爲我的程序做一個非常簡單的makefile。該程序有6個繼承類,我已經爲他們每個人創建了單獨的.h和.cpp文件。C++ Makefile,重新定義類
我遇到的問題是,當我運行makefile我得到一個錯誤,告訴我:
In file included from Items.h:2:0,
from GraphicsCards.h:3,
from GraphicsCards.cpp:1:
Orders.h:5:7: error: redefinition of ‘class Orders’
Orders.h:5:7: error: previous definition of ‘class Orders’
make: *** [GraphicsCards.o] Error 1
下面是我的Makefile:
# all - compile the program if any source files have changed
all: Orders.o Customers.o Items.o GraphicsCards.o Proccessors.o HardDrives.o driver.o
g++ Orders.o Customers.o Items.o GraphicsCards.o Proccessors.o HardDrives.o driver.o -o program
# Orders.o
Orders.o: Orders.cpp Orders.h
g++ -c Orders.cpp -o Orders.o
# Customers.o
Customers.o: Customers.cpp Customers.h
g++ -c Customers.cpp -o Customers.o
# Items.o
Items.o: Items.cpp Items.h
g++ -c Items.cpp -o Items.o
# GraphicsCards.o
GraphicsCards.o: GraphicsCards.cpp GraphicsCards.h
g++ -c GraphicsCards.cpp -o GraphicsCards.o
# Proccessors.o
Proccessors.o: Proccessors.cpp Proccessors.h
g++ -c Proccessors.cpp -o Proccessors.o
# HardDrives.o
HardDrives.o: HardDrives.cpp HardDrives.h
g++ -c HardDrives.cpp -o HardDrives.o
# driver.o
driver.o: driver.cpp Orders.h Customers.h Items.h GraphicsCards.h Proccessors.h HardDrives.h
g++ -c driver.cpp -o driver.o
# clean - delete the compiled version of your program and
# any object files or other temporary files created during compilation.
clean:
rm -f *.o program
舉個例子這是我的GraphicsCards .h文件看起來像:
#include <string>
#include "Orders.h"
#include "Items.h"
using namespace std;
class GraphicsCards: protected Items
{
private:
int speed;
string model;
int memory;
};
此外,我所有的x.cpp文件都有#include「xh」作爲它們的第一行
我對C++編碼相當陌生,所以即使你不能幫助我解決我的問題,任何提示或建議都將不勝感激!
您是否正確使用[include guard](http://stackoverflow.com/questions/21090041/why-include-guards?s=2|4.9890)? –