Reputation: 55
Let's say I have two C++ main files, project.cpp
, and projectgraph.cpp
.
They both use the same header file functions.h
, as well as the file functions.cpp
.
To compile project.cpp
, I'm using the following makefile :
CXX = g++
CXXFLAGS= -std=c++11 -w -Wall -g
project: project.o functions.o
$(CXX) $(CXXFLAGS) -o project projet.o functions.o
functions.o: functions.h
clean:
rm -rf *.o
I would like to be able to choose between project.cpp
and projectgraph.cpp
to be compiled using the make
command in the terminal. For example :
make
in the terminal : project.cpp
would be compiled.make graph
in the terminal : projectgraph.cpp
would be compiled.How can I change the makefile to get this result ?
Thanks !
Upvotes: 2
Views: 364
Reputation: 781190
Just add a target for the additional program. Also, functions.cpp
needs to be in the dependency list for functions.o
, and functions.h
should be in the dependency list of project
and projectgraph
.
CXX = g++
CXXFLAGS= -std=c++11 -w -Wall -g
project: project.o functions.o functions.h
$(CXX) $(CXXFLAGS) -o project projet.o functions.o
functions.o: functions.cpp functions.h
projectgraph: projectgraph.o functions.o functions.h
$(CXX) $(CXXFLAGS) -o project projet.o functions.o
clean:
rm -rf *.o
Then you can use
make projectgraph
Upvotes: 3
Reputation: 437
I believe all you need to do is :
graph : project.o functions.o
$(CXX) $(CXXFLAGS) -o projectgraph projectgraph.o functions.o
At least, that is what I have done int he past. There is a whole lot more you can do to make project the default etc but the above should be what you asked for.
Upvotes: 0