Project Makefile problems with Google Test

自作多情 提交于 2019-12-12 23:18:03

问题


For the life of me I cannot figure out how to remove the mv statements in the following makefile

TEST_DIR = ../gtest
USER_DIR = src
TESTS_DIR = tests
OBJ_DIR = obj

CPPFLAGS += -isystem $(GTEST_DIR)/include -I$(USER_DIR)

CXXFLAGS += -g -Wall -Wextra

TESTS = test

GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \
                $(GTEST_DIR)/include/gtest/internal/*.h

all : $(TESTS)

clean :
    rm -rf obj
    rm -rf bin
    mkdir obj
    mkdir bin

GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)

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

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

$(OBJ_DIR)/gtest.a : $(OBJ_DIR)/gtest-all.o
    $(AR) $(ARFLAGS) $@ $^ 

$(OBJ_DIR)/gtest_main.a : $(OBJ_DIR)/gtest-all.o $(OBJ_DIR)/gtest_main.o
    $(AR) $(ARFLAGS) $@ $^

$(OBJ_DIR)/addition.o : $(USER_DIR)/addition.cpp $(USER_DIR)/addition.h $(GTEST_HEADERS)
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< 
    mv addition.o obj/addition.o

$(OBJ_DIR)/test.o : $(TESTS_DIR)/test.cpp $(USER_DIR)/addition.h $(GTEST_HEADERS)
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(TESTS_DIR)/test.cpp 
    mv test.o obj/test.o

test : $(OBJ_DIR)/addition.o $(OBJ_DIR)/test.o $(OBJ_DIR)/gtest_main.a
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@

The problem is the mv test.o obj/test.o line and the others like it. I know there is a way to have make do this automatically for you but for the life of me I cannot find/figure it out.

This is the precanned makefile that comes with google test that I have modified to work for me.


回答1:


Something like

CPPFLAGS += -MMD -MP

gtest_objs := $(OBJ_DIR)/gtest_all.o $(OBJ_DIR)/gtest_main.o
my_objs    := $(OBJ_DIR)/addition.o $(OBJ_DIR)/test.o
all_objs   := $(gtest_objs) $(objs)

deps       := $(all_objs:.o=.d)

$(gtest_objs): CPPFLAGS += -I$(GTEST_DIR)
$(gtest_objs): $(OBJ_DIR)/gtest_%.o: $(GTEST_DIR)/src/gtest_%.cc
$(my_objs): $(OBJ_DIR)/%.o: $(USER_DIR)/%.cpp
$(all_objs):
    $(COMPILE.cpp) $(OUTPUT_OPTION) $<

-include $(deps)

The rule for all_objs is copied from the builtin rule, and the deps-related stuff will generate dependencies for you automatically.



来源:https://stackoverflow.com/questions/37382804/project-makefile-problems-with-google-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!