How To Get A Makefile Directory Path

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Home

Things to do before 24
1001 Albums
1001 Movies
ASCII Table
World Beers
Races

timmurphy.org
Software and etc.
stay updated via rss

Categories
Chemistry
Gnuplot
Language
Japanese
LaTeX
LilyPond
Linux
Debian
Fedora
Ubuntu
Mathematics
Optics
Software Development
Apache
Awk
Bash
C
C++
CSS
Docker
Fortran
HTML
Java
KDE
Make
OpenGL
paste
Perl
PHP
Python
R
tr
Vim
SQL
HyperSQL
MySQL
PostgreSQL
Uncategorized
Version Control
CVS
Git
Windows
Windows 7
Windows 8

Popular Posts
LaTeX Bold and Italic Font
LaTeX Horizontal Lines
LaTeX Line Spacing
LaTeX Margins
LaTeX Page Numbering
LaTeX Therefore Symbol

LaTeX Tutorials
My First LaTeX Document
My Second LaTeX Document
My Third LaTeX Document

search this site

Tags

Bash C class code coding color command command line compile database document figure file font function gotcha header highlight image include indent LaTeX
layout linux list math MySQL page PostgreSQL programming Python query script SET sh shell sql string style table terminal text Ubuntu unix vim

Meta
Register
Log in
Entries feed
Comments feed
WordPress.org

How to get a Makefile directory path


Posted: 27th September 2015 by Tim in Linux, Make
Tags: absolute, dir, directory, gnu, make, makefile, path
4

Sometimes in a Makefile you need to reference something from the directory where the Makefile lives. $(PWD) won’t work in this instance, since that will return
the path from where make was called which may be different if, for example, you use the --directory option. In this case, we can use the following code to find
Makefile’s directory:
$(dir $(realpath $(firstword $(MAKEFILE_LIST))))

Let’s pull this apart and look at each component. For this example, our Makefile path is /tmp/path/to/my/Makefile and contains the following:

include other/Makefile
default:
@echo $(PWD)
@echo $(dir $(realpath $(firstword $(MAKEFILE_LIST))))
First, MAKEFILE_LIST will contain all of the Makefiles which have been loaded. If you havent’t included any others, only one will be listed here:
$(MAKEFILE_LIST) = Makefile other/Makefile

Since we’re only interested in the current Makefile, we strip off the includes:
$(firstword $(MAKEFILE_LIST)) = Makefile

Now that we have one file, find the absolute path:


$(realpath $(firstword $(MAKEFILE_LIST))) = /tmp/path/to/my/Makefile

Finally, strip off the file name, leaving only the directory path:
$(dir $(realpath $(firstword $(MAKEFILE_LIST)))) = /tmp/path/to/my/

So, if we call make --directory=/tmp/path/to/my default from the /tmp directory, the following will be printed:
/tmp
/tmp/path/to/my/

If you’re interested in the path of the last included Makefile, use lastword instead of firstword. For example, you may use lastword from other/Makefile to
find that directory.

1. niclas says:
August 9, 2018 at 8:41 am

Thanks, works perfectly!

2. Bilge says:
March 12, 2019 at 10:34 pm

Doesn’t work with spaces in paths.

3. mert says:
March 26, 2019 at 10:42 pm

bilge find me

4. ?????Makefile?????????????? – ??? says:


July 22, 2019 at 4:06 pm
[…] How to get a Makefile directory path « timmurphy.org […]

Name (required)

Mail (will not be published) (required)

Website

Submit Comment

newer posts
older posts

Social Connections

Blog Roll

LaTeX Bold and Italic Font


LaTeX Horizontal Lines
LaTeX Line Spacing
LaTeX Margins
LaTeX Page Numbering
LaTeX Therefore Symbol
My First LaTeX Document
My Second LaTeX Document
My Third LaTeX Document

Recent Comments
Colin: Thank you for postin
Klaus-Peter: Siehe: https://tldp.
Günther Bellen: thanks man!
Smita: In my thesis, the fi
Gilberto: The Solution. Thanks
Anon: How depressing for m
gözde GÜZEL: \displaystyle !! Thi
paul: for christs sake nev
Jaime Garces: I tried the snipet o
Steve W: (less than, greater

Home | Things to do before 24 | 1001 Albums | 1001 Movies | ASCII Table | World Beers | Races | Posts RSS | Comments RSS
© 2010 timmurphy.org. All Rights Reserved. Greyzed Theme created by The Forge Web Creations. Powered by WordPress.

You might also like