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.
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:
Cpp
Code Copied!
var isMobile = window.innerWidth “);
editor88877.setValue(decodedContent); // Set the default text
editor88877.clearSelection();
editor88877.setOptions({
maxLines: Infinity
});
function decodeHTML88877(input) {
var doc = new DOMParser().parseFromString(input, “text/html”);
return doc.documentElement.textContent;
}
// Function to copy code to clipboard
function copyCodeToClipboard88877() {
const code = editor88877.getValue(); // Get code from the editor
navigator.clipboard.writeText(code).then(() => {
jQuery(“.maineditor88877 .copymessage”).show();
setTimeout(function() {
jQuery(“.maineditor88877 .copymessage”).hide();
}, 2000);
}).catch(err => {
console.error(“Error copying code: “, err);
});
}
function runCode88877() {
var code = editor88877.getSession().getValue();
// 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:
Cpp
Code Duplicated!
var isMobile = window.innerWidth ");
editor91355.setValue(decodedContent); // Assign the initial text
editor91355.clearSelection();
editor91355.setOptions({
maxLines: Infinity
});
function decodeHTML91355(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function to copy code to clipboard
function copyCodeToClipboard91355() {
const code = editor91355.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
// alert("Code copied to clipboard!");
function closeoutput91355() {
var code = editor91355.getSession().getValue();
jQuery(".maineditor91355 .code-editor-output").hide();
}
// Bind event listeners to the buttons
document.getElementById("copyBtn91355").addEventListener("click", copyCodeToClipboard91355);
document.getElementById("runBtn91355").addEventListener("click", runCode91355);
document.getElementById("closeoutputBtn91355").addEventListener("click", closeoutput91355);
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.
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.
function closeoutput77908() {
var code = editor77908.getSession().getValue();
jQuery(".maineditor77908 .code-editor-output").hide();
}
// Bind event listeners to the buttons
document.getElementById("copyBtn77908").addEventListener("click", copyCodeToClipboard77908);
document.getElementById("runBtn77908").addEventListener("click", runCode77908);
document.getElementById("closeoutputBtn77908").addEventListener("click", closeoutput77908);
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:
Cpp
Code Copied!
var isMobile = window.innerWidth ");
editor74118.setValue(decodedContent); // Set the default text
editor74118.clearSelection();
editor74118.setOptions({
maxLines: Infinity
});
function decodeHTML74118(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function to duplicate code to clipboard
function copyCodeToClipboard74118() {
const code = editor74118.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
// alert("Code copied to clipboard!");
function closeoutput74118() {
var code = editor74118.getSession().getValue();
jQuery(".maineditor74118 .code-editor-output").hide();
}
// Bind event listeners to the buttons
document.getElementById("copyBtn74118").addEventListener("click", copyCodeToClipboard74118);
document.getElementById("runBtn74118").addEventListener("click", runCode74118);
document.getElementById("closeoutputBtn74118").addEventListener("click", closeoutput74118);
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:
Cpp
Code Copied!
var isMobile = window.innerWidth ");
editor7453.setValue(decodedContent); // Initialize the default text
editor7453.clearSelection();
editor7453.setOptions({
maxLines: Infinity
});
function decodeHTML7453(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function to copy code to clipboard
function copyCodeToClipboard7453() {
const code = editor7453.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
// alert("Code copied to clipboard!");
function closeoutput7453() {
var code = editor7453.getSession().getValue();
jQuery(".maineditor7453 .code-editor-output").hide();
}
// Attach event listeners to the buttons
document.getElementById("copyBtn7453").addEventListener("click", copyCodeToClipboard7453);
document.getElementById("runBtn7453").addEventListener("click", runCode7453);
document.getElementById("closeoutputBtn7453").addEventListener("click", closeoutput7453);
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:
Cpp
Code Copied!
var isMobile = window.innerWidth ");
editor13084.setValue(decodedContent); // Initialize the default text
editor13084.clearSelection();
editor13084.setOptions({
maxLines: Infinity
});
function decodeHTML13084(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function to copy code to clipboard
function copyCodeToClipboard13084() {
const code = editor13084.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
// alert("Code copied to clipboard!");
function closeoutput13084() {
var code = editor13084.getSession().getValue();
jQuery(".maineditor13084 .code-editor-output").hide();
}
// Attach event listeners to the buttons
document.getElementById("copyBtn13084").addEventListener("click", copyCodeToClipboard13084);
document.getElementById("runBtn13084").addEventListener("click", runCode13084);
document.getElementById("closeoutputBtn13084").addEventListener("click", closeoutput13084);
Output with Error:
``````html
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:
Cpp
Code Duplicated!
var isMobile = window.innerWidth ");
editor24749.setValue(decodedContent); // Define the initial text
editor24749.clearSelection();
editor24749.setOptions({
maxLines: Infinity
});
function decodeHTML24749(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function to duplicate code into clipboard
function copyCodeToClipboard24749() {
const code = editor24749.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
jQuery(".maineditor24749 .copymessage").show();
setTimeout(function() {
jQuery(".maineditor24749 .copymessage").hide();
}, 2000);
}).catch(err => {
console.error("Error duplicating code: ", err);
});
}
function closeoutput24749() {
var code = editor24749.getSession().getValue();
jQuery(".maineditor24749 .code-editor-output").hide();
}
// Register event listeners to the buttons
document.getElementById("copyBtn24749").addEventListener("click", copyCodeToClipboard24749);
document.getElementById("runBtn24749").addEventListener("click", runCode24749);
document.getElementById("closeoutputBtn24749").addEventListener("click", closeoutput24749);
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:
Cpp
Code Duplicated!
var isMobile = window.innerWidth ");
editor9604.setValue(decodedContent); // Set the initial text
editor9604.clearSelection();
editor9604.setOptions({
maxLines: Infinity
});
function decodeHTML9604(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function to duplicate code into clipboard
function copyCodeToClipboard9604() {
const code = editor9604.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
jQuery(".maineditor9604 .copymessage").show();
setTimeout(function() {
jQuery(".maineditor9604 .copymessage").hide();
}, 2000);
}).catch(err => {
console.error("Error duplicating code: ", err);
});
}
``````javascript
err);
});
}
function executeCode9604() {
var sourceCode = editor9604.getSession().getValue();
function hideOutput9604() {
var sourceCode = editor9604.getSession().getValue();
jQuery(".maineditor9604 .code-editor-output").hide();
}
// Bind event listeners to the buttons
document.getElementById("copyBtn9604").addEventListener("click", copyToClipboard9604);
document.getElementById("runBtn9604").addEventListener("click", executeCode9604);
document.getElementById("closeoutputBtn9604").addEventListener("click", hideOutput9604);
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:
Cpp
Code Copied!
var isMobile = window.innerWidth ");
editor6639.setValue(decodedContent); // Set the default text
editor6639.clearSelection();
editor6639.setOptions({
maxLines: Infinity
});
function decodeHTML6639(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function to copy code to clipboard
function copyToClipboard6639() {
const code = editor6639.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
// alert("Code copied to clipboard!");
function hideOutput6639() {
var sourceCode = editor6639.getSession().getValue();
jQuery(".maineditor6639 .code-editor-output").hide();
}
// Bind event listeners to the buttons
document.getElementById("copyBtn6639").addEventListener("click", copyToClipboard6639);
document.getElementById("runBtn6639").addEventListener("click", executeCode6639);
document.getElementById("closeoutputBtn6639").addEventListener("click", hideOutput6639);
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.
editor88793.setValue(decodedContent); // Assign the default text
editor88793.clearSelection();
editor88793.setOptions({
maxLines: Infinity
});
function decodeHTML88793(input) {
var doc = new DOMParser().parseFromString(input, “text/html”);
return doc.documentElement.textContent;
}
// Function to copy code to clipboard
function copyCodeToClipboard88793() {
const code = editor88793.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
// alert(“Code copied to clipboard!”);
function closeoutput88793() {
var code = editor88793.getSession().getValue();
jQuery(".maineditor88793 .code-editor-output").hide();
}
// Bind event listeners to the buttons
document.getElementById("copyBtn88793").addEventListener("click", copyCodeToClipboard88793);
document.getElementById("runBtn88793").addEventListener("click", runCode88793);
document.getElementById("closeoutputBtn88793").addEventListener("click", closeoutput88793);
Output with Error:
Explanation: The code leads to a linker error due to the pure virtual function display() being declared in the Base class without implementation in the Derived class. As Derived fails to provide a definition for display(), it remains categorized as an abstract class and cannot be instantiated.
Resolution: Implement the Pure Virtual Function in the Derived Class
Example:
Cpp
Code Copied!
var isMobile = window.innerWidth ");
editor77134.setValue(decodedContent); // Assign the default text
editor77134.clearSelection();
editor77134.setOptions({
maxLines: Infinity
});
function decodeHTML77134(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function to copy code to clipboard
function copyCodeToClipboard77134() {
const code = editor77134.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
// alert("Code copied to clipboard!");
function closeoutput77134() {
var code = editor77134.getSession().getValue();
jQuery(".maineditor77134 .code-editor-output").hide();
}
``````javascript
// Attach event handlers to the buttons
document.getElementById("copyBtn77134").addEventListener("click", copyCodeToClipboard77134);
document.getElementById("runBtn77134").addEventListener("click", runCode77134);
document.getElementById("closeoutputBtn77134").addEventListener("click", closeoutput77134);
Outcome:
Clarification: The code effectively establishes a pure virtual function within the subclass, enabling successful compilation and operation.
6. Namespace and Scope Challenges
A function or variable defined within a namespace must be referred to using the namespace prefix unless a directive is in effect. Invoking the function without the appropriate prefix would prevent the linker from locating its definition, resulting in an unresolved reference error.
Instance:
Cpp
Code Copied!
var isMobile = window.innerWidth ");
editor70195.setValue(decodedContent); // Set the default text
editor70195.clearSelection();
editor70195.setOptions({
maxLines: Infinity
});
function decodeHTML70195(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function to copy code to clipboard
function copyCodeToClipboard70195() {
const code = editor70195.getValue(); // Get code from the editor
navigator.clipboard.writeText(code).then(() => {
// alert("Code copied to clipboard!");
jQuery.ajax({
url: "https://intellipaat.com/blog/wp-admin/admin-ajax.php",
type: "post",
data: {
language: "cpp",
code: code,
cmd_line_args: "",
variablenames: "",
action: "compilerajax"
},
success: function(response) {
var myArray = response.split("~");
var data = myArray[1];
jQuery(".output70195").html("
" + data + "");
jQuery(".maineditor70195 .code-editor-output").show();
jQuery("#runBtn70195 i.run-code").hide();
}
})
}
function closeoutput70195() {
var code = editor70195.getSession().getValue();
jQuery(".maineditor70195 .code-editor-output").hide();
}
// Attach event handlers to the buttons
document.getElementById("copyBtn70195").addEventListener("click", copyCodeToClipboard70195);
document.getElementById("runBtn70195").addEventListener("click", runCode70195);
document.getElementById("closeoutputBtn70195").addEventListener("click", closeoutput70195);
Output with Error:
Clarification: The preceding code causes a linker error because certain operations are defined within the MyNamespace namespace but are invoked without using the namespace prefix in main(). Consequently, the compiler cannot locate myFunction() in the global scope, resulting in an undefined error.
Solution: Utilize the Namespace Prefix or Directive
Instance:
Cpp
Code Copied!
var isMobile = window.innerWidth ");
editor83575.setValue(decodedContent);
``````javascript
// Clear the default text
editor83575.clearSelection();
editor83575.setOptions({
maxLines: Infinity
});
function decodeHTML83575(input) {
var document = new DOMParser().parseFromString(input, "text/html");
return document.documentElement.textContent;
}
// Function to duplicate code to clipboard
function copyCodeToClipboard83575() {
const codeSnippet = editor83575.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(codeSnippet).then(() => {
// alert("Code copied to clipboard!");
function closeoutput83575() {
var codeSnippet = editor83575.getSession().getValue();
jQuery(".maineditor83575 .code-editor-output").hide();
}
// Attach event listeners to the buttons
document.getElementById("copyBtn83575").addEventListener("click", copyCodeToClipboard83575);
document.getElementById("runBtn83575").addEventListener("click", runCode83575);
document.getElementById("closeoutputBtn83575").addEventListener("click", closeoutput83575);
Output:
Explanation: The code invokes myFunction() with the appropriate namespace qualifier, enabling the compiler to locate and link the function without encountering any errors.
Conclusion
Undefined references and unresolved external symbol errors emerge when the linker is unable to locate a definition for a declared function or variable. Typically, these errors result from absent functions, incorrect function types, missing object files, and inadequately linked libraries. To resolve such issues, all functions must be properly defined, the function signatures and declarations must correspond, and all requisite files must be linked appropriately, along with the management of templates, inline functions, and namespaces.
What are undefined reference/unresolved external symbol errors in C++ – FAQs
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional
Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes.The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.