Home > Article > Backend Development > C++ compilation error: undefined reference, how to solve it?
C is a popular programming language, but during use, the compilation error "undefined reference" often occurs, which brings a lot of trouble to program development. This article will discuss the solution to the "undefined reference" error from both the cause and the solution.
1. Cause of error
When the C compiler compiles a source file, it will be divided into two stages: the compilation stage and the link stage. The compilation phase converts the source code in the source files into assembly code, while the linking phase combines different source files into an executable file.
There are two concepts that need to be distinguished here: declaration and definition. Declaration refers to declaring the existence of a variable or function in a program, telling the compiler its name, type, parameter list and other information. Definition is to allocate storage space for a variable or function and give the implementation code. In a project, if declarations and definitions are not put together, a compilation error of "undefined reference" may occur.
Specifically, the main reason for this error is:
For the header file, in order to ensure the program For robustness and readability, header file protection (conditional compilation instructions defined by #ifndef and #define) is generally added. However, when a header file is included multiple times, the compiler will throw a "multiple definition" error. In order to solve this problem, the header file protection mechanism was introduced. However, improper use of the header file protection mechanism may also lead to the compilation error of "undefined reference".
When the compiler cannot find some undefined functions during the link phase, "Undefined" will appear Reference" error. Usually, this is because the corresponding library file or link library file is missing.
If the function or variable is not defined in the program, the error "undefined reference" will occur.
2. Solution
In order to avoid the header file being included multiple times, you can use the header file protection mechanism. Just add the following code to the header file:
#ifndef _HEADER_FILE_NAME_H_ #define _HEADER_FILE_NAME_H_ //..头文件内容 #endif
Among them, _HEADER_FILE_NAME_H_ can be replaced with other identifiers to distinguish different header files. This can avoid repeated declaration and definition of header files, thereby eliminating the compilation error of "undefined reference".
If certain library functions or link library files are used when compiling the program, then these library files need to be linked to the available link during the link phase. in the executable file. For the Windows platform, you can use the following command on the command line to link:
g++ main.cpp -o main.exe -l library_name
Among them, -l is followed by the name of the library file that needs to be linked. For the Linux platform, you need to use the following command on the command line to link:
g++ main.cpp -o main -l library_name
Similarly, -l is followed by the name of the library file that needs to be linked. If an "undefined reference" error occurs, you can check whether the required library file is linked to and whether the path is correct.
If the "undefined reference" error occurs, it is usually because the corresponding declarations and definitions cannot be found in the program. To solve this problem, you can check that all declarations and definitions are included in the program to ensure that the program compiles successfully.
To sum up, when the "undefined reference" error occurs, it is usually because the header file is included multiple times, the link library file is not linked, or the functions or variables in the program are not correctly defined. Therefore, in order to avoid this error from happening, we need to carefully check the declaration and definition of the program and point out where the problem occurs. In actual programming, you must always pay attention to this error, locate and solve the problem in time, and ensure the correctness and normal operation of the program.
The above is the detailed content of C++ compilation error: undefined reference, how to solve it?. For more information, please follow other related articles on the PHP Chinese website!