[Feelings & Sharing] Science Popularization: What are make, makefile, cmake, and nmake?
Tofloor
poster avatar
yanjuner
Super Moderator
2024-07-04 15:15
Author

Original article: https://bbs.deepin.org/post/260957 Original author:sammy-621

Have you ever been confused by make, cmake, makefile, etc.? Read the following article, and perhaps you'll understand clearly.

Gcc

GCC is the GNU Compiler Collection, which can be simply thought of as a compiler that can compile many programming languages (including C, C++, Objective-C, Fortran, Java, etc.).

When our program has only one source file, we can directly use the gcc command to compile it. But what if our program contains many source files? Compiling them one by one using the gcc command can be confusing and labor-intensive, which is why the make tool was created.

Make

The make tool can be seen as an intelligent batch processing tool. It doesn't have compilation and linking capabilities itself but uses a batch processing approach—by invoking commands specified in a makefile file to compile and link.

Makefile

What is this? Simply put, it's like the sheet music for a song. The make tool is like a conductor, directing the entire orchestra on how to perform based on the sheet music. Similarly, the make tool compiles and links based on the commands in the makefile. The makefile commands include invoking gcc (or other compilers) to compile certain source files.

In simple projects, a makefile can be written manually, but when projects become very large, writing makefiles by hand becomes very cumbersome. Additionally, if you switch platforms, you need to modify the makefile again. This is where the CMake tool comes in.

Cmake

cmake can more easily generate makefile files for the make tool. CMake also has other powerful features, such as generating platform-specific makefiles, so you don't have to modify them yourself.

But what does CMake use to generate makefiles? It uses a file called CMakeLists.txt (also known as a configuration file).

CMakeLists.txt

Who writes the CMakeLists.txt file? You write it yourself.

Nmake

What is nmake? Nmake is a command included in Microsoft Visual Studio, which requires installing VS. It can be seen as equivalent to make on Linux.

Summary of the General Workflow

  1. Use an editor to write source code, such as .c files.
  2. Use a compiler to compile the code into object files, such as .o files.
  3. Use a linker to link the object files into an executable file, such as .exe.

However, if there are too many source files, compiling them one by one is very troublesome. So, people thought, why not design a batch processing program to batch compile source files?

Thus, the make tool was created. It is an automated compilation tool that allows you to fully compile with a single command. But you need to write a rule file that make uses to batch compile, and this file is the makefile. So, writing makefile files is an essential skill for programmers.

For large projects, writing makefiles can be very complex. Therefore, people thought, why not design a tool that reads all the source files and automatically generates makefiles? This led to the creation of the CMake tool, which can output various makefiles or project files, helping programmers reduce their workload. However, this also means writing CMakeLists.txt files, which are the rules that CMake follows. (CMake sets up many libraries, which are not executable files at this point, but make generates the binary executable files.)

Original article: https://baijiahao.baidu.com/s?id=1730515159983772797&wfr=spider&for=pc

Reply Favorite View the author
All Replies

No replies yet