I have a makefile for a project that I want to be used for debug and release builds. For the debug build I have to include an extra cpp file that holds all the unit tests. I have added a debug option to the makefile and everything seems to work except for one line.
My makefile is pretty long, but the offending line is in this block:
debug: CXXFLAGS+=-DDEBUG_TEST
debug: CXXFLAGS+=-DTEST_DIRECTORY='"$(subst /makefile,,$(abspath $(lastword $(MAKEFILE_LIST))))/tests"'
debug: SRCS+=src/test.cpp
debug: flex bison $(SRCS) $(EXE)
The first two lines that add to CXXFLAGS are parsed successfully, as well as the last line which does the compilation. However, it seems that no matter what I do SRCS never gets the test file appended to it. I have even printed the contents before and after and it shows no change. What am I doing wrong?
I am using GNU Make 3.81 on windows.
Edit: I've decided to add the entire makefile to help answering.
CXX=g++
CXXFLAGS+=-std=c++11 -U__STRICT_ANSI__
FLEX=win_flex.exe
BISON=win_bison.exe
FLEXBISONSRCS=src/parser.cpp src/tokens.cpp src/math_parser.cpp src/math_tokens.cpp
SRCS=$(FLEXBISONSRCS) src/main.cpp src/assembler.cpp src/instruction.cpp src/util.cpp
OBJS=$(SRCS:.cpp=.o)
EXE=bin/yasa
debug: CXXFLAGS+=-DDEBUG_TEST
debug: CXXFLAGS+=-DTEST_DIRECTORY='"$(subst /makefile,,$(abspath $(lastword $(MAKEFILE_LIST))))/tests"'
debug: SRCS+=src/test.cpp
all debug: flex bison $(SRCS) $(EXE)
@echo ' $$^: $^'
@echo ' $$(SRCS): $(SRCS)'
$(EXE): $(OBJS)
$(CXX) $(OBJS) -o $@
.cpp.o:
$(CXX) $(CXXFLAGS) $< -c -o $@
flex:
$(FLEX) -o src/tokens.cpp src/65c816.l
$(FLEX) -o src/math_tokens.cpp src/math.l
bison:
$(BISON) -d -o src/parser.cpp src/65c816.y
$(BISON) -d -o src/math_parser.cpp src/math.y
rebuild: clean all
clean:
rm src/*.o
debug
target instead of havingdebug
have the build recipe (or live with the fact that changes to that test file won't trigger a make rebuild of thedebug
binary). – Etan ReisnerSRCS
but the scope of that change is limited. See gist.github.com/deryni/897ff4584abc34fa8851 . – Etan Reisner$(info $$SRCS is [${SRCS}])
to print out the value. The output doesn't change after the assignment and test.cpp never gets compiled even on a clean build. – ozdrgnaDiies$(info)
call won't see it. The assignment is scoped such that that can't function. What is the body of thedebug
target? It must be doing the compilation itself (as you have the.c
files and not object files listed as prerequisites). I'm assuming it probably uses$^
in the recipe? Using$(SRCS)
here would "fix" the problem but ultimately the simpler solution is to make the assignment global based on target being built and not target-specific. I'll write up an answer. – Etan Reisner