GNU make, get relative PATH for i.e recursive builds with dependencies based on local git copies

In case you’re building yourself everything from source in your project, there is a good chance you once had a problem because you were using relative (like ../myressource) paths in your CFLAGS or LD_FLAGS definition and passed them down to the build system.

Short answer: when getting down in sub directories to i.e build your dependencies, the relative path can’t stay good. Only the absolute path can work.

One solution, if using GNU make, is to use something like this to compute the absolute path:

RES_RELATIVE_PATH=./path_to_my_ressource
RES_ABSOLUTE_PATH=$(realpath $(RES_RELATIVE_PATH))
CFLAGS+= -I$(RES_ABSOLUTE_PATH)

# Note: the naming is up to you. In my case, for something like cJSON, I'm using the following naming:

CJSON_RELPATH=./external/cJSON
CJSON_PATH=$(realpath $(CJSON_RELPATH))
CFLAGS+= -I$(CJSON_PATH)

You can also directly initialize with a shell call:

CJSON_CFLAGS=-I$(shell realpath "./external/cJSON")
CFLAGS+= $(CJSON_FLAGS)

#code #makefile #gnu #path #absolute #relative