2017-04-14 8 views
0

私はC++プログラムをコンパイルするためのMakefileを書き、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) 

カレントディレクトリのファイル構造は以下の通りです:

私はコンパイルする g++ main.cpp MyQueue.cpp -o queueを使用
. 
├── main.cpp 
├── Makefile 
├── MyQueue.cpp 
└── MyQueue.h 
0 directories, 4 files 

プログラムは、結果は正しいです。次のようにしている私がMyQueue(int)定義した

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.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 } 

だから、何が間違っているの!しかし、私は次のような結果があるプログラムをコンパイルするmakeを使用します私のプログラムか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オプションが必要なオブジェクトファイルを作成します。あなたは、実行可能ファイルmain.oを作成し、リンクするg++試行を意味し、それを持っていません。


オブジェクトファイルをビルドする目標も間違っています。例えば、

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

は、私は '-c'を追加しようとするが、結果はまだ間違っている、ご返信いただきありがとうございます。この 'main.cpp :(。text + 0x21)のように:' MyQueue :: MyQueue(int) 'への未定義参照。 – tzkone

+0

@tzkoneオブジェクトファイルを作成するあなたの目標も間違っています。 –

+0

WOW!あなたは正しいです、私はもう一度やり直してください、結果は正しいです。しかし、 '$(OBJS):$(SRCS)'コマンドの何が間違っていますか? – tzkone

2

オブジェクトファイルのソースファイルを変換するには、-cフラグを使用する必要があります。

などが

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

あなたの返信ありがとう!私はあなたの解決策を試しますが、結果はまだ間違っています。このように "main.cpp :(。text + 0x21):未定義の参照は' MyQueue :: MyQueue(int) '"になります。 – tzkone

関連する問題