Click the "C Language and CPP Programming" above and select "Follow/Top/Star the Public Account"
Useful resources delivered to you first!
Recently, some friends mentioned they did not receive the daily article push. This is because WeChat has changed its push mechanism, and some friends are unable to see the articles for the day. Missing out on useful knowledge and information is unfortunate, so I recommend marking it⭐️ so you can receive the push immediately.
Introduction
For development on Windows, many IDEs come with integrated compilers, such as Visual Studio, providing a “one-click compile” feature. After coding, you only need one action to complete compilation, linking, and generating the target file.
Linux development is different from Windows; typically, the gcc/g++ compiler is used in Linux. If developing Linux programs for ARM, you will also need to use the arm-linux-gcc/arm-linux-g++ cross-compiler.
Linux can also achieve a “one-click compile” feature, which requires a compilation script called a “Makefile.” Makefiles can be written manually or generated using automated build tools (like scons, CMake). Manually writing a Makefile is one of the differences between Linux and Windows programmers. Generally, a universal Makefile can suit most Linux project programs.
3 Makefile Templates
2.1 Makefile for Compiling Executable Files
VERSION =1.00
CC =gcc
DEBUG =-DUSE_DEBUG
CFLAGS =-Wall
SOURCES =$(wildcard ./source/*.c)
INCLUDES =-I./include
LIB_NAMES =-lfun_a -lfun_so
LIB_PATH =-L./lib
OBJ =$(patsubst %.c, %.o, $(SOURCES))
TARGET =app
#links
$(TARGET):$(OBJ)
@mkdir -p output
$(CC) $(OBJ) $(LIB_PATH) $(LIB_NAMES) -o output/$(TARGET)$(VERSION)
@rm -rf $(OBJ)
#compile
%.o: %.c
$(CC) $(INCLUDES) $(DEBUG) -c $(CFLAGS) $< -o $@
.PHONY:clean
clean:
@echo "Remove linked and compiled files......"
rm -rf $(OBJ) $(TARGET) output
[Key Points]
[1] Program Version
During the development and debugging process, multiple program versions may be generated. You can add a version number identifier after (or before) the target file.
VERSION = 1.00
$(CC) $(OBJ) $(LIB_PATH) $(LIB_NAMES) -o output/$(TARGET)$(VERSION)
[2] Compiler Selection
On Linux, it is gcc/g++; for ARM, it is arm-linux-gcc; the names of customized cross-compilers provided by different CPU manufacturers may vary, such as Hisilicon’s “arm-hisiv300-linux-gcc.”.
CC = gcc
[3] Macro Definitions
In the development process, special code generally adds macro conditions to choose whether to compile, such as debugging print output code. -D is the identifier, followed by the “macro.”.
DEBUG =-DUSE_DEBUG
[4] Compilation Options
You can specify compilation conditions, such as displaying warnings (-Wall), optimization levels (-O).
CFLAGS =-Wall -O
[5] Source Files
Specify the source file destination path, using “wildcard” to get all dependent source files in the path.
SOURCES =$(wildcard ./source/*.c)
[6] Header Files
Include dependent header files, including header files for source files and library files.
INCLUDES =-I./include
[7] Library File Names
Specify the names of library files. Library files have fixed formats; static libraries are libxxx.a; dynamic libraries are libxxx.so. To specify the library file name, you only need to write the “xxx” part.
LIB_NAMES =-lfun_a -lfun_so
[8] Library File Paths
Specify the storage path for dependent library files. Note that if a dynamic library is referenced, it may be copied to the “/lib” or “/usr/lib” directory. When executing the application, the system defaults to indexing dynamic libraries from this file.
LIB_PATH =-L./lib
[9] Target Files
Use “patsubst” to compile source files (.c) into target files (.o).
OBJ =$(patsubst %.c, %.o, $(SOURCES))
[10] Executable File
Name of the executable file.
TARGET =app
[11] Compilation
%.o: %.c
$(CC) $(INCLUDES) $(DEBUG) $(CFLAGS) $< -o $@
[12] Linking
You can create an “output” folder to store the target executable file. After linking, the target executable file can be deleted along with the temporary files (.o) generated during compilation.
$(TARGET):$(OBJ)
@mkdir -p output
$(CC) $(OBJ) $(LIB_PATH) $(LIB_NAMES) -o output/$(TARGET)$(VERSION)
@rm -rf $(OBJ)
[13] Clean Compilation Information
Execute “make clean” to clear temporary files generated during compilation.
.PHONY:clean
clean:
@echo "Remove linked and compiled files......"
rm -rf $(OBJ) $(TARGET) output
2.2 Makefile for Compiling Static Libraries
VERSION =
CC =gcc
DEBUG =
CFLAGS =-Wall
AR =ar
ARFLAGS =rv
SOURCES =$(wildcard *.c)
INCLUDES =-I.
LIB_NAMES =
LIB_PATH =
OBJ =$(patsubst %.c, %.o, $(SOURCES))
TARGET =libfun_a
#link
$(TARGET):$(OBJ)
@mkdir -p output
$(AR) $(ARFLAGS) output/$(TARGET)$(VERSION).a $(OBJ)
@rm -rf $(OBJ)
#compile
%.o: %.c
$(CC) $(INCLUDES) $(DEBUG) -c $(CFLAGS) $< -o $@
.PHONY:clean
clean:
@echo "Remove linked and compiled files......"
rm -rf $(OBJ) $(TARGET) output
[Key Points]
The basic format is consistent with the “Makefile for Compiling Executable Files,” with the following differences.
[1] Use the “ar” command to link target files (.o) into a static library file (.a). The naming format for static library files is fixed: libxxx.a.
2.3 Makefile for Compiling Dynamic Libraries
VERSION =
CC =gcc
DEBUG =
CFLAGS =-fPIC -shared
LFLAGS =-fPIC -shared
SOURCES =$(wildcard *.c)
INCLUDES =-I.
LIB_NAMES =
LIB_PATH =
OBJ =$(patsubst %.c, %.o, $(SOURCES))
TARGET =libfun_so
#link
$(TARGET):$(OBJ)
@mkdir -p output
$(CC) $(OBJ) $(LIB_PATH) $(LIB_NAMES) $(LFLAGS) -o output/$(TARGET)$(VERSION).so
@rm -rf $(OBJ)
#compile
%.o: %.c
$(CC) $(INCLUDES) $(DEBUG) -c $(CFLAGS) $< -o $@
.PHONY:clean
clean:
@echo "Remove linked and compiled files......"
rm -rf $(OBJ) $(TARGET) output
[Key Points]
The basic format is consistent with the “Makefile for Compiling Executable Files,” with the following differences.
[1] Compilation and linking options include the “-fPIC -shared” options. The naming format for dynamic library files is fixed: libxxx.so.
Demo
3.1 Compiling the Application
Write a test routine, with the file storage directory structure as follows. Header files are stored in the “include” directory, library files in the “lib” directory, source files in the “source” directory, and the Makefile in the current directory.

Source Code 1:
/*Header File*/
#ifndef _FUN0_H_
#define _FUN0_H_
#endif
extern void fun0_printf(void);
extern void fun1_printf(void);
/*Source File*/
#include <stdio.h>
#include "fun0.h"
void fun0_printf(void)
{
printf("Call 'fun0'. \r\n");
}
</stdio.h>
Source Code 2:
/*Header File*/
#ifndef _FUN1_H_
#define _FUN1_H_
#endif
extern void fun1_printf(void);
/*Source File*/
#include <stdio.h>
#include "fun1.h"
void fun1_printf(void)
{
printf("Call 'fun1'.\r\n");
}
</stdio.h>
Main Function Source Code:
/*Source File*/
#include <stdio.h>
#include "fun0.h"
#include "fun1.h"
#include "fun_lib_a.h"
#include "fun_lib_so.h"
int main(void)
{
#ifdef USE_DEBUG
printf("Debug Application startup.\r\n");
#endif
fun0_printf();
fun1_printf();
fun_lib_a_printf();
fun_lib_so_printf();
return 0;
}
</stdio.h>
Library files are stored in the “./lib” directory, one static library libfun_a.a and one dynamic library libfun_so.so.
The Makefile file is the same as the Makefile template in section “2.1”.
Test Run:

[If the executable file prompts that “libfun_so.so” is missing, you need to copy “libfun_so.so” to the “/lib” or “/usr/lib” directory, as the system executes the program, defaulting to this path to pull dynamic libraries]
3.2 Generating Static Library
Write a test routine, producing a library file that is called in section “3.1” (libfun_a.a). The file storage directory structure is as follows:

Source File:
/*Header File*/
#ifndef _FUN_LIB_A_H_
#define _FUN_LIB_A_H_
#endif
extern void fun_lib_a_printf(void);
/*Source File*/
#include <stdio.h>
#include "fun_lib_a.h"
void fun_lib_a_printf(void)
{
printf("Call 'fun_lib_a'.\r\n");
}
</stdio.h>
The Makefile file is the same as the Makefile template in section “2.2”.
Compile to generate the static library:

3.3 Generating Dynamic Library
Write a test routine, producing a library file that is called in section “3.1” (libfun_so.so). The file storage directory structure is as follows:

Source File:
/*Header File*/
#ifndef _FUN_LIB_SO_H_
#define _FUN_LIB_SO_H_
#endif
extern void fun_lib_so_printf(void);
/*Source File*/
#include <stdio.h>
#include "fun_lib_so.h"
void fun_lib_so_printf(void)
{
printf("Call 'fun_lib_so'.\r\n");
}
</stdio.h>
Compile to generate the dynamic library:

Original text:https://blog.csdn.net/qq_20553613/article/details/90649734
The source is from the internet, and the copyright belongs to the original author. If there is any infringement, please contact us for deletion.
——
EOF
——
A friend of mine, whom I admire greatly, has developed a website that records interview questions from major internet companies, allowing users to view questions by industry, company, position, subject, and examination time. Interested parties are welcome to scan the QR code below to use it~
Hello, I am Feiyu, having studied CS at a prominent 985 university for both my bachelor's and master's degrees, and I have worked as a Linux C/C++ backend development engineer in departments like Baidu Search and ByteDance e-commerce.
I am also a Zhihu blogger @Han Feiyu, where I share experiences in C/C++, computer learning, and work insights. Feel free to click here to view my previous study notes & experiences & shared resources.
I have formed several communities for discussion, where there are both experts and novices. If you're interested, you can join the group for exchange.
Feel free to add me on WeChat, and I will invite you to the technical exchange group. Additionally, I will often share computer learning experiences and work insights on WeChat, as well as some internal referral opportunities.
Add me on WeChat to open another window