“From today on, study hard and make progress every day”
Repetition is the best way to remember; spend one minute every day to memorize the basics of C language.
“100 Essential Notes for C Language Beginners Series“
Persevere! We are finally entering the practical series, which is also the most important and challenging part of C language.
Keep it up! Young ones!
[~~ A beginner, please forgive any mistakes ~~]
53. How to Manage and Organize Multiple Directories and Files in Actual C Language Project Development: A Simple and Easy-to-Use Template
Complex projects definitely involve multiple directories and files; it is impossible to write all the code in a single<span>main.c</span> file, as this would be too complex and tightly coupled, making maintenance and modification very difficult!
Mastering multi-file programming in projects is indeed a necessary skill in real projects!
The Simplest Multi-File Project Directory Structure
Assuming we are developing a powerful calculator in C language to perform various complex calculations such as addition, subtraction, multiplication, and division, we will modularize the core code of the calculator for easier management and reuse, storing it in separate files.
Single Layer Directory Structure
simple_c_project/
├── main.c # Main program entry
├── calculator.h # Header file (declaration of calculator interface functions)
├── calculator.c # Source file (implementation of calculator interface functions)
└── Makefile # Compilation script
Step 1: Header File (Interface Declaration)
calculator.h
#ifndef CALCULATOR_H // Header file guard to prevent multiple inclusions
#define CALCULATOR_H
// Function declarations
int add(int a, int b);
int subtract(int a, int b);
int multiply(int a, int b);
double divide(int a, int b);
// Constant definitions
#define MAX_INPUT 100
#define PI 3.14159
#endif // CALCULATOR_H
Step 2: Source File (Concrete Implementation)
calculator.c
#include "calculator.h"
// Addition implementation
int add(int a, int b) {
return a + b;
}
// Subtraction implementation
int subtract(int a, int b) {
return a - b;
}
// Multiplication implementation
int multiply(int a, int b) {
return a * b;
}
// Division implementation (note error handling)
double divide(int a, int b) {
if (b == 0) {
return 0.0; // Simple error handling
}
return (double)a / b;
}
Step 3: Main Program Call
main.c
#include <stdio.h>
#include "calculator.h" // Include our custom header file
int main() {
int x = 10, y = 5;
printf("=== Simple Calculator ===\n");
printf("%d + %d = %d\n", x, y, add(x, y));
printf("%d - %d = %d\n", x, y, subtract(x, y));
printf("%d * %d = %d\n", x, y, multiply(x, y));
printf("%d / %d = %.2f\n", x, y, divide(x, y));
return 0;
}
Step 4: Manual Compilation and Execution
# Compile separately
gcc -c calculator.c -o calculator.o
gcc -c main.c -o main.o
# Link to create executable file
gcc calculator.o main.o -o calculator
# Run
./calculator
Step 5: Automated Compilation
Automated compilation eliminates the need to repeatedly write compilation and execution commands; it can be automated.
Simple Makefile
# Compiler settings
CC = gcc
CFLAGS = -Wall -Iinclude
# Target file
TARGET = calculator
SRCS = ./main.c ./calculator.c
OBJS = $(SRCS:.c=.o)
# Default target
all: $(TARGET)
# Link target files
$(TARGET): $(OBJS)
$(CC) -o $@ $^
# Compile each .c file
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
# Clean generated files
clean:
rm -f $(OBJS) $(TARGET)
# Run program
run: $(TARGET)
./$(TARGET)
Compilation and Execution
# Compile all modules
make all
# Run
make run
# Clean files
make clean
How to Add More Modules and Files
Multi-Layer Directory Structure Expansion
better_c_project/
├── src/
│ ├── main.c
│ ├── calculator.c
│ └── math.c
├── include/
│ ├── calculator.h
│ └── math.h
└── Makefile
Summary
The benefits of multi-file programming in C language projects include:
- • Modularity: Separation of functions, easy maintenance
- • Reusability: Modules can be used in different projects
- • Collaborative Development: Multiple people can work on different files simultaneously
- • Compilation Efficiency: Only recompile modified files
Some students contacted me, wanting to have a study and exchange group. I hesitated to create one before due to concerns about advertisements, but I think having a group would indeed be more convenient, so I will try to create one this time.
If you need it, hurry up and join; the validity period is 7 days.

———- End ———-
[Special Statement: All articles in this public account are original or authorized by the author. Some content and images are sourced from the internet. Please feel free to use them. The views are for learning reference only. If there are any mistakes, please forgive me.~~]


“If you like C, please give a thumbs up”
Click the bottom right corner to view
“