Chapter 8. C and C++
The issues and techniques shown in Chapter 6 are enhanced and applied in this chapter to C and C++ projects. We’ll continue with the mp3 player example building on our nonrecursive makefile.
Separating Source and Binary
If we want to support a single source tree with multiple platforms and multiple builds per platform, separating the source and binary trees is necessary, so how do we do it? The make program was originally written to work well for files in a single directory. Although it has changed dramatically since then, it hasn’t forgotten its roots. make works with multiple directories best when the files it is updating live in the current directory (or its subdirectories).
The Easy Way
The easiest way to get make to place binaries in
a separate directory from sources is to start the make program from the binary directory. The output files are accessed using
relative paths, as shown in the previous chapter, while the input files must be found
either through explicit paths or through searching through vpath. In either case, we’ll need to refer to the source directory in
several places, so we start with a variable to hold it:
SOURCE_DIR := ../mp3_player
Building on our previous makefile, the
source-to-object function is unchanged, but the
subdirectory function now needs to take into
account the relative path to the source.
# $(call source-to-object, source-file-list) source-to-object = $(subst .c,.o,$(filter %.c,$1)) \ $(subst .y,.o,$(filter %.y,$1)) \ $(subst .l,.o,$(filter ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access