2017-07-30 121 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无关的链接错误。 –