Linker discrepancies, such as “undefined reference” or “unresolved external symbol,” can pose challenges for C++ programmers trying to progress. This sort of error particularly arises when the code appears to compile correctly but fails during linking. It may also happen if the linker is unable to locate the definition of a declared function, variable, or object. Common reasons include missing function definitions, incorrect signatures, absent object files, or libraries that have not been linked. In the following article, we will examine these errors, their frequent causes, techniques for effective resolution, and several illustrative examples.
Contents:
- Linker Discrepancies in C++
- Reasons for Undefined Reference / Unresolved External Symbol Errors in C++
- Solutions for Undefined Reference / Unresolved External Errors in C++
- Frequent Scenarios and Solutions for Undefined Reference / Unresolved External Symbol Errors in C++
- Final Thoughts
Linker Discrepancies in C++
Linker discrepancies such as “undefined reference” or “unresolved external symbol” arise notably when the code appears to be compiled and the linking process encounters issues. These errors can also occur if the linker fails to ascertain the definition of a declared function, variable, or object. Frequent causes include missing function definitions, inappropriate signatures, absence of object files, or libraries not being linked.
1. What is an Undefined Reference Error in C++?
An undefined reference error is triggered whenever the linker is unable to locate the definition of a function or variable declared and used in the program. Thus, contrary to a compilation error, which halts the compilation process, an undefined reference suggests that the linking stage could not identify an implementation of an entity recognized by the compiler in the source file.
Illustration:
```javascript
closeoutput88877() {
var code = editor88877.getSession().getValue();
jQuery(".maineditor88877 .code-editor-output").hide();
}
// Bind event listeners to the buttons
document.getElementById("copyBtn88877").addEventListener("click", copyCodeToClipboard88877);
document.getElementById("runBtn88877").addEventListener("click", runCode88877);
document.getElementById("closeoutputBtn88877").addEventListener("click", closeoutput88877);
Output:
Explanation: The code illustrates how a function, myFunction(), is declared but lacks a definition, causing an undefined reference error during the linking step since the linker fails to locate its implementation.
2. What does an Unresolved External Symbol Error mean in C++?
An unresolved external symbol error is akin to an MSVC undefined reference error. It arises whenever an object file or library is absent during the linking process, leading to missing function or variable definitions.
Example:
Output:

Explanation: The code demonstrates how a function myFunction() has been declared but not defined, which results in an unresolved external symbol error during the linking phase because the linker cannot locate its implementation.
Reasons for Undefined Reference / Unresolved External Symbol Errors in C++
- When you declare a function but no linking files provide a definition for that function, you will encounter an undefined reference to that function.
- If the declaration and the definition of a function are mismatched, it will produce a linker error.
- In the absence of an object file containing a function definition, the linker cannot resolve the reference.
- If you are utilizing a static library and it may not have been linked properly, this could lead to an unresolved external symbol.
- Template and inline functions need to be defined in the header file to prevent linking issues.
- If a function is situated within a namespace but is called without clear specification, the linker will be unable to find it.
- A pure virtual function must be implemented in a derived class; otherwise, the linker will generate an error.
Solutions for Undefined Reference / Unresolved External Errors in C++
- Missing Function Definition: Ensure every declared function has an associated definition.
- Incorrect Function Signature: Verify that the function declaration and definition correspond correctly.
- Undefined Class Member Function: Supply a definition for ClassName::method().
- Static Library Not Linked: Confirm that the .a (GCC) or .lib (MSVC) file is linked prior to compilation.
- Virtual Function Not Defined: Implement all pure virtual functions of derived classes.
- Namespace Issues: Use Namespace::Function() or apply a directive to access functions.
- Inline and Template Issues: You must define inline and template functions properly to avoid linking errors.
``````html
functions in header files.
Frequent Scenarios and Solutions for Undefined Reference / Unresolved External Symbol Errors in C++
1. Absent Function Definition
An absent function definition is an issue that arises when a function is declared but lacks a corresponding definition in any of the linked files, leading the linker to produce an undefined reference error.
Illustration:
Output with Error:

Clarification: The preceding code specifies myFunction() but fails to provide a definition for it, causing the linker to be unable to locate its implementation, which results in an undefined reference error during the linking stage.
Resolution: Include the function definition.
Illustration:
Result:

Clarification: The provided code guarantees that the linker can locate the function's implementation effectively, thus fixing the error.
2. Incorrect Function Signature
An erroneous function signature is an issue that arises when the function declaration and definition do not correspond precisely, resulting in the linker failing to resolve the function invocation.
Illustration:
Error Output:
Clarification: The code above results in an undefined reference error because the function declaration (void myFunction(int x);) anticipates an int parameter, yet the definition (void myFunction()) omits any parameters, consequently preventing the linker from aligning the function invocation myFunction(10) with the provided definition.
How to Rectify: Ensure the function definition corresponds with the declaration.
Illustration:
Output:

Explanation: This snippet illustrates how a function definition aligns with its declaration, enabling the linker to resolve the function call without encountering any issues.
3. Issues with Inline Functions
The following are undefined reference errors that arise when inline functions are declared in one unit but not defined within the same unit, causing the linker to fail in detecting a definition for these inline functions.
Example:
Output with Error:
Clarification: The provided code illustrates that the inline function myFunction() is declared but lacks a definition, leading to an error due to an undefined reference.
How to Resolve it: Provide the inline function's definition in either the same file or in a header file.
Sample:
Result:

Clarification: The program exhibits how the compiler circumvents the linking error by replacing the inline definition calls to myFunction().
4. Template Function Concern
A template function must be defined within the same translation unit or included in a header file, since the compiler needs to produce its definition for every type utilized. In the absence of the definition, the linker will produce an undefined reference error.
Sample:
Output with Error:

Clarification: The provided code encounters an undefined reference issue because the template function add(T a, T b) is declared but lacks a definition, implying that the complete definition should reside in the same translation unit.
Resolution: Implement the template function in either the same file or a header file.
Illustration:
Output:

Clarification: The preceding code appropriately defines and utilizes a template function. The template function add(T a, T b) incorporates both a declaration and a definition within the same file, enabling the compiler to create the correct instantiation of the function when called with add(3, 4).
5. Virtual Function Not Implemented in Derived Class
A pure virtual function in a base class necessitates implementation in any derived class. The absence of this implementation results in an undefined reference error generated by the linker, leaving the class abstract and inhibiting the creation of objects.
Illustration:
``````html
class="code-editor-output-wrap output88793">



