1

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 Answers1

0

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.

user657267
  • 20,568
  • 5
  • 58
  • 77
  • I'm sorry, I guess I don't understand. I replaced everything in my makefile below GTEST_HEADERS with the code you provided. I get an error: target `obj/gtest-all.o' doesn't match the target pattern – J Andrew McCormick May 23 '16 at 17:33
  • @JAndrewMcCormick I only noticed now the filenames are different, change `gtest-all.cc` to `gtest_all.cc`, I've changed the makefile accordingly. – user657267 May 23 '16 at 21:53