GNU Make path handling


are commonly used in conjunction with GNU Make's automatic macros (like $@) GNU Make provides a modifier syntax.  Appending D or F to any automatic macro is equivalent to calling $(dir) or $(notdir) on it.  For example, $(@D) is equivalent to $(dir $@) and $(@F) is the same as $(notdir $@).

GNU Make 3.81 added two very useful functions: abspath and realpath.

realpath is a GNU Make wrapper for the C library realpath function which removes ./, resolves ../, removes duplicated / and follows symlinks.  The argument to realpath must exist in the file system.   The path returned by realpath is absolute.  If the path does not exist then the function returns an empty string.

Note that you cannot use realpath as a file exists function because the final part of the path does not have to exist; to do file exists used $(wildcard FILE).  For example, the full path of the current directory could be found like this:

         current := $(realpath ./)

abspath is similar but it does not follow symlinks and does not have to refer to an existing file or directory.

About the author

John Graham-Cumming's picture John Graham-Cumming

John Graham-Cumming is Co-Founder at Electric Cloud, Inc . Prior to joining Electric Cloud, John was a Venture Consultant with Accel Partners, VP of Internet Technology at Interwoven, Inc. (IWOV), VP of Engineering at Scriptics Corporation (acquired by Interwoven), and Chief Architect at Optimal Networks, Inc. John holds BA and MA degrees in Mathematics and Computation and a Doctorate in Computer Security from Oxford University. John is the creator of the highly acclaimed open source POPFile project. He also holds two patents in network analysis and has others pending.

AgileConnection is one of the growing communities of the TechWell network.

Featuring fresh, insightful stories, is the place to go for what is happening in software development and delivery.  Join the conversation now!

Upcoming Events

Nov 09
Nov 09
Apr 13
May 03