We want to get organised on the command line. We want two directories in our toplevel LearningCpp directory:
bin src
the bin directory has the binaries, the src directory the source files.
In that same directory we want a Makefile.
So to recap, the Makefile is the rules to compile our source programs into binary files.
With this Makefile we can type:
make # which will compile any programs which need compiling - their source code has changed
make clean # delete the binary programs
make myfirstprog # target particular programs to build
bin src
the bin directory has the binaries, the src directory the source files.
In that same directory we want a Makefile.
So to recap, the Makefile is the rules to compile our source programs into binary files.
With this Makefile we can type:
make # which will compile any programs which need compiling - their source code has changed
make clean # delete the binary programs
make myfirstprog # target particular programs to build
In the Makefile we have heavy use of variables so we can have long term flexibility e.g.
CC = c++ # This sets the variable CC to the compiler program c++ and we use it as $(CC)
Now there are lots of fancy aspects to Makefiles so you have to look them up here e.g.
PATHSOURCES = $(wildcard src/*.cpp) # will expand src/*.cpp so provide a full list of all source files
CC = c++ # This sets the variable CC to the compiler program c++ and we use it as $(CC)
Now there are lots of fancy aspects to Makefiles so you have to look them up here e.g.
PATHSOURCES = $(wildcard src/*.cpp) # will expand src/*.cpp so provide a full list of all source files
Our Makefile
# Declaration of variables
# the compiler I have on the MAC which is called clang++ but is effectibely
# g++ (Gnu c++)
CC = c++
FUSSY = -Wall
# Been finding this was needed for code examples so left it in
CC_FLAGS = -Wc++11-extensions
# if we are using maths functions which is common, so left it in
LD_FLAGS = -lm
# keep my .ccp sources files in one place
SRCDIR = src
# put the binaries in another place
BINDIR = bin
# might as well compile all with -g debug option
DEBUG=-g
PATHSOURCES = $(wildcard src/*.cpp)
#substitue src/ for "" from $(PATHSOURCES)
SOURCES = $(subst src/,,$(PATHSOURCES))
#$(var:pattern=replacement)
PATHOBJECTS1 = $(subst src,bin,$(PATHSOURCES))
PATHOBJECTS = $(subst .cpp,,$(PATHOBJECTS1))
OBJECTS= $(SOURCES:.cpp=)
# default is to make every object "make"
all:: $(PATHOBJECTS).
# just to see what $(OBEJECTS) is set to "make objects"
show-objects:
echo $(PATHOBJECTS)
# just to see what $(SOURCES) is set to "make sources"
show-sources:
echo $(PATHSOURCES)
#The rule to make a bin/%
$(BINDIR)/%: $(SRCDIR)/%.cpp
$(CC) $(FUSSY) $(DEBUG) $(CC_FLAGS) $< -o $@ $(LD_FLAGS)
# "make clean" delete all your binaries
clean:
rm -f $(PATHOBJECTS)
%: $(SRCDIR)/%.cpp
$(CC) $(FUSSY) $(DEBUG) $(CC_FLAGS) $< -o $(BINDIR)/$@ $(LD_FLAGS)
# the compiler I have on the MAC which is called clang++ but is effectibely
# g++ (Gnu c++)
CC = c++
FUSSY = -Wall
# Been finding this was needed for code examples so left it in
CC_FLAGS = -Wc++11-extensions
# if we are using maths functions which is common, so left it in
LD_FLAGS = -lm
# keep my .ccp sources files in one place
SRCDIR = src
# put the binaries in another place
BINDIR = bin
# might as well compile all with -g debug option
DEBUG=-g
PATHSOURCES = $(wildcard src/*.cpp)
#substitue src/ for "" from $(PATHSOURCES)
SOURCES = $(subst src/,,$(PATHSOURCES))
#$(var:pattern=replacement)
PATHOBJECTS1 = $(subst src,bin,$(PATHSOURCES))
PATHOBJECTS = $(subst .cpp,,$(PATHOBJECTS1))
OBJECTS= $(SOURCES:.cpp=)
# default is to make every object "make"
all:: $(PATHOBJECTS).
# just to see what $(OBEJECTS) is set to "make objects"
show-objects:
echo $(PATHOBJECTS)
# just to see what $(SOURCES) is set to "make sources"
show-sources:
echo $(PATHSOURCES)
#The rule to make a bin/%
$(BINDIR)/%: $(SRCDIR)/%.cpp
$(CC) $(FUSSY) $(DEBUG) $(CC_FLAGS) $< -o $@ $(LD_FLAGS)
# "make clean" delete all your binaries
clean:
rm -f $(PATHOBJECTS)
%: $(SRCDIR)/%.cpp
$(CC) $(FUSSY) $(DEBUG) $(CC_FLAGS) $< -o $(BINDIR)/$@ $(LD_FLAGS)
With <ompose><b> now using Eclipse, that effectively runs "make all" and so if you have edited a program then this correctly detects the change. (You will need to hit the save icon first.)
Or, if you are working on the command line (the terminal shell) you can use vim to edit your source program and then type make to compile the program. Two ways of doing the same thing.