The `cmake --build` does not use the `Makefile` it generates
It appears that after you generate a file with cmake -S .
on Linux, the resulting Makefile
is not explicitly referenced by cmake --build .
. Instead CMake relies on whatever make
it calls to figure out what file to use. Given that several make
-flavors accept more than one possible file name (especially relevant on systems which are handling names case-sensitive), I don't think this can be considered correct behavior.
The effect is this:
$ cmake -S .
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc - works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ - works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/hello
$ cmake --build .
g++ hello.cpp -o hello
Hello from GNUmakefile
With the attached (MWE) "project": mwe.tgz you can reproduce this easily.
When using features specific to a particular make
flavor, I try to make sure only that make
flavor will pick it up. And that's the case with GNU make. The MWE project contains the means to build with either GNU make or CMake, but the latter will pick up the GNUmakefile
when executing make
, even though that's not the file it generated.
I understand that many of the GNU make specific features used by CMake these days have been picked up (if only for compatibility) by other make flavors (.NOTPARALLEL
, .PHONY
...) such as NetBSD's make. However, the latter doesn't accept the names GNUmakefile
, makefile
and Makefile
in this order of precedence.
Long story short: if you insist on generating a Makefile
with this particular file name (in the past I had looked for ways to override it, but hadn't found any nice and documente way), the subsequent cmake --build .
should be invoking make -f Makefile
, no matter the make
flavor. As far as I can tell the -f
switch is very portable for the Unix flavors of make
.
If you rename the above GNUmakefile
to makefile
it will still get picked up before the Makefile
generated by CMake (provided case-sensitive file name handling).