2017-07-30 50 views
0

我想爲未來的項目設置一個模板文件夾。我將爲我的單元測試合併Google測試。我遇到的問題是我的makefile沒有達到我期望的水平。任何幫助是極大的讚賞!makefile的問題C++ Linux

這裏是我的項目結構:

project_root Makefile /test <-- contains google test files /src <-- my cpp files

這裏是我的Makefile:

# Google Test location 
GTEST_DIR = test 

# Where to find user code. 
USER_DIR = src 

# Flags passed to the preprocessor. 
CPPFLAGS += -isystem $(GTEST_DIR)/include 

# Flags passed to the C++ compiler. 
CXXFLAGS += -g -Wall -Wextra -pthread 

# All tests produced by this Makefile. Remember to add new tests you 
# created to the list. 
TESTS = sample1_unittest 

.PHONY : all 
all: $(EXE) 

clean : 
    rm -f $(TESTS) gtest.a gtest_main.a *.o 

# All Google Test headers. Usually you shouldn't change this 
# definition. 
GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \ 
       $(GTEST_DIR)/include/gtest/internal/*.h 

# Builds gtest.a and gtest_main.a. 
GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS) 

gtest-all.o : $(GTEST_SRCS_) 
    $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \ 
      $(GTEST_DIR)/src/gtest-all.cc 

gtest_main.o : $(GTEST_SRCS_) 
    $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \ 
      $(GTEST_DIR)/src/gtest_main.cc 

gtest.a : gtest-all.o 
    $(AR) $(ARFLAGS) [email protected] $^ 

gtest_main.a : gtest-all.o gtest_main.o 
    $(AR) $(ARFLAGS) [email protected] $^ 

# Builds a sample test. A test should link with either gtest.a or 
# gtest_main.a, depending on whether it defines its own main() 
# function. 

### these next few lines would do the job, but i'm trying to automate 
### things a bit so they are commented out 

#sample1.o : $(USER_DIR)/sample1.cc $(USER_DIR)/sample1.h $(GTEST_HEADERS) 
# $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/sample1.cc 
# 
#sample1_unittest.o : $(USER_DIR)/sample1_unittest.cc \ 
#      $(USER_DIR)/sample1.h $(GTEST_HEADERS) 
# $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/sample1_unittest.cc 
# 
#sample1_unittest : sample1.o sample1_unittest.o gtest_main.a 
# $(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o [email protected] 


# here is where my trouble begins: 
SRC = $(USER_DIR)/sample1.cc $(USER_DIR)/sample1_unittest.cc 
OBJ=$(SRC:.cc=.o) 
EXE=go 

%.o : %.cc $(GTEST_HEADERS) 
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -o [email protected] -c $< 


$(EXE): $(OBJ) gtest_main.a 
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $< -o [email protected] 

你能查看評論 「# here is where my trouble begins

當我運行$ make下的部分我收到以下錯誤:

make: Nothing to be done for 'all'.

我一直在使用類似的makefile很長一段時間,但現在我已經爲單元測試添加了它,並將src文件移動到它自己已損壞的文件夾中。

我將在未來的所有版本中使用它,所以我現在非常希望能夠很好地完成此設置。

非常感謝您提供任何幫助。

謝謝你的修復。

我提出的以下3行以上.PHONY:所有

SRC = $(USER_DIR)/sample1.cc $(USER_DIR)/sample1_unittest.cc 
OBJ=$(SRC:.cc=.o) 
EXE=go 

我也有一個最後的錯誤,改變最後一行應該是:

$(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o [email protected] 
+0

你期望它做什麼? – Beta

+0

@貝塔我期望它建立一個可執行測試甜蜜叫「去」 – posop

+0

cmake更容易和更好! – sailfish009

回答

0

它看起來如EXEall: $(EXE)中調用時未定義。 它只被定義得低得多。所以你的全部:翻譯時只有all:

+0

這解決了這個問題,我有一個輔助構建問題'collect2:錯誤:ld返回1退出狀態 Makefile:68:目標的配方'去'失敗'我會追查一下,除非你有任何建議 – posop

+0

這是一個與makefile無關的鏈接錯誤。 –