Currently, I have my makefile set up to compile and make a fairly large project. I have written a second cpp file with main function for running tests. I want these to run separately, but build together and they use the same files. How is this accomplished?
edit: As reference, here is my current makefile. I'm not sure how to adjust it.
CC=g++
CFLAGS=-c -Wall -DDEBUG -g
LDFLAGS=
SOURCES=main.cpp Foo.cpp Bar.cpp Test.cpp A.cpp B.cpp C.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=myprogram
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@
.cpp.o:
$(CC) $(CFLAGS) $< -o $@
Normally you would just have multiple targets and do something like this:
Then you can just
make
(defaults tomake all
), or justmake target
ormake tests
as needed.So for your makefile example above you might want to have something like this:
Here's one way to do it:
This works because built-in rules exist for compiling objects from c++ source (
%.o: %.cpp
) and linking main programs (%: %.o
).Also note the use of target-specific values for the variables
CPPFLAGS
andCXXFLAGS
.