2017-04-14 63 views
0

我寫一個Makefile來編譯我的C++程序,Makefile文件的代碼如下:我的Makefile或C++程序出了什麼問題?

1 TARGET = main 
    2 
    3 CXX = g++ 
    4 CXXFLAGS = -Wall -g 
    5 HEADER = $(wildcard ./*.h) 
    6 SRCS = $(wildcard *.cpp) 
    7 OBJS = $(patsubst %.cpp, %.o, $(SRCS)) 
    8 RM = rm -f 
    9 
10 $(TARGET):$(OBJS) 
11  $(CXX) -o [email protected] $^ 
12 
13 $(OBJS):$(SRCS) 
14  $(CXX) $(CXXFLAGS) $< -o [email protected] 
15 
16 
17 .PHONY:clean 
18  clean: 
19  $(RM) $(OBJS) $(TARGET) 

當前目錄下的文件結構如下:

. 
├── main.cpp 
├── Makefile 
├── MyQueue.cpp 
└── MyQueue.h 
0 directories, 4 files 

我用g++ main.cpp MyQueue.cpp -o queue編譯程序,結果是對的;但我用make編譯程序的結果如下:

g++ -Wall -g main.cpp -o main.o 
/tmp/ccA6CmCE.o: In function `main': 
/home/tzk/DSA/main.cpp:7: undefined reference to `MyQueue::MyQueue(int)' 
collect2: error: ld returned 1 exit status 
make: *** [main.o] Error 1 

我已經定義了MyQueue(int)MyQueue.hMyQueue.cppmain.cpp的代碼如下:

MyQueue.h 
    1 #ifndef MYQUEUE_H_ 
    2 #define MYQUEUE_H_ 
    3 class MyQueue 
    4 { 
    5  public: 
    6   MyQueue(int queueCapacity); 
    7   virtual ~MyQueue(); 
    8   void ClearQueue(); 
    9   bool QueueEmpty() const; 
10   bool QueueFull() const; 
11   int QueueLength() const; 
12   bool EnQueue(int element); 
13   bool DeQueue(int& element); 
14   void QueueTraverse(); 
15 
16  private: 
17   int *m_pQueue; 
18   int m_iQueueLen; 
19   int m_iQueueCapacity; 
20   int m_iHead; 
21   int m_iTail; 
22 }; 
23 
24 #endif 

MyQueue.cpp 
    1 #include "MyQueue.h" 
    2 #include <iostream> 
    3 using namespace std; 
    4 
    5 MyQueue::MyQueue(int queueCapacity) 
    6 { 
    7  m_iQueueCapacity = queueCapacity; 
    8  m_iHead = 0; 
    9  m_iTail = 0; 
10  m_iQueueLen = 0; 
11  m_pQueue = new int[m_iQueueCapacity]; 
12 } 

main.cpp 
    1 #include <iostream> 
    2 #include <cstdlib> 
    3 #include "MyQueue.h" 
    4 
    5 int main(void) 
    6 { 
    7  MyQueue *p = new MyQueue(4); 
    8 
    9  delete p; 
10  p = NULL; 
11 
12  return 0; 
13 } 

所以,這有什麼錯我的程序或Makefile?

============================================== ============

我嘗試添加-c的命令如下:

13 $(OBJS):$(SRCS) 
14  $(CXX) $(CXXFLAGS) -c $< -o [email protected] 

但是,結果還是錯的:

g++ -Wall -g -c main.cpp -o main.o 
g++ -Wall -g -c main.cpp -o MyQueue.o 
g++ -o main main.o MyQueue.o 
MyQueue.o: In function `main': 
/home/tzk/DSA/main.cpp:6: multiple definition of `main' 
main.o:/home/tzk/DSA/main.cpp:6: first defined here 
main.o: In function `main': 
main.cpp:(.text+0x21): undefined reference to `MyQueue::MyQueue(int)' 
MyQueue.o: In function `main': 
main.cpp:(.text+0x21): undefined reference to `MyQueue::MyQueue(int)' 
collect2: error: ld returned 1 exit status 
make: *** [main] Error 1 
+0

請不要標記C++與C標籤的問題。 C是與C++不同的語言。用兩者標記可能會導致錯誤的答案。 – Toby

回答

4

當你創建目標文件,你需要-c選項。您沒有它,這意味着g++試圖創建和鏈接可執行文件main.o


你的目標建立目標文件也是錯誤的。它應該是例如

%.o: %.cpp 
    $(CXX) $(CXXFLAGS) -c $< -o [email protected] 
+0

謝謝您的回覆,我嘗試添加'-c',但結果仍然錯誤。像這樣'main.cpp :(。text + 0x21):未定義的引用'MyQueue :: MyQueue(int)''。 – tzkone

+0

@tzkone製作目標文件的目標也是錯誤的。 –

+0

哇!你是對的,我再試一次,結果是對的。但是,命令'$(OBJS):$(SRCS)'有什麼問題? – tzkone

2

您必須使用-c標誌來轉換對象文件中的源文件。

$(OBJS):$(SRCS) 
    $(CXX) $(CXXFLAGS) -c $< -o [email protected] 
+0

謝謝您的回覆!我嘗試你的解決方案,但結果仍然是錯誤的。像這樣「main.cpp :(。text + 0x21):未定義的引用'MyQueue :: MyQueue(int)'」。 – tzkone