If this question is about a sample Makefile
output of the CMakeList.txt
file then please check the cmake-backend sources and generate one such Makefile
. If it is not then adding to the reply of @Roberto I am trying to make it simple by hiding the details.
CMake function
While Make
is flexible tool for rules and recipe, CMake
is a layer of abstraction that also adds the configuration feature.
My plain CMakeLists.txt
will look like the following,
cmake_minimum_required(VERSION 2.8)
project(example)
file(GLOB testapp_SOURCES *.cc)
add_executable(testapp ${testapp_SOURCES})
Note, that CMake
hides how
the build can be done. We only specified what
is the input and output.
The CMakeLists.txt
contains list of function-calls that are defined by cmake
.
(CMake function) Vs Make rules
In Makefile
the rules and recipes
are used instead of functions
. In addition to function
-like feature, rules and recipes
provide chaining. My minimalistic Makefile
will look like the following,
-include "executable.mk"
TARGETS=testapp.bin
all:${TARGETS}
While the executable.mk
will look like the following,
SOURCES=$(wildcard *.cpp)
OBJECTS=$(SOURCES:.cpp=.o)
DEPS=$(SOURCES:.cpp=.d)
%.bin:$(OBJECTS)
$(CC) $(CFLAGS) -o $@ $^ $(LFLAGS) $(LIBS)
.PHONY: all clean
clean:
$(RM) $(OBJECTS) $(DEPS) $(TARGETS)
-include $(DEPS)
Starting from the scratch I shall start with a Makefile
like the following,
all: testapp.bin
testapp.bin:sourcea.o sourcb.o
$(CC) $(CFLAGS) -o $@ $^ $(LFLAGS) $(LIBS)
.PHONY: all clean
clean:
$(RM) $(OBJECTS) testapp.bin
I got this snippet from here and modified it. Note that some implicit-rules are added to this file which can be found in the makefile-documentation. Some implicit variables are also relevant here.
Note, that Makefile
provides the detail recipe
showing how
the build can be done. It is possible to write executable.mk
to keep the details defined in one file. In that way the makefile can be reduced as I showed earlier.
Internal Variables in CMake
and Make
Now getting little advanced, in CMake
we can set a compiler flag like the following,
set(CMAKE_C_FLAGS "-Wall")
Please find out more about CMake
default variables in CMakeCache.txt
file.
The CMake
code above will be equivalent to Make
code below,
CFLAGS = -Wall
Note that CFLAGS
is an internal variable in Make
, the same way, CMAKE_C_FLAGS
is internal variable in CMake
.
adding include and library path in CMake
We can do it in cmake
using functions.
target_include_directories(testapp PRIVATE "myincludes")
list(APPEND testapp_LIBRARIES
mytest mylibrarypath
)
target_link_libraries(testapp ${testapp_LIBRARIES})
Vs adding include and library path in Make
We can add include and libraries by adding lines like the following,
INCLUDES += -Imyincludes
LIBS += -Lmylibrarypath -lmytest
Note this lines above can be generated from auto-gen tools or pkg-config. (though Makefile is not dependent of auto-config tools)
CMake configure/tweek
Normally it is possible to generate some config.h
file just like auto-config
tools by using configure_file
function. It is possible to do more trick writing custom functions. And finally we can select a config like the following,
cmake --build . --config "Release"
It is possible to add some configurable option using the option
function.
Makefile configure/tweak
If somehow we need to compile it with some debug flag, we can invoke the make
like,
make CXXFLAGS=NDEBUG
I think internal variables, Makefile-rules
and CMake-functions
are good start for the comparison, good luck with more digging.
cmake
I wanted this too. But I doubt you'll find it because the capabilities just don't map that well one to the other. If you try to makecmake
act likemake
, you'll drive yourself nuts, seriously. Best just to start from scratch. Things that are trivial inmake
are quite involved incmake
, and vice-versa. – Ernest Friedman-Hillmake
andcmake
are so distinct that they should be seen more as complementary instead of competing tools? – Ehtesh Choudhurycp *.x $(OUTDIR)
" you'd write in a Makefile. Perhaps the most annoying part for me is that the generated Makefiles are by design completely nonportable and inflexible (continued) – Ernest Friedman-Hill