C++ error: fatal error LNK1169: One or more multiple-defined symbols found

I. Overview

  When using visual studio to write C++ code, in order to write specifications and standards, it is often necessary to create multiple source files and header files (and the source files need to contain relevant header files). This not only exercises your modular thinking, but also enables timely positioning. Which module caused the error? But there are pros and cons. Think about it, C/C++ are both compiled languages, so do compiled languages ​​need to go through: preprocessing, compilation, assembly, and linking stages?

 Here is a brief introduction to the work done at each stage:

(1) Preprocessing:
Add all the contents of the header file (.h) to the source file (.cpp).
Conditional compilation (#ifdef...#define...#endif, etc.)
macro replacement (#define num 100: replace num with 100)
Delete the comments in the file
(2) Compile
and check whether the source code complies with the C++ syntax specifications (we define illegal identifiers, which are often determined at compile time)
Translate the code in the source file into assembly language
(3) Assemble
the Translate assembly language into machine language (binary language)
(4) Link
to generate executable file

 Now that you have understood the above work, let’s look at the following code:
(1)student.h:
int a;
.....//可以放其他代码的声明
(2)student.cpp:
#include "student.h"
(3)teacher.h:
(4)teacher.cpp:
#include"student.h"
#include "teacher.h"
(5)main.cpp:
#include "student.h"
#include "teacher.h"
以上代码我稍微解释一下:
	就是在main.cpp中包含了teacher.h和student.h,而teacher.cpp中又包含了student.h,我们只在student.h中定义了一个全局变量int a。现在的情况相当于是student.h被包含了两次,也就是全局变量a被重复定义了两次。
注意:
	一般我们把函数的声明或变量的定义放置在.h文件中

The error message is as follows:

Insert image description here
Explain the error message: variable a is repeatedly defined multiple times (note: the variable is defined in the header file)

1. When multiple files are included, variables will be repeatedly defined.

(1) The cause of the problem
  is that when the C++ program is compiled, it will compile the contents of the source file (.cpp), and because the source file (.cpp) also contains the contents of the header file (.h), Therefore, variables will be repeatedly defined. If you don’t understand, you can read the explanation in parentheses (note: there is a definition of variable a in student.h, then student.h is included in teacher.cpp, and finally in main. cpp contains student.h and teacher.h)
Insert image description here
(2) To solve the problem
  , take the code at the beginning of the article as an example. The solved code is as follows:

第一种解决办法:
(1)student.h:
extern int a;
.....//可以放其他代码的声明
(2)student.cpp:
int a;
#include "student.h"
(3)teacher.h:

(4)teacher.cpp:
#include"student.h"
#include "teacher.h"
(5)main.cpp:
#include "student.h"
#include "teacher.h"

第二种解决办法:
(1)student.h:
static int a;
.....//可以放其他代码的声明
(2)student.cpp:
#include "student.h"
(3)teacher.h:
(4)teacher.cpp:
#include"student.h"
#include "teacher.h"
(5)main.cpp:
#include "student.h"
#include "teacher.h"

Note:
The first solution:
  define the variable in the source file (.cpp) and introduce it with the keyword "extern" in the header file (.h). At this time, the compiler will know that the variable is in cpp. Variables, not variables in .h.

The second solution:
  still put the definition of the variable in the header file (.h), but the variable is modified with "static". The function of adding static is to limit the role of the variable. Domain is only valid in this file and will be invalid in other files.

The third solution:
  Use inline to solve the problem.
The fourth solution:
  Use the force/force method. This method can be solved in the visual studio integrated development environment. , the compilation can pass, but the compiler will still prompt a warning.

2. Conditional compilation: #ifndef…#define…#endif

  Many blogs on the Internet say that conditional compilation can be used to solve the problem of repeated definitions of variables in multiple files. Some students have added conditional compilation to the files, but the repeated definitions have not been solved once the program is run. The blogger needs to say something here. First of all, the idea of ​​using conditional compilation is correct and worthy of recognition. Why? Because conditional compilation can solve the problem of repeated inclusion of header files (for example: earlier in the article, teacher.cpp contains student.h, and main.cpp contains teacher, h and tstudent.h, doesn’t it mean that student.h is included Duplicate inclusion twice), it is worth noting that although conditional compilation can solve the problem of repeated inclusion of header files in the compilation stage (which is equivalent to solving the problem of duplicate definitions of variables), it cannot pass during linking because the linking stage needs to ensure that the defined There is only one copy of global variables in the entire program. So conditional compilation still cannot solve it

Guess you like

Origin blog.csdn.net/Mr_zhang1911116/article/details/126241815