Secondary expansion in make prereqs with patsubst - c++

I have the following make file:
.SECONDEXPANSION:
CXX = g++
CXXFLAGS = -Wall -W -ansi -I. -I../Include
BUILD_DIR = obj
OUT = bin/EngineTests
SRC = LiveTestReporterStdout.cpp \
EngineTests.cpp \
IntegratorValidationTest.cpp \
PRIVATE_SRC = Private/Unit/BlockTests/AlertBlockTest.cpp \
Private/Unit/BlockTests/CapBlockTest.cpp \
Private/TestHelpers.cpp
OBJS = $(patsubst %.cpp, $(BUILD_DIR)/%.o, $(SRC))
all: $(OUT)
private: SRC += $(PRIVATE_SRC)
private: $(OUT)
$(BUILD_DIR)/%.o : %.cpp
#echo $(CXX) $(CXXFLAGS) $<
#mkdir -p $(dir $#)
#$(CXX) $(CXXFLAGS) -c $< -o $#
$(OUT): $(OBJS)
#echo Linking $# ...
# blah
The goal being able to do make to build all from just $(SRC) and make private to build from $(SRC) + $(PRIVATE_SRC). But it doesnt work - I'm aware that target specific variables cant be used for pre-reqs and the solution somehow involves secondary expansion, but cant get it to work.
I have tried OBJS = $$(patsubst %.cpp, $(BUILD_DIR)/%.o, $$(SRC)) and various things like that.
Many thanks

Related

make: unable to locate user-defined header file that is included?

When I run make, I get the following error:
make: *** No rule to make target Menu.h', needed by Menu.o'. Stop.
Here is my Makefile:
PROG = sim
CURR_PATH = ~/Projects/restaurant/cpp/
CC = g++
CPPFLAGS = -g -v -Wall $(LOCAL_INCLUDES) -I$(BOOST_ROOT)
ODIR = ./bin
SDIR = ./src
LOCAL_INCLUDES = $(patsubst %,-I$(CURR_PATH)src/%,$(PKG_DIRS))
PKG_DIRS = $(shell ls $(SDIR))
FIND_SRC_FILES = $(notdir $(wildcard $(SDIR)/$(pkg)/*.cpp))
SRC_FILES = $(foreach pkg,$(PKG_DIRS),$(FIND_SRC_FILES))
OBJ_FILES = $(patsubst %.cpp,%.o,$(SRC_FILES))
MAIN_OBJ = main.o
.PHONY : prog
prog : $(PROG)
all : ; $(info $$CPPFLAGS is [${CPPFLAGS}])#echo Hello world
$(PROG) : $(OBJ_FILES)
$(CC) $(CPPFLAGS) -o $(PROG) $(MAIN_OBJ)
%.o : %.cpp
$(CC) $(CPPFLAGS) -c $< -o $#
$(OBJ_FILES) : %.o : %.h
$(CC) $(CPPFLAGS) -c $(patsubst %.h,%.cpp,$<) -o $#
BTW, in case you're wondering what LOCAL_INCLUDES looks like, the output for the 'all' recipe is the following:
$CPPFLAGS is [-g -v -Wall -I~/Projects/restaurant/cpp/src/concurrent -I~/Projects/restaurant/cpp/src/containers -I~/Projects/restaurant/cpp/src/data -I~/Projects/restaurant/cpp/src/loader -I~/Projects/restaurant/cpp/src/main -I~/Projects/restaurant/cpp/src/people -I~/Projects/restaurant/cpp/src/sim -I/usr/local/boost_1_72_0]
Hello world
Sorry for the single line output, I am unaware of how to format in a more readable fashion. But as you can see, the directory data, which contains Menu.h, is being properly included. But for some reason, make is unable to find it. What could possibly be going wrong here?
Let me know if you need more information.
Cheers
The compiler knows how to find that header file. Make does not know how to find it, and Make is the one producing that error message.
I suggest you make this modification:
INCLUDE_DIRS = $(addprefix $(CURR_PATH)src/,$(PKG_DIRS))
LOCAL_INCLUDES = $(addprefix -I,$(INCLUDE_DIRS))
vpath %.h $(INCLUDE_DIRS)
(P.S. Your use of CURR_PATH and . is confusing, and data/ is a terrible place to put header files.)
EDIT:
All right, let's take this in stages. Step 1, try this makefile:
OBJ_FILES = Menu.o
INCLUDE_DIRS = ~/Projects/restaurant/cpp/src/data
vpath %.h $(INCLUDE_DIRS)
Menu.o:
$(OBJ_FILES) : %.o : %.cpp %.h
#echo building $# from $^
and tell us what happens. (If it doesn't work, tell us what happens.)
As aforementioned, make's vpath did not like the absolute path that it was being given. See below for my modified Makefile:
PROG = sim
CURR_PATH = ~/Projects/restaurant/cpp/
CC = g++
CPPFLAGS = -g -v -Wall $(LOCAL_INCLUDES) -I$(BOOST_ROOT)
ODIR = bin
# vpath only needs SDIR and PKG_DIRS!
SDIR = src
PKG_DIRS = $(shell ls $(SDIR))
INCLUDE_DIRS = $(addprefix $(CURR_PATH)src/,$(PKG_DIRS))
LOCAL_INCLUDES = $(addprefix -I,$(INCLUDE_DIRS))
FIND_SRC_FILES = $(notdir $(wildcard $(SDIR)/$(pkg)/*.cpp))
SRC_FILES = $(foreach pkg,$(PKG_DIRS),$(FIND_SRC_FILES))
OBJ_FILES = $(patsubst %.cpp,%.o,$(SRC_FILES))
MAIN_OBJ = main.o
vpath %.h $(addprefix $(SDIR)/,$(PKG_DIRS))
.PHONY : all
prog : $(PROG)
all : ; $(info $$CPPFLAGS is [${CPPFLAGS}])#echo Hello world
$(PROG) : $(OBJ_FILES)
$(CC) $(CPPFLAGS) -o $(PROG) $(MAIN_OBJ)
%.o : %.cpp
$(CC) $(CPPFLAGS) -c $< -o $#
$(OBJ_FILES) : %.o : %.h
$(CC) $(CPPFLAGS) -c $(patsubst %.h,%.cpp,$<) -o $#
I hope this helps other people that come across similar issues.

Compiling multiple test files efficiently with Makefile

I have a C++ project with the following structure:
/Project
Makefile
/src (.cpp source files)
...
/include (.h header files)
...
/libs
...
/build (.o object files)
...
/tests (target .cpp files I want to compile)
test1.cpp
test2.cpp
test3.cpp
...
/bin (output directory for compiled files)
...
For the tests inside my test file, I would like to be able to
Compile them individually, e.g. "make test1", "make test2"
Compile them all at once
But I would like to be able to do this without needing to define new variables (e.g. TARGET1, TARGET2,...) for each new test file, nor add a bunch of new lines to my makefile for each new test file.
For example, right now I have something like:
CXX = g++
SRC_DIR = ./src
BUILD_DIR = ./build
LIB = -I libs
INC = -I include
SRCS = $(shell find $(SRC_DIR) -type f -name *.cpp)
OBJS = $(patsubst $(SRC_DIR)/%, $(BUILD_DIR)/%, $(SRCS:.cpp=.o))
TARGET1 ?= test1.cpp
TARGET2 ?= test2.cpp
TARGET3 ?= test3.cpp
all: $(OBJS)
$(CXX) ./tests/$(TARGET1).cpp $(LIB) $(INC) $^ -o ./bin/$(TARGET1)
$(CXX) ./tests/$(TARGET2).cpp $(LIB) $(INC) $^ -o ./bin/$(TARGET2)
$(CXX) ./tests/$(TARGET3).cpp $(LIB) $(INC) $^ -o ./bin/$(TARGET3)
$(TARGET1): $(OBJS)
$(CXX) ./tests/$(TARGET1).cpp $(LIB) $(INC) $^ -o ./bin/$(TARGET1)
$(TARGET2): $(OBJS)
$(CXX) ./tests/$(TARGET2).cpp $(LIB) $(INC) $^ -o ./bin/$(TARGET2)
$(TARGET3): $(OBJS)
$(CXX) ./tests/$(TARGET3).cpp $(LIB) $(INC) $^ -o ./bin/$(TARGET3)
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp
$(CXX) $(INC) -c -o $# $<
which does the job, but isn't very scalable. How could I do this scalably?
Make has some more tricks that you can use (not tested):
CXX = g++
SRC_DIR = src
BUILD_DIR = build
TEST_DIR = tests
BIN_DIR = bin
LIB = -I libs
INC = -I include
SRCS = $(wildcard $(SRC_DIR)/*.cpp)
OBJS = $(patsubst $(SRC_DIR)/%.cpp,$(BUILD_DIR)/%.o,$(SRCS))
TESTS = $(wildcard $(TEST_DIR)/*.cpp)
TARGETS = $(patsubst $(TEST_DIR)/%.cpp,$(BIN_DIR)/%,$(TESTS))
all: $(TARGETS)
$(TARGETS): $(BIN_DIR)/%: $(OBJS)
$(CXX) $(TEST_DIR)/$*.cpp $(LIB) $(INC) $^ -o $#
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp
$(CXX) $(INC) -c -o $# $<
The main trick here is the static pattern rule for $(TARGETS): in the recipe $* expands as the stem of the pattern. The other tricks are a simpler use of patsubst and the use of wildcard instead of the less efficient shell find. Note that this last one works only if your source files are flat in src, not if they are organized in a hierarchy of sub-directories.
But this does not answer your most tricky request: a way to invoke make testX instead of make bin/testX. So, here is the most tricky part:
SHORTERTARGETS = $(patsubst $(TEST_DIR)/%.cpp,%,$(TESTS))
.PHONY: $(SHORTERTARGETS)
# $(1): short target
define TARGETS_rule
$(1): $(BIN_DIR)/$(1)
endef
$(foreach t,$(SHORTERTARGETS),$(eval $(call TARGETS_rule,$(t))))
You can even use this foreach-eval-call to factorize other parts of your Makefile:
CXX = g++
SRC_DIR = src
BUILD_DIR = build
TEST_DIR = tests
BIN_DIR = bin
LIB = -I libs
INC = -I include
SRCS = $(wildcard $(SRC_DIR)/*.cpp)
OBJS = $(patsubst $(SRC_DIR)/%.cpp,$(BUILD_DIR)/%.o,$(SRCS))
TESTS = $(wildcard $(TEST_DIR)/*.cpp)
TARGETS = $(patsubst $(TEST_DIR)/%.cpp,$(BIN_DIR)/%,$(TESTS))
SHORTERTARGETS = $(patsubst $(TEST_DIR)/%.cpp,%,$(TESTS))
.PHONY: all $(SHORTERTARGETS)
all: $(TARGETS)
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp
$(CXX) $(INC) -c -o $# $<
# $(1): short target
define TARGETS_rule
$(1): $(BIN_DIR)/$(1)
$(BIN_DIR)/$(1): $(OBJS)
$(CXX) $(TEST_DIR)/$(1).cpp $(LIB) $(INC) $$^ -o $$#
endef
$(foreach t,$(SHORTERTARGETS),$(eval $(call TARGETS_rule,$(t))))
The most difficult to understand in this last version is the need of $$in the recipe (double expansion). But here the GNU make manual is your friend.

How to create multiple executables using makefile from a single target

I am trying to build excutables for multiple files which are built in the same way. When i run make all the excutables should be generated. I am getting error at prerequisites part of the macro.
CXX = g++
CXX_FLAGS = -g -Wall
LD_FLAGS =
INC_DIR = -I/my/path/include
SRC_DIR = .
LIB_DIR = -L$/my/path/lib
OBJ_DIR = obj
EXE_DIR = exe
SRCS := $(foreach s_dir, $(SRC_DIR), $(wildcard $(s_dir)/*.cpp))
OBJS := $(patsubst $(SRC_DIR)/%.cpp, $(OBJ_DIR)/%.o, $(SRCS))
EXES := $(patsubst $(SRC_DIR)/%.cpp, $(EXE_DIR)/%.out, $(SRCS))
all: create_directories create_objects create_exes
create_directories:
#echo "Creating $(OBJ_DIR) and $(EXE_DIR)..."
#mkdir -p obj
#mkdir -p exe
create_objects:
$(foreach b_dir, $(OBJ_DIR), $(eval $(call build-objects, $(b_dir))))
create_exes:
$(foreach ot, $(EXE_DIR), $(eval $(call build-exes, $(ot))))
define build-objects
$1/%.o: %.cpp
$(CXX) $(CXX_FLAGS) $(INC_DIR) -MMD -MP -c $$< -o $$#
endef
define build-exes
$1/%.out:obj/%.o
$(CXX) $(LD_FLAGS) -o $# $(OBJS) $(LIB_DIR) -lmylib
endef
Is this a right way to do generate multiple exes or any other simple way?
If I'm reading this makefile right, then it's much too complicated.
First let's have a rule to build object files:
$(OBJ_DIR)/%.o: %.cpp
$(CXX) $(CXX_FLAGS) $(INC_DIR) -MMD -MP -c $< -o $#
Now if we're not sure about the existence of obj/, we could add a rule to create it, but for the moment let's just put in a failsafe (we'll come back to this later):
$(OBJ_DIR)/%.o: %.cpp
#mkdir -p $(OBJ_DIR)
$(CXX) $(CXX_FLAGS) $(INC_DIR) -MMD -MP -c $< -o $#
And a similar rule to build the executables:
$(EXE_DIR)/%.out: $(OBJ_DIR)/%.o
#mkdir -p $(EXE_DIR)
$(CXX) $(LD_FLAGS) -o $# $^ $(LIB_DIR) -lmylib
And finally (at the top) some variables, the lists of files, and the all rule:
CXX = g++
CXX_FLAGS = -g -Wall
LD_FLAGS =
INC_DIR = -I/my/path/include
SRC_DIR = .
LIB_DIR = -L$/my/path/lib
OBJ_DIR = obj
EXE_DIR = exe
SRCS := $(wildcard $(SRC_DIR)/*.cpp)
EXES := $(patsubst $(SRC_DIR)/%.cpp, $(EXE_DIR)/%.out, $(SRCS))
# Let the object files take care of themselves
all: $(EXES)
That's all you need. Once this is working perfectly, we can discuss refinements like rules for building directories.

No rule to make target ***

I am currently trying to build Face Tracker (by Jason Saragih) with "make" in my Windows 7 x64, but all I'm getting is: "No rule to make target src/lib/IO.o, needed by bin/face_tracker".
Does anyone have any idea of what is happening, please?
This is the makefile (also available in Jason Saragih's page):
# Paths
OPENCV_PATH=/OpenCV246/
# Programs
CC=
CXX=g++
# Flags
ARCH_FLAGS=-arch x86_64
CFLAGS=-Wextra -Wall -pedantic-errors $(ARCH_FLAGS) -O3
LDFLAGS=$(ARCH_FLAGS)
DEFINES=
INCLUDES=-I$(OPENCV_PATH)/include -Iinclude/
LIBRARIES=-L$(OPENCV_PATH)/lib -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_objdetect
# Files which require compiling
SOURCE_FILES=\
src/lib/IO.cc\
src/lib/PDM.cc\
src/lib/Patch.cc\
src/lib/CLM.cc\
src/lib/FDet.cc\
src/lib/PAW.cc\
src/lib/FCheck.cc\
src/lib/Tracker.cc
# Source files which contain a int main(..) function
SOURCE_FILES_WITH_MAIN=src/exe/face_tracker.cc
# End Configuration
SOURCE_OBJECTS=$(patsubst %.cc,%.o,$(SOURCE_FILES))
ALL_OBJECTS=\
$(SOURCE_OBJECTS) \
$(patsubst %.cc,%.o,$(SOURCE_FILES_WITH_MAIN))
DEPENDENCY_FILES=\
$(patsubst %.o,%.d,$(ALL_OBJECTS))
all: bin/face_tracker
%.o: %.cc Makefile
## Make dependecy file
$(CXX) -MM -MT $# -MF $(patsubst %.cc,%.d,$<) $(CFLAGS) $(DEFINES) $(INCLUDES) $<
## Compile
$(CXX) $(CFLAGS) $(DEFINES) $(INCLUDES) -c -o $# $<
-include $(DEPENDENCY_FILES)
bin/face_tracker: $(ALL_OBJECTS)
$(CXX) $(LDFLAGS) $(LIBRARIES) -o $# $(ALL_OBJECTS)
.PHONY: clean
clean:
#echo "Cleaning"
#for pattern in '*~' '*.o' '*.d' ; do \
find . -name "$$pattern" | xargs rm ; \
done
Thanks,
Fabio

How to use qmake file with google test and shared library

I started with a makefile that would generate the dependencies of my C++ files. It was a C++ project using google test. Later, I started a Qt project which uses qmake and links to a shared library which the old makefile builds. Needless to say, the old makefile is really complicated now.
I would like to make a qmake file which can do the following:
Build a shared library for a list a sources
Build google test (optionally, I would accept a separate makefile for this)
Build my Qt executable with a different list of sources linking to the first shared library
All builds should have debug and release versions which will output to different directories
Can someone point me in the right direction for making a *.pro file which will do that? I'm really not clear on how to do things like multiple targets in qmake.
Here is the current makefile I am using (clearly a mess):
GTEST_DIR = /home/matt/lib/gtest-1.5.0
GMOCK_DIR = /home/matt/lib/gmock-1.5.0
SRC_DIR = /home/matt/Documents/myproject
QTINC := -I/usr/share/qt4/mkspecs/linux-g++ -I/usr/include/qt4/QtCore \
-I/usr/include/qt4/QtGui -I/usr/include/qt4
TEST_SRCS = test/TestRunner.cpp test/CellTest.cpp test/PuzzleTest.cpp \
test/SingleCandidateMethodTest.cpp test/ExclusionMethodTest.cpp \
test/BlockIntersectionMethodTest.cpp test/CoveringSetMethodTest.cpp \
test/SimpleValidatorTest.cpp test/PuzzleMarkerTest.cpp \
test/PlayerValidatorTest.cpp test/SolverHelperTest.cpp \
test/GuessCommandTest.cpp test/MarkCommandTest.cpp \
test/UnmarkCommandTest.cpp test/MethodSolverTest.cpp \
test/SimplePuzzleImporterTest.cpp test/SolvedPuzzleImporterTest.cpp \
test/AddHintMarksCommandTest.cpp test/CellControllerTest.cpp \
test/PuzzleControllerTest.cpp
QT_SRCS =
LIB_SRCS = Puzzle.cpp Cell.cpp SingleCandidateMethod.cpp ExclusionMethod.cpp \
BlockIntersectionMethod.cpp CoveringSetMethod.cpp SimpleValidator.cpp \
PuzzleMarker.cpp PlayerValidator.cpp SolverHelper.cpp GuessCommand.cpp \
MarkCommand.cpp UnmarkCommand.cpp MethodSolver.cpp \
SimplePuzzleImporter.cpp SolvedPuzzleImporter.cpp GameManager.cpp \
CellController.cpp AddHintMarksCommand.cpp GameController.cpp \
PuzzleController.cpp
DEPDIR = .deps
df = $(DEPDIR)/$(#F)
# preprocessor
CPPFLAGS += -I$(GTEST_DIR)/include -I$(GMOCK_DIR)/include -I$(SRC_DIR) $(QTINC)
# C++ compiler
CXXFLAGS = -Wall -std=c++0x
# qt defines
QTDEF = -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED
# stuff to link for Qt
QTFLAGS = -L/usr/lib -lQtCore -lQtGui -lpthread
# gtest headers, don't need to change
GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \
$(GTEST_DIR)/include/gtest/internal/*.h
# gmock stuff, don't need to change
GMOCK_HEADERS = $(GMOCK_DIR)/include/gmock/*.h \
$(GMOCK_DIR)/include/gmock/internal/*.h \
$(GTEST_HEADERS)
MAKEDEPEND = $(CXX) $(CPPFLAGS) -MM -o $(df).d $<
MAKEDEPEND_TEST = $(CXX) $(CPPFLAGS) -MM -o $(df).d -MT $(basename $<).o $<
MAKEDEPEND_QT = $(CXX) $(CPPFLAGS) -MM -o $(df).d -MT $(basename $<).o $<
SRCS := main.cpp $(LIB_SRCS)
OBJS := $(SRCS:%.cpp=%.o)
LIB_OBJS := $(LIB_SRCS:%.cpp=%.o)
QT_OBJS := $(QT_SRCS:%.cpp=%.o)
TEST_OBJS := $(TEST_SRCS:%.cpp=%.o)
# targets:
debug : CXXFLAGS += -g -O0
# removed this warning because it sucks: -Wconversion (int to size_t!)
debug_warn : CXXFLAGS += -pedantic -Wextra
debug_warn : debug
debug : all
release : CXXFLAGS += -O2
release : all
lib : CXXFLAGS += -fPIC
lib : libSudokuLib.so
libSudokuLib.so : $(LIB_OBJS)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -shared -o libSudokuLib.so $(LIB_OBJS)
all : sudoku run_tests
sudoku : $(OBJS) $(QT_OBJS)
$(CXX) $(CPPFLAGS) $(QTDEF) $(CXXFLAGS) $(QTFLAGS) $^ -o $#
run_tests : $(LIB_OBJS) $(TEST_OBJS) gtest.a gmock.a
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $#
# dependency stuff
.D_TARGET:
mkdir -p $(DEPDIR)
touch $#
.PRECIOUS: .D_TARGET
# GTEST building stuff don't touch me
GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)
GMOCK_SRCS_ = $(GMOCK_DIR)/src/*.cc $(GMOCK_HEADERS)
gtest-all.o : $(GTEST_SRCS_)
$(CXX) $(CPPFLAGS) -I$(GTEST_DIR) -I$(GMOCK_DIR) $(CXXFLAGS) -c \
$(GTEST_DIR)/src/gtest-all.cc
gmock-all.o : $(GMOCK_SRCS_)
$(CXX) $(CPPFLAGS) -I$(GTEST_DIR) -I$(GMOCK_DIR) $(CXXFLAGS) \
-c $(GMOCK_DIR)/src/gmock-all.cc
gmock_main.o : $(GMOCK_SRCS_)
$(CXX) $(CPPFLAGS) -I$(GTEST_DIR) -I$(GMOCK_DIR) $(CXXFLAGS) \
-c $(GMOCK_DIR)/src/gmock_main.cc
gmock.a : gmock-all.o gtest-all.o
$(AR) $(ARFLAGS) $# $^
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) $# $^
gtest_main.a : gtest-all.o gtest_main.o
$(AR) $(ARFLAGS) $# $^
# QT stuff
%Qt.o : %Qt.o .D_TARGET
$(MAKEDEPEND_QT);
#cp $(df).d $(df).P;
# sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
# -e '/^$$/ d' -e 's/$$/ :/' < $(df).d >> $(df).P;
#rm -f $(df).d
$(CXX) $(CPPFLAGS) $(QTDEF) $(CXXFLAGS) -o $# -c $<
# tests
%Test.o : %Test.cpp .D_TARGET $(GMOCK_HEADERS)
$(MAKEDEPEND_TEST);
#cp $(df).d $(df).P;
# sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
# -e '/^$$/ d' -e 's/$$/ :/' < $(df).d >> $(df).P;
#rm -f $(df).d
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $# -c $<
# objects from sources
%.o : %.cpp .D_TARGET
$(MAKEDEPEND);
#cp $(df).d $(df).P; \
# sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
# -e '/^$$/ d' -e 's/$$/ :/' < $(df).d >> $(df).P;
#rm -f $(df).d
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $# -c $<
-include $(QT_SRCS:%.cpp=$(DEPDIR)/%.o.P)
-include $(TEST_SRCS:test/%.cpp=$(DEPDIR)/%.o.P)
-include $(SRCS:%.cpp=$(DEPDIR)/%.o.P)
clean:
$(RM) $(OBJS) $(TEST_OBJS) $(QT_OBJS) \
gtest.a gtest_main.a gtest-all.o gtest_main.o \
.D_TARGET sudoku run_tests
rm -rf $(DEPDIR)
And here is the project.pro file for qmake (which relies on that first makefile instead of building the library itself)
TEMPLATE = app
TARGET = qtsudoku
DEPENDPATH += .
INCLUDEPATH += . ../myproject
CONFIG += qt warn_on debug
QMAKE_CXXFLAGS += -std=c++0x
LIBS += -L/home/matt/Documents/myproject -lSudokuLib
# Input
HEADERS += QtPuzzleModel.h QtPuzzleView.h QtGameApplication.h QtDirector.h \
QtMainWindow.h QtFactory.h
SOURCES += main.cpp QtPuzzleModel.cpp QtGameApplication.cpp QtDirector.cpp \
QtMainWindow.cpp QtFactory.cpp
In general, a good way to do something like this is with the SUBDIRS qmake template. You would make a qmake file for each of the items you want to build (shared library, google test, and the executable), then make a SUBDIRS template to do those in order. I think the subdirs template will provide the debug/release flags to each underlying make file.
For the shared library, the qmake library template should be fine.
I don't know about google test, I assume you could generate a qmake file for it if desired, or you could continue with the makefile.
For linking the two, you could make a qmake file that has a main.cpp, specifies the others as libraries, and builds an executable.
You can use DESTDIR, MOC_DIR, OBJECTS_DIR, and UI_DIR to change where generated files go.