0

I wrote a makefile inspired from here but am unable to run make with the debug flags when generating the intermediate output files (.o files):

CFLAGS=-g -Wall -Wextra -DNDEBUG $(OPTFLAGS)

SOURCES=$(wildcard src/**/*.cpp src/*.cpp)
OBJECTS=$(patsubst %.cpp,%.o,$(SOURCES))

TEST_SRC=$(wildcard tests/*_tests.cpp)
TESTS=$(patsubst %.cpp,%,$(TEST_SRC))

TARGET=build/main.out

all: $(TARGET) tests

$(TARGET): build $(OBJECTS)
    g++ $(OBJECTS) -o $@ 

build:
    @mkdir -p build
    @mkdir -p bin

.PHONY: tests
tests: CFLAGS += $(TARGET)
tests: $(TESTS)
    sh ./tests/runtests.sh

clean:
    rm -rf build $(OBJECTS) $(TESTS)
    rm -f tests/tests.log 
    find . -name "*.gc*" -exec rm {} \;

I get the following output when I run make:

g++    -c -o src/ob/abc.o src/ob/abc.cpp
g++    -c -o src/ob/main.o src/ob/main.cpp
g++ src/ob/abc.o src/ob/main.o -o build/main.out 
sh ./tests/runtests.sh
Running unit tests:

I know that I'm not adding $(CFLAGS) where my target is $(TARGET). How do I add CFLAGS so that when the individual cpp files are being compiled to .o, it happens with the flags defined in CFLAGS?

2
  • 3
    For the c++ compiler use CXXFLAGS instead of CFLAGS. – πάντα ῥεῖ Oct 6 '16 at 14:36
  • Whoops thanks that was it! – user2635088 Oct 6 '16 at 14:40

Browse other questions tagged or ask your own question.