1# A sample Makefile for building Google Test and using it in user 2# tests. Please tweak it to suit your environment and project. You 3# may want to move it to your project's root directory. 4# 5# SYNOPSIS: 6# 7# make [all] - makes everything. 8# make TARGET - makes the given target. 9# make clean - removes all files generated by make. 10 11# Please tweak the following variable definitions as needed by your 12# project, except GTEST_HEADERS, which you can use in your own targets 13# but shouldn't modify. 14 15# Points to the root of Google Test, relative to where this file is. 16# Remember to tweak this if you move this file. 17GTEST_DIR = .. 18 19# Points to the location of the Google Test libraries 20GTEST_LIB_DIR = . 21 22# Where to find user code. 23USER_DIR = ../samples 24 25# Flags passed to the preprocessor. 26# Set Google Test's header directory as a system directory, such that 27# the compiler doesn't generate warnings in Google Test headers. 28CPPFLAGS += -isystem $(GTEST_DIR)/include 29 30# Flags passed to the C++ compiler. 31CXXFLAGS += -g -Wall -Wextra -pthread -std=c++11 32 33# Google Test libraries 34GTEST_LIBS = libgtest.a libgtest_main.a 35 36# All tests produced by this Makefile. Remember to add new tests you 37# created to the list. 38TESTS = sample1_unittest 39 40# All Google Test headers. Usually you shouldn't change this 41# definition. 42GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \ 43 $(GTEST_DIR)/include/gtest/internal/*.h 44 45# House-keeping build targets. 46 47all : $(GTEST_LIBS) $(TESTS) 48 49clean : 50 rm -f $(GTEST_LIBS) $(TESTS) *.o 51 52# Builds gtest.a and gtest_main.a. 53 54# Usually you shouldn't tweak such internal variables, indicated by a 55# trailing _. 56GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS) 57 58# For simplicity and to avoid depending on Google Test's 59# implementation details, the dependencies specified below are 60# conservative and not optimized. This is fine as Google Test 61# compiles fast and for ordinary users its source rarely changes. 62gtest-all.o : $(GTEST_SRCS_) 63 $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \ 64 $(GTEST_DIR)/src/gtest-all.cc 65 66gtest_main.o : $(GTEST_SRCS_) 67 $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \ 68 $(GTEST_DIR)/src/gtest_main.cc 69 70libgtest.a : gtest-all.o 71 $(AR) $(ARFLAGS) $@ $^ 72 73libgtest_main.a : gtest-all.o gtest_main.o 74 $(AR) $(ARFLAGS) $@ $^ 75 76# Builds a sample test. A test should link with either gtest.a or 77# gtest_main.a, depending on whether it defines its own main() 78# function. 79 80sample1.o : $(USER_DIR)/sample1.cc $(USER_DIR)/sample1.h $(GTEST_HEADERS) 81 $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/sample1.cc 82 83sample1_unittest.o : $(USER_DIR)/sample1_unittest.cc \ 84 $(USER_DIR)/sample1.h $(GTEST_HEADERS) 85 $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/sample1_unittest.cc 86 87sample1_unittest : sample1.o sample1_unittest.o $(GTEST_LIBS) 88 $(CXX) $(CPPFLAGS) $(CXXFLAGS) -L$(GTEST_LIB_DIR) -lgtest_main -lpthread $^ -o $@ 89