Understanding the Make Tool and Makefile in C Programming

Compiled languages require compilation before execution. This is seen as an advantage by some, as it allows for syntax checks and other information verification during the compilation process, helping to avoid basic errors. Additionally, the compiled code can run faster. However, others view this as a disaster. Often, this is not due to any other reason but because the compilation command you need to input may take several lines to complete. Modifying it in the middle can be nearly impossible.Thus, we have a new tool called make.

1. The Make Tool

The make tool was invented to build C programs, especially when there are many library dependencies (particularly non-standard libraries) and when special system environment variables need to be set. Typically, the make tool is used in larger projects. However, using it in smaller programs can simplify the process and make your program builds easier.To use the make tool, you need to write a makefile, which is usually named makefile. Of course, you can use a different name, but then you must specify the makefile name when executing the make command. If you name it makefile, the make program will look for this file in the current directory without needing to specify the filename. Therefore, it is recommended to use the name makefile.

The make tool can actually be applied not only to C language. It is also not limited to Linux; it can be used cross-platform.

So, what is the structure of this makefile?It is actually quite simple.

target:  dependency1,  dependency2, ……        compile command

2. Usage Example

Here we have a program that needs to be built, and during the build process, it will depend on another program. At this point, we can conveniently use the make tool.Filename: test.c

#include <stdio.h>int add(int d, int e);int main(){     int a = 2, b = 3, c;     c = add(a, b);     printf("a add b = %d\n", c);     return 0;}

The above is our main program, which calls the external program add to perform addition. It is important to note that the line int add(int d, int e); at the beginning of the program is a function prototype declaration, as the function itself is written in another file. This function does not know that it exists. To use the add function, you must first inform the main function.Filename: add.c

int add(int x, int y){    return x + y;}

The above is our subroutine that implements addition, which is the called program. It exists in another program file and simply returns the sum.First, we will demonstrate how to compile these two files directly using the command line.

gcc test.c add.c -o test

This may not seem long, but I have encountered situations with dozens of dependency libraries. You can imagine the scene of a full-screen shell window on a 24-inch monitor where one line cannot be completed.So, this is when the make tool comes into play.First, write a makefile, and name it makefile.

test: test.c add.c    gcc test.c add.c -o test

Execution result:

utopia@DESKTOP:~$ makegcc test.c add.c -o testutopia@DESKTOP:~$ makemake: 'test' is up to date.

Running make twice in a row will also reveal new findings; when make is executed again, it checks whether the original code has changed.What happens if a dependency file is missing?If you execute the compilation command directly, you will receive the following prompt.

gcc test.c add.c -o testgcc: error: add.c: No such file or directorymakefile:2: recipe for target 'testbuild' failedmake: *** [testbuild] Error 1

However, using the make file will yield the following prompt.

utopia@DESKTOP:~$ makemake: *** No rule to make target 'add.c', needed by 'test'.  Stop.

The biggest difference between the two is that the make file checks for the provided dependencies before large-scale compilation. If a dependency file is missing, it will stop the compilation process, saving a lot of time and avoiding the awkward situation of finding a missing file halfway through compilation.

3. Summary

As a tool to improve program build efficiency, make is widely used in program compilation. If you use tools that provide source code compilation, you will definitely have used the make tool.The make tool solves the problems of dependency shortages during the build process and the difficulty of maintaining build commands, making compiling programs less painful. After all, writing a makefile once is much more convenient than re-entering long compilation commands every time, not to mention the possibility of setting more complex system variables.To view the complete 【C Language MOOC Tutorial】, please scan the QR code below!Understanding the Make Tool and Makefile in C Programming

Scan the QR code

Click on 【MOOC Tutorial】

Free access to C language tutorials

Leave a Comment