Makefile to put object files from source files different directories into a single, separate directory?

There’s more than one way to do it, but this is a pretty good one (I really should have that hotkeyed). vpath %.cpp ../src src = Foo.cpp Bar.cpp test_src = Main.cpp FooTest.cpp BarTest.cpp objects = $(patsubst %.cpp,obj/%.o,$(src)) test_objects = $(patsubst %.cpp,obj/%.o,$(test_src)) $(objects): | obj obj: @mkdir -p $@ obj/%.o : %.cpp @echo $< @$(CXX) $(CXXFLAGS) … Read more

How can I configure my makefile for debug and release builds?

You can use Target-specific Variable Values. Example: CXXFLAGS = -g3 -gdwarf2 CCFLAGS = -g3 -gdwarf2 all: executable debug: CXXFLAGS += -DDEBUG -g debug: CCFLAGS += -DDEBUG -g debug: executable executable: CommandParser.tab.o CommandParser.yy.o Command.o $(CXX) -o output CommandParser.yy.o CommandParser.tab.o Command.o -lfl CommandParser.yy.o: CommandParser.l flex -o CommandParser.yy.c CommandParser.l $(CC) -c CommandParser.yy.c Remember to use $(CXX) or $(CC) … Read more

How to get current relative directory of your Makefile?

The shell function. You can use shell function: current_dir = $(shell pwd). Or shell in combination with notdir, if you need not absolute path: current_dir = $(notdir $(shell pwd)). Update. Given solution only works when you are running make from the Makefile’s current directory. As @Flimm noted: Note that this returns the current working directory, … Read more

How to speed up Compile Time of my CMake enabled C++ Project?

Here’s what I had good results with using CMake and Visual Studio or GNU toolchains: Exchange GNU make with Ninja. It’s faster, makes use of all available CPU cores automatically and has a good dependency management. Just be aware of a.) You need to setup the target dependencies in CMake correctly. If you get to … Read more

Building C-program “out of source tree” with GNU make

Here’s the Makefile I’ve added to the documentation (currently in review so I’ll post it here) : # Set project directory one level above the Makefile directory. $(CURDIR) is a GNU make variable containing the path to the current working directory PROJDIR := $(realpath $(CURDIR)/..) SOURCEDIR := $(PROJDIR)/Sources BUILDDIR := $(PROJDIR)/Build # Name of the … Read more