41

1. cmake is a command from CMake software: preparation for build automation system; make and make install are commands from Make software: build automation system.

2. From reading this post, what I understand is that:

a. This "cmake and make" stuffs actually use g++ / gcc in its implementation. cmake and make stuffs are basically just tools in using g++ / gcc. Is that correct?

b. gcc / g++ are the compiler that do the actual work.

c. So I can just use gcc / g++ directly without using the make and CMake things?

3. According to this stackoverflow answer: CMake takes a CMakeList.txt file, and outputs it to a platform-specific build format, e.g., a Makefile, Visual Studio, etc.

However when I came across this openCV installation :

mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..

It executes cmake command in a directory where there is no CMakeLists.txt file. Can you explain and elaborate on this?

4. The usual steps that I've seen are: cmake, make, sudo make install. I read this stackoverflow post, what I understand:

(i) make is for building the project.

(ii) make install is to copy the binary / executables to the installed directories.

a. So when we make, where are the result / binary files / executables stored at?

b. If we only run make without make install, does it mean that the files are not generated?

c. I came across this openCV tutorial on using openCV with GCC and CMake. It uses:

cd <DisplayImage_directory>
cmake .
make

Why doesn't it do make install as well?

5. In summary:

  • CMake takes CMakeList.txt file (which is cross platform) to generate a Makefile (which is specific to a platform).

  • I can just write Makefile manually and skip the CMake step. but it is better to do with the CMake step because it is cross platform, otherwise I have to rewrite the Makefile again if I change platform.

  • Make takes Makefile (which is generated by CMake or written manually) as a guide to compile and build. Make basically uses gcc / g++ or other compiler in its work. Make itself is just a tool for the compiler.

  • Make install put the result / executables into the install path

Community
  • 1
  • 1
mon
  • 641
  • 1
  • 7
  • 14

1 Answers1

43

CMake generates files for other build systems. These can be Makefiles, Ninja files or projects files for IDEs like Visual Studio or Eclipse. The build files contain calls to compilers like GCC, Clang, or cl.exe. If you have several compilers installed, you can choose one.
All three parts are independent. The compiler, the build system and CMake.

It is easier to understand when you have the history. People used their compiler. Over time they added so many flags, that it was cumbersome to type them every time. So they put the calls in a script. From that the build systems (Make, Ninja) evolved.
The people wanted to support multiple platforms, compilers, scenarios and so on and the build system files became hard to maintain and their use was error-prone. That's the reason people invented meta build system that creates the files for the actual build system. Examples are Autotools or CMake.

  1. Yes
  2. CMake does not use your compiler, make does not implement it, but it calls (uses) the compiler.
  3. The CMakeLists.txt file should be in the parent directory of release. The last argument of the CMake call indicates the path where the CMakeLists.txt file is located.
  4. Right, make generates the file in the build directory. In your example from 3. release is the build directory. You can find all the generated files and use them. Installing is optional, especially if you want to develop the software, you are not installing it.
  5. Try writing Makefiles for a large project and you will see how much work it is. But yes, everything in 5 is right.
Livne Rosenblum
  • 196
  • 1
  • 12
usr1234567
  • 21,601
  • 16
  • 108
  • 128
  • Thanks for your answer. I want to clarify point 3 again. So the last argument is the location of CMakeLists.txt, in this case the last argument is .., that's why the CMakeLists.txt is in the parent dir... If I want to point to my current directory I use one dot . , is it? – mon Sep 29 '16 at 07:54
  • 6
    @mon: Yes, but that would be a in-source build which is discouraged. Out-of-source build are better, you can just delete the complete folder and have a fresh start. And your source directories are not cluttered with generated files. – usr1234567 Sep 29 '16 at 08:07
  • 1
    Thanks. Is there a good book where one can read more about this? – Asker Oct 11 '20 at 17:00