2011-12-06 75 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],您将不得不插入制表符当然。