2014-05-13 68 views
1

我想製作一個makefile,它可以爲CppUTest生成一個exe文件。它無法找到標題,我做錯了什麼?第一次做makefile,不是100%確定我在做什麼。CPPUTestMakeFile幫助鏈接

#The compiler to use 
CC = g++ 
LINK = -g -pedantic -Wall -lstdc++ -lpthread -ldl -lm -Wl,-rpath,. 
COMPILE = -g -O3 -D_THREAD_SAFE -pedantic -Wall -c -Wno-deprecated 
#Name of the EXE file to create. 
EXE = ./Tests 
SRCS = $(shell ls *.cpp) 
OBJS = $(subst .cpp,.o,$(SRCS)) 
#Extra flags to give to the C compiler. 
CFLAGS = 
#Libraries to include 
LIBS= -lCppUTestExt -lCppUTest -lm 
#Extra flags to give to the C++ compiler. 
CXXFLAGS = -I/home/mg/DS-5-Workspace/Tests/include  
#Extra flags to give to compilers when they are supposed to invoke the linker, ‘ld’, 
#such as -L. Libraries (-lfoo) should be added to the LDLIBS variable 
#instead.    

LDFLAGS = -L/home/mg/DS-5-Workspace/Tests/cpputest/lib 
#Extra flags to give to the C preprocessor and programs that use it (the C and 
#Fortran  compilers). 

CPPFLAGS = 

.SUFFIXES: .o .cpp 

.cpp.o: 
$(CC) $(CFLAGS) $(CXXFLAGS) $(LDFLAGS) $(COMPILE) $(LIBS) $< 

all: $(OBJS) 
$(CC) $(CFLAGS) $(CXXFLAGS) $(LDFLAGS) $(LIBS) $(OBJS) -o $(EXE) $(LINK) 


-include depend.mak 


depend: 
g++ -MM $(SRCS) > depend.mak 


#static: 
#ar -crvs $(a) $(OBJS) 


#shared: $(OBJS) 
#$(CC) -shared -Wl,-soname -lc -o $(so) $(OBJS) 

clean: 
rm -rf $(OBJS) depend.mak $(EXE) $(so) $(a) 

我有以下錯誤:

error: CppUTest/CommandLineTestRunner.h: No such file or directory

回答

1

好吧,你混了很多的東西。

讓我們清理它和必要的東西保留:

EXE   := Tests 

SRC   := $(wildcard *.cpp) 

OBJ   := $(SRC:%.cpp=obj/%.o) 

DEP   := $(OBJ:.o=.d) 

LDLIBS  := -lCppUTestExt -lCppUTest -lm -lstdc++ -lpthread -ldl 

LDFLAGS  := -L/home/mg/DS-5-Workspace/Tests/cpputest/lib 
LDFLAGS  += -Wl,-rpath,. 

CPPFLAGS := -I/home/mg/DS-5-Workspace/Tests/include 
CPPFLAGS += -MMD -MP -D_THREAD_SAFE 

CXXFLAGS := -W -Wall -Wno-deprecated -pedantic -O3 -g 

.PHONY: all clean fclean re 

all: obj $(EXE) 

clean: 
    $(RM) -r obj 

fclean: clean 
    $(RM) $(EXE) 

re: fclean all 

-include $(DEP) 

$(EXE): $(OBJ) 
    $(CXX) $(LDFLAGS) $^ $(LDLIBS) -o [email protected] 

# %.a: $(OBJ) 
# $(AR) crvs [email protected] $^ 
# ranlib [email protected] 

# %.so: CXXFLAGS += -fPIC 
# %.so: $(OBJ) 
# $(CXX) $(LDFLAGS) $^ $(LDLIBS) -o [email protected] 

obj: 
    @mkdir -p [email protected] 

obj/%.o: %.cpp 
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -o [email protected] -c $< 

幾點說明:

  • 避免$(shell ...)功能,因爲它會每一個變量稱爲如果時間被執行運營商代替=運營商而不是:=運營商。

  • $(CC)是一個內置變量,包含ccgcc(應該是等效的)。使用內置的$(CXX)來使用g++

  • -g-pedantic-O3-Wno-deprecated-Wall是編譯器標誌,它們應該在CFLAGS(對於C)或CXXFLAGS(對於C++)內置變量。

  • -I <path>-D_THREAD_SAFE是預處理器標誌,因此應該在CPPFLAGS內置變量中。

  • -MMD -MP將自動爲每個.o文件生成相關性文件(.d擴展名)。你可以read more here

  • .cpp.o:是後綴規則,而suffix rules are the old-fashioned way of defining implicit rules for make。你應該僅僅依靠這些隱含的規則讓自己知道或者使自己成爲現代的方式。

  • 您不需要自己定義.SUFFIXES:這些廣泛使用的目標。 變量SUFFIXES在make讀取任何makefile之前被定義爲默認的後綴列表。使3.82默認定義這些後綴:

    .SUFFIXES: .out .a .ln .o .c .cc .C .cpp .p .f .F .m .r .y .l .ym .yl .s .S .mod .sym .def .h .info .dvi .tex .texinfo .texi .txinfo .w .ch .web .sh .elc .el 
    

如果您有任何疑問,請繼續。

+0

哇,謝謝,這有幫助。如你所知,這是我的第一個makefile。我仍然有錯誤「未定義引用CommandLineTestRunner :: RunAllTests(int,char **)'」它現在找到頭,但不能運行代碼。 – user1876942

+0

那麼,現在這不再是一個生成文件錯誤。所有.cpp文件都與Makefile位於同一目錄中嗎? – Chnossos

+0

我所有的.cpp文件都在同一個目錄中。我是否也需要將CppUTest文件放在那裏?無論如何,我會將答案標記爲正確的,我想也不會出現makefile錯誤。謝謝。 – user1876942