Programmers have been using CMake and Make for a long time now. When you join a big company or start working on a project with a large codebase, there are all these builds that you need to take care of. You must have seen those “CMakeLists.txt” files floating around. You are supposed to run “cmake” and “make” commands on the terminal. A lot of people just follow the instructions blindly, not really caring about why we need to do things in a certain way. What is this whole build process and why is it structured this way? What are the differences between CMake and Make? Does it matter? Are they interchangeable?
As it turns out, they are quite different. It is important to understand the differences between them to make sure you don’t get yourself in trouble. Before getting into the differences, let’s first see what they are.
The way in which we design a software system is that we first write code, then the compiler compiles it and creates executable files. These executable files are the ones that carry out the actual task. “Make” is a tool that controls the generation of executables and other non-source files of a program from the program’s source files.
The “Make” tool needs to know how to build your program. It gets its knowledge of how to build your program from a file called the “makefile”. This makefile lists each of the non-source files and how to compute it from other files. When you write a program, you should write a makefile for it, so that it is possible to use “Make” to build and install the program. Simple stuff! If you didn’t understand it, go back and read the paragraph again because it’s important for the next part.
Why do we need “Make”?
The reason we need “Make” is because it enables the end user to build and install your package without knowing the details of how it’s done. Every project comes with its own rules and nuances, and it can get quite painful every time you have a new collaborator. That’s the reason we have this makefile. The details of the build process are actually recorded in the makefile that you supply. “Make” figures out automatically which files it needs to update, based on which source files have changed. It also automatically determines the proper order for updating the files, in case one non-source file depends on another non-source file.
Recompiling the entire program every time we change a small part of the system would be inefficient. Hence, if you change a few source files and then run “Make”, it doesn’t recompile the whole thing. It updates only those non-source files that depend directly or indirectly on the source files that you changed. Pretty neat! “Make” is not limited to any particular language. For each non-source file in the program, the makefile specifies the shell commands to compute it. These shell commands can run a compiler to produce an object file, the linker to produce an executable, ar to update a library, Makeinfo to format documentation, etc. “Make” is not limited to just building a package either. You can also use “Make” to control installing or uninstalling a package, generate tags tables for it, or anything else you want to do often enough to make it worth while writing down how to do it.
CMake stands for Cross-platform Make. CMake recognizes which compilers to use for a given kind of source. In case you didn’t know, you can’t use the same compiler to build all the different kinds of sources. You can do this manually every time you want to build your project, but it would be tedious and painful. CMake invokes the right sequence of commands for each type of target. Therefore, there is no explicit specification of commands like $(CC).
For coding junkies who really want the gory details, read on. If you are not into all that, you can skip to the next section. All the usual compiler/linker flags dealing with the inclusion of header files, libraries, etc are replaced by platform independent and build system independent commands. Debugging flags are included by either setting the variable CMAKE_BUILD_TYPE to “Debug”, or by passing it to CMake when invoking the program:
CMake also offers the platform independent inclusion of the ‘-fPIC’ flag (via the POSITION_INDEPENDENT_CODE property) and many others. Still, more obscure settings can be implemented by hand in CMake just as well as in a Makefile (by using COMPILE_FLAGS and similar properties). Of course CMake really starts to shine when third party libraries (like OpenGL) are included in a portable manner.
What is the difference?
The build process has one step if you use a Makefile, namely typing “make” at the command line. For CMake, there are two steps: First, you need to setup your build environment (either by typing cmake <source_dir> in your build directory or by running some GUI client). This creates a makefile or something equivalent, depending on the build system of your choice (e.g. Make on *nix, VC++ or MinGW on Windows, etc). The build system can be passed to CMake as a parameter. However, CMake makes reasonable default choices depending on your system configuration. Second, you perform the actual build in the selected build system.
We are going to jump into the GNU build system territory here. If you are not familiar with that, this paragraph might look like jibber-jabber to you. Alright, now that I have given the statutory warning, let’s move on! We can compare CMake with Autotools. When we do that, we can see the shortcomings of Make, and they form the reason for the creation of Autotools. We can also see the obvious advantages of CMake over Make. Autoconf solves an important problem i.e. reliable discovery of system-specific build and runtime information. But this is only a small part in the development of portable software. To this end, the GNU project has developed a suite of integrated utilities to finish the job Autoconf started: the GNU build system, whose most important components are Autoconf, Automake, and Libtool.
“Make” can’t do that, at least not without modifying it anyway! You can make it do all that stuff but it would take a lot of time maintaining it across platforms. CMake solves the same problem, but at the same time, it has a few advantages over the GNU Build System:
- The language used to write CMakeLists.txt files is readable and easier to understand.
- It doesn’t only rely on “Make” to build the project.
- It supports multiple generators like Xcode, Eclipse, Visual Studio, etc.
When comparing CMake with Make, there are several advantages of using CMake:
- Cross platform discovery of system libraries.
- Automatic discovery and configuration of the toolchain.
- Easier to compile your files into a shared library in a platform agnostic way, and in general easier to use than make.
CMake does more than just “make”, so it can be more complex. In the long run, it’s better to learn how to use it. If you have just a small project on only one platform, then maybe “Make” can do a better job.
40 thoughts on “CMake vs Make”
Thank you for sorting this out. Very helpful.
Thanks! Glad to hear that it’s helpful.
Thanks a bunch for the precise and simple overview. Clarifies a lot of points for Make/CMake noobs like me 🙂
Thanks! Glad to hear that you found it useful 🙂
Very precise information 🙂
Thanks, it’s helpful for me
Thank you, this was helpful 🙂
Glad to hear it 🙂
Extremely helpful. Thanks a lot!
Thanks! Glad to hear it.
Thanks, very helpful!
To make sure I understand, does it mean that whereas you need to specify a compiler when using make (via makefile), the same will be automatic for cmake as it automatically recognises the right one to use
Gracias, This helped a bunch.
Thanks! Good to know that it was helpful.
Thank a lot! That was very illustrative.
Thanks Ibra! Glad you found it useful.
Very helpful, simple and clear!! Thanks!!
Good job summarizing the role of cmake. Very coherent article!
One question on what you said, how does cmake work in Eclipse? Is that CDT? I have no information that cmake can be used in Eclipse. All I have is trying to import cmake into an Eclipse CDT project and there are many open problems. If you have more/better info, please share.
“CMake solves the same problem, but at the same time, it has a few advantages over the GNU Build System:
The language used to write CMakeLists.txt files is readable and easier to understand.
It doesn’t only rely on “Make” to build the project.
It supports multiple generators like Xcode, Eclipse, Visual Studio, etc.”
Helpful 🙂 thanks
thank you very informative
Thanks. Glad you liked it.
Thanks, it was very helpful.
Thanks! Really comprehensive explanation!
thanks.It’s very clearly! 🙂
Thanks for the awesome clarity.
But i have a question, in some build both cmake and make are used. Thought they both do the same thing but differently.
Great! There’s a make-like build utility based on Lua. http://xmake.io/#/
$ xmake run
Thanks!, It’s really well explained and very interesting for all.
Thanks, it was very helpful
Muy buena explicación, muchas gracias por la aportación