yelsayed
yelsayed

Reputation: 5542

Determine library path programmatically in Makefile

I'm new to makefile, and I'm writing a simple C++ shared library.

Is there a way of finding a library's path dynamically by the makefile itself? What I want is something like this: (in my makefile)

INCLUDE_DIRS := `which amplex-gui`
LIBRARY_DIRS := `which amplex-gui`

amplex-gui is a library I use in my code, and I need to put its lib and include directories in my makefile. I want to figure out its path dynamically because each user might install it in a different path on their machine. Therefore, I need my makefile to dynamically parse the which command (or perhaps the $PATH environment variable) to find that path. How can I go about doing this?

Upvotes: 0

Views: 310

Answers (1)

MadScientist
MadScientist

Reputation: 101131

Remember that backquoting is shell syntax. Make doesn't do anything special with backquotes. If you're using GNU make, you can use $(shell which amplex-gui) to get equivalent behavior as backquotes.

Regarding your comment above, I'm not sure exactly what you mean by "nest commands", but you can definitely use the shell's $() syntax within a make shell function. However, as with all strings that make expands, you need to double the dollar signs to quote them so that they are passed to the shell. So for example:

INCLUDE_DIRS := $(shell echo $$(dirname $$(dirname $$(which amplex-gui))))

Of course you can also use make functions; unfortunately the make dir function is annoying in that it leaves the final slash, so it cannot be used multiple times directly. You have to put a patsubst in there, like:

INCLUDE_DIRS := $(dir $(patsubst %/,%,$(dir $(shell which amplex-gui))))

Finally, if you have a sufficiently new version of GNU make there's the abspath function, so you could do something like this:

INCLUDE_DIRS := $(abspath $(dir $(shell which amplex-gui))../..)

Upvotes: 1

Related Questions