Reputation: 6563
Let's say I have a project where I've already run CMake in .build directory:
project/
.build/
.src
Presently I must do this to run the build:
cd .build
make
I'd like to be able to run make from the root of my project, perhaps with
make -f ./build/Makefile
but it doesn't work. I get errors like this
make[1]: CMakeFiles/Makefile2: No such file or directory
make[1]: *** No rule to make target `CMakeFiles/Makefile2'. Stop.
make: *** [all] Error 2
This is because the CMake-generated Makefile assumes its working directory is the same as where it resides (.build).
Is it possible to have CMake generate a makefile such that the makefile changes the working directory to where it resides?
Upvotes: 3
Views: 4353
Reputation: 9498
Another approach would be to create another Makefile in the directory you want to run make from, with contents something like:
all:
cd ./build; make $(MFLAGS)
Then you could just type "make" from the top level directory.
I don't think there is a way to get cmake to generate a Makefile that can be run from any location. That's just not how make is ordinarily used.
But you might be able to coax cmake into creating an additional helper makefile in your project root directory, with some cmake like the following in CMakeLists.txt:
configure_file(ProjectRootMakefile.in ${CMAKE_SOURCE_DIR}/Makefile @ONLY)
(If needed, replace that "${CMAKE_SOURCE_DIR}" with an expression that resolves to your project root directory)
And you need to create the file ProjectRootMakefile.in in your source tree, with contents like:
all:
cd @CMAKE_BINARY_DIR@; make $(MFLAGS)
which will be used as the template for your project root Makefile. (Ensure that you use a tab character, not spaces, before the "cd")
I haven't tested these suggestions, but there should be enough here to give you the general idea.
Upvotes: 0
Reputation: 8851
You just need to tell make where is your base directory, no need to change cmake.
make -C your_build_directory
Upvotes: 8