C++ programs are written in files, just read this article

I often hear a term - writing in separate files .

So, what is this?
The meaning of writing in separate files (modular programming) is to store the code of a program in multiple files, that is, to place different functional modules in different files, and to establish necessary connections between these modules. A programming method that cooperates with each other to complete the entire function.
Insert image description here

And, what are its advantages?

1.Advantages

1. Easy to maintain : Writing programs in separate files can facilitate modular management. Each module has relatively independent functions and responsibilities. Modifying one module will not affect the functions of other modules, so the maintenance and update of the program are easier. easy.

2. Code reuse : Modular programming allows multiple programs to share the same module to reduce code redundancy and duplication.

3. High readability : Writing in separate files can make the program more readable. The functions and responsibilities of each module are clear and easy to understand.

4. Improve development efficiency : Programmers can develop multiple modules at the same time to implement the entire program, which can improve development efficiency.

5. Convenient testing : Modular programs can be tested separately, which makes it easier to identify and solve problems in the program.

6. Improve program stability : Modular programming can reduce the possibility of program errors and improve program stability and reliability. 1. Easy to maintain: Writing programs in separate files can facilitate modular management. Each module has relatively independent functions and responsibilities. Modifying one module will not affect the functions of other modules, so the maintenance and update of the program are easier. easy.

1. File type

C++ programs are usually divided into three types of files:

Header file (.h) : Contains various declarations for inclusion by cpp files.
Module file (.cpp) : Contains function definitions, also known as function modules.
Main program file (.cpp) : A file containing the main() function, which serves as the program entry and calls the methods implemented in the module file.

1.1.Header file

Header files exist to contact multiple source files, and they are the interface between source files. Similar to the C language, C++ requires declaration first and then use, but it is compiled in a single file during compilation. Here is an example without a header file:

// main.cpp
void fun();     // 必须先声明,否则会出错

int main(){
    
    
fun();       // 调用fun函数
}

// fun.cpp
#include <iostream>
void fun(){
    
    
std::cout << "This is fun." << std::endl;
}

After compiling these two files through the command "g++ main.cpp fun.cpp", they can run normally. What needs to be understood is that the compilation process of C++ is to compile each file separately, and then combine the compiled multiple files into an executable program through linking. Only function declarations are checked during compilation. As long as the file has declared a function before, it will be compiled successfully. Function definitions are checked during the linking phase, and linking involves multiple files. In the above example, main.cpp must first declare the function fun() before it can be compiled. Although the function is not defined in main.cpp, it is found that fun.cpp defines the function when linking, so the program is correct.

However, manually declaring functions from other files every time is tedious and error-prone. Therefore, we put the declaration statements into a file and call it a "header file". If you need to use a certain function, include the header file in which it is located. The contents of the header file will be pasted into the source file before compilation, so that it can pass normally during compilation. Next let’s look at an example using a header file:

// main.cpp
#include "header.h"

int main(){
    
    
fun();
}

// header.h
#ifndef HEADER_H
#define HEADER_H

void fun();

#endif

// fun.cpp
#include <iostream>
void fun(){
    
    
std::cout << "This is fun." << std::endl;
}

The contents of header files are usually surrounded by conditional compilation prepared statements (such as the example above) to prevent multiple inclusions due to dependency problems.

Now that we understand the role of header files, what content should be placed in header files? What should not be placed in header files? It's very simple. If this part needs to be copied to each related cpp file, put it in the header file; if copying it in multiple cpp files may cause link errors, don't put it in the header file. Specifically:

Function declaration: Obviously it should be placed in the header file, as mentioned before.
Class definition, structure definition: Imagine the logic of function definition, it seems that it cannot be placed in the header file. But it should actually be placed in the header file. First, each cpp file should have a definition so the compiler knows how to allocate space for the object. Second, type definitions do not allocate space on memory.
Template function: The compiler must instantiate the corresponding function at compile time based on the function template, so it should be placed in the header file.
Inline functions: The calling location is inserted at compile time and therefore should also be placed in the header file.
Function definition: No! C++ stipulates that a function with the same signature in a program can only have one definition. If the function definition is placed in a header file and multiple cpp files include the header file, multiple definition versions will appear during linking, resulting in link errors.
Variable definition: No! Similar to the above situation, when it is included in multiple files, there will be a problem of defining the same variable multiple times, resulting in link errors. However, static variables and extern variables can appear in multiple files, as well as macro-defined constants, because these will not cause link errors.

1.2. cpp source file

After extracting the contents of the header file, the remaining contents can be placed in the cpp file. In the cpp file, there is no change except that the corresponding header file needs to be included.

Although they are all cpp files, it is still necessary to treat them as module files and main program files. In actual development, the function providers and users of module files and main program files are usually developed by different people.

2. Multi-file compilation

If you use the integrated development environment under Windows, you only need to click the "Build" button and it will be done with one click, but you may have a lack of understanding of this process. Therefore, taking Linux as an example, we will introduce the simplest multi-file compilation command:

g++ main.cpp fun.cpp -o out # 列举所有要编译的文件

You can also compile it separately into an .o file first and then link it:

g++ -c main.cpp -o main.o
g++ -c fun.cpp -o fun.o
g++ main.o fun.o -o out

The second approach may cause confusion - why so much trouble? However, this approach provides one advantage - module compilation updates. Only the modified files need to be compiled and linked with other files without recompiling the entire project.

In an actual project, the number of project files may be very large. No matter which method is used, the files need to be listed one by one, which is not practical. Integrated development environment tools naturally do not have this problem. But on Linux, you can also use Makefile for project management and automatic compilation. Of course, this is another aspect to learn.

Guess you like

Origin blog.csdn.net/aliyonghang/article/details/132548330