2011-12-06 130 views
0

我已經在NetBeans上編寫了C++程序,現在我想在命令行上的Linux上運行它。我怎麼寫Makefile,寫它的邏輯是什麼?編寫C++程序的Makefile

我有3個.cpp文件和2個.hpp文件。

這就是我試圖做:

# Makefile 

# the C++ compiler 
CXX  = g++ 
CC  = $(CXX) 

# options to pass to the compiler 
CXXFLAGS = -Wall -ansi -O2 -g 
Run: Run.cpp Assessment3.o Student.o 
$(CXX) $(CXXFLAGS) Run.cpp Assessment3.o Student.o -o Run 

Run: Run.cpp Assessment3.hpp Assessment3.o Student.o 
$(CXX) $(CXXFLAGS) Run.cpp Assessment3.o Student.o -o Run 

Assessment3.o: Assessment3.cpp Assessment3.hpp 
$(CXX) $(CXXFLAGS) -c Assessment3.cpp 

Student.o: Student.cpp Assessment3.o 
    $(CXX) $(CXXFLAGS) -c Student.cpp 

它給了我「失蹤分隔符。停止。'命令行錯誤。這並不是說這是一個錯誤。 乾杯

+0

您需要縮進'$(CXX)...'行 - 最好使用製表符,儘管make的大多數現代版本都可以使用空格或製表符(舊版本需要製表符)。但請參閱下面的答案以獲取更簡單的版本。 –

+0

對「make missing separator」進行網絡搜索將導致大量網頁描述此問題及其解決方案。在這裏問這個問題真的不是什麼工作,而不是搜索? – eriktous

回答

3

假設你想建立一個名爲foo可執行文件,你可以使用一個簡單的makefile它建立的一切一氣呵成只有一個依賴:

# makefile 

SRCS = a.cpp b.cpp c.cpp 

HDRS = a.h b.h 

CXXFLAGS = -Wall 

foo: $(SRCS) $(HDRS) 
    g++ $(CXXFLAGS) $(SRCS) -o [email protected] 

編輯

另外,取上面的初始makefile並修復一些小問題:

# Makefile 

# the C++ compiler 
CXX  = g++ 
CC  = $(CXX) 

# options to pass to the compiler 
CXXFLAGS = -Wall -ansi -O2 -g 

Run: Run.o Assessment3.o Student.o 
    $(CXX) $(CXXFLAGS) Run.o Assessment3.o Student.o -o [email protected] 

Run.o: Run.cpp 
    $(CXX) $(CXXFLAGS) Run.cpp -o [email protected] 

Assessment3.o: Assessment3.cpp Assessment3.hpp 
    $(CXX) $(CXXFLAGS) -c Assessment3.cpp -o [email protected] 

Student.o: Student.cpp Student.hpp 
    $(CXX) $(CXXFLAGS) -c Student.cpp -o [email protected] 
0

的「失蹤分離」的錯誤通常是由於沒有使用TAB字符的命令前執行特定的目標。請嘗試以下操作:

Run: Run.cpp Assessment3.o Student.o 
[TAB] $(CXX) $(CXXFLAGS) Run.cpp Assessment3.o Student.o -o Run 

而不是[TAB],您將不得不插入製表符當然。