An Overview of Makefile, Make, and CMake

If you are developing under Linux, you must know what a Makefile is. If you don’t know, you could say you are not a qualified Linux developer because a Makefile is an essential skill.

So, what exactly is the function of a Makefile?

First, you should know about gcc. GCC (GNU Compiler Collection) is a programming language compiler developed by GNU. When using the gcc command to compile, you may encounter some troubles:

  1. For C language, when using gcc to compile, it only automatically links some basic C standard libraries (like libc.a or libc.so). Many dependency libraries (like non-standard libraries, third-party libraries, etc.) need to be linked manually by adding the libraries to the gcc command. Below are some troubles that require manual linking:

    1) If you use the math library, even if you include the standard header file <math.h>, you will encounter undefined errors during compilation if you do not link it manually:

    #include <stdio.h>
    #include <math.h>
    #define PI 3.14159265
    int main(){
        double angle, result;
        angle = 30.0;
        result = sin (angle * PI / 180.0);
        printf ("result = %f \n", result);
        return 0;
    }

    If you don’t link the library manually, you will get a compilation error, but after linking manually, there will be no error:

    An Overview of Makefile, Make, and CMake

    The filename of the math library is libm.a. GCC automatically adds the prefix ‘lib’ and the suffix ‘.a’ based on the basic name after ‘-l’. For example, gcc test.c -o test.out -lm, where ‘m’ is the base name, resulting in the math library libm.a.

    2) When you use threads, you need to manually add -lpthread, otherwise, it will report an error, but once added, it compiles successfully.

    An Overview of Makefile, Make, and CMake

    3) There are actually many more libraries that need to be added manually…

  2. When your program has only one source file, you can compile directly using the gcc command. But what if you have many source files? Do you type each file one by one in the gcc command? If you have 100 source files, would you type them all? Not only are there many source files, but each file may also depend on different libraries, making the command very long, which is clearly not a feasible method.

  3. When developing a project, even a small debug change like modifying an if condition requires recompiling everything. In a project with hundreds or thousands of source files, compiling all files can take a lot of time. Spending so much time just to fix a small bug is clearly inefficient.

  4. While developing, we often encounter many problems, such as files being in different directories, leading to different paths. There are many other common issues that I won’t list here.

With these headaches from the above issues, the Makefile comes into play. In a Makefile, you can set the compilation rules you want, specify which files to compile, which files do not need to be compiled, etc., and it supports multithreaded concurrent operations, which can reduce compilation time.

However, there is another tool called make, which is used to execute the Makefile. You can think of make as a musician, while the Makefile is a piece of sheet music. The musician plays according to the content of the sheet music; make compiles and links based on the contents written in the Makefile. Make is more like a batch processing tool that can batch process source files. By executing a single make command, you can achieve automatic compilation.

An Overview of Makefile, Make, and CMake

When compiling an entire project, make will only compile the files that have been modified; files that have not been modified will not be recompiled. This way, after debugging a small bug, you do not need to spend a lot of compilation time. As long as no files are added or deleted, the content of the Makefile does not need to be modified. Therefore, using make + Makefile greatly improves our work efficiency.

For smaller projects, Makefiles can be written manually, but for very large projects, writing a Makefile by hand can also be a hassle, and Makefiles are not universal; if you switch to another platform, the Makefile must be rewritten.

Thus, some people wondered if we could automatically generate a Makefile. We just need to read all the source files, which led to the emergence of another tool, CMake, a cross-platform project management tool that can generate Makefile files for make to execute, eliminating the need to modify when switching platforms.

An Overview of Makefile, Make, and CMake

CMake still deals with abstract concepts like targets and dependencies. Under Linux, it generates Makefiles for Linux, and under Windows, if using Visual Studio, it generates project files for Visual Studio. It customizes project files for various compilers, which is both abstract and user-friendly.

At this point, a question arises: how does CMake generate the Makefile?

Actually, CMake generates the Makefile based on a file called CMakeLists.txt. Just as make is used to execute Makefiles, CMake is used to execute CMakeLists.txt. So, who generates the CMakeLists.txt? Haha! CMakeLists.txt is written by hand.

Recently, I saw a saying that I thought was very good: “In the world of programming, there are no shortcuts; you still have to be down-to-earth.”

We can only find ways to make our usage more convenient step by step, continuously improving. Society progresses bit by bit, doesn’t it? Our lives are now more convenient and faster, ultimately relying on human creativity. The same goes for CMakeLists.txt; it also needs to be written by us, but writing CMakeLists.txt is more convenient than writing a Makefile, which is progress!

END
Author: Linux_Daily
Source: Mixed Talk Linux
Copyright belongs to the original author. If there is any infringement, please contact for deletion.
Recommended Reading
Because the code is too little, it is considered that the algorithm is not good…
God’s explanation: The four major communication interfaces UART, I2C, SPI, and 1-wire
Why are Chinese numbers four-digit entry and Western numbers three-digit entry?
→ Follow to avoid getting lost ←

Leave a Comment