Problem Description
- Recently, I wrote a Makefile for compiling the device tree. When it was used
make -f xx/Makefile
, the execution path changed, which caused some operation paths in the Makefile to be incorrect and unable to be executed.
get the current directory
-
Make built-in
$(CURDIR)
, can print out the full path of the current path -
Note: If you execute the Makefile under other paths, the
$(CURDIR)
printed path is the execution path, not the path where the Makefile is located -
test code
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
cur_makefile_path := $(dir $(mkfile_path))
DIR_ROOT := $(cur_makefile_path)
CUR_ROOT := $(CURDIR)
All:
echo $(DIR_ROOT)
echo $(CUR_ROOT)
- Compared with the path of Makefile,
$(CURDIR)
the path of execution is printed
Get the path of the Makefile
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
cur_makefile_path := $(dir $(mkfile_path))
The $(cur_makefile_path) here will not be confused because it is executed under different paths. The verification is as follows
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
cur_makefile_path := $(dir $(mkfile_path))
DIR_ROOT := $(cur_makefile_path)
All:
echo $(DIR_ROOT)
-
Test results:
-
Note that there is a Makefile in the current path, either directly
make
ormake -f Makefile
-
Execute the Makefile under other paths, you can use it
$ make -f make_demo/make_01/Makefile
, that is,$ make -f Makefile路径
add the path of Makefile after it
execution path
That is, the path to enter the make command, which can be $(CURDIR)
printed out using
relative path
-
It is best to get the path where the Makefile is located here, and then splice the path where the Makefile is located
-
Example: Execute the dtc/dtc tool in the current path of the Makefile
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
cur_makefile_path := $(dir $(mkfile_path))
DIR_ROOT := $(cur_makefile_path)
HOST_DTC := $(DIR_ROOT)dtc/dtc
- The above
DIR_ROOT
cannot be implemented using$(CURDIR)
or.
, otherwise the execution path is different, and it cannot be executed correctly
summary
- Pay attention to the path of the Makefile. Generally, the absolute path where the Makefile is located is required, not the current path. If you execute other directories, it is best to obtain the absolute path of the Makefile first, and then splice the relative path