mon
mon

Reputation: 691

Understanding roles of CMake, make and GCC

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:

Upvotes: 46

Views: 35667

Answers (1)

usr1234567
usr1234567

Reputation: 23294

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.

Upvotes: 51

Related Questions