Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • 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

like image 892
mon Avatar asked Sep 29 '16 05:09

mon


People also ask

What is the difference between CMake and gcc?

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.

Does CMake use gcc?

Usually under Linux, one uses CMake to generate a GNU make file which then uses gcc or g++ to compile the source file and to create the executable.

What is the difference between make and CMake?

Make (or rather a Makefile) is a buildsystem - it drives the compiler and other build tools to build your code. CMake is a generator of buildsystems. It can produce Makefiles, it can produce Ninja build files, it can produce KDEvelop or Xcode projects, it can produce Visual Studio solutions.

What is the role of CMake?

CMake is a meta build system that uses scripts called CMakeLists to generate build files for a specific environment (for example, makefiles on Unix machines). When you create a new CMake project in CLion, a CMakeLists. txt file is automatically generated under the project root.


1 Answers

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.
like image 176
usr1234567 Avatar answered Sep 17 '22 16:09

usr1234567