How to Fix Undefined Reference Error in C++?
Last Updated :
31 Jan, 2024
In C++, the undefined reference error typically occurs during the linking phase of compilation. This error indicates that the compiler knows about a function or variable (it has been declared), but it cannot find the actual implementation (definition) during the linking process. The linker needs to resolve these references to create a complete executable.
In this article, we will discuss how to fix undefined reference errors in C++.
Fix Undefined Reference Errors in C++
Below are some practical situations of undefined reference errors and the solution to fix them:
1. Define the Function and Variables
Undefined reference errors are mostly caused due to missing function definitions. So, if we have declared a function or variable but forgot to provide its implementation, we need to define it.
Example
C++
// C++ program to fix undefined reference error
#include <iostream>
using namespace std;
// Declaration without definition
void myFunction();
int main()
{
// Call to undefined function
myFunction();
return 0;
}
Output
/usr/bin/ld: /tmp/ccQwkbwQ.o: in function `main':
main.cpp:(.text+0x9): undefined reference to `myFunction()'
collect2: error: ld returned 1 exit status
2. Linking Necessary Libraries
Make sure that you are linking all necessary source files, object files and libraries during compilation and if you split your code into multiple files make sure that all object files are linked correctly.
For Example, linking mylib source file, use the following command:
g++ main.cpp myFunctions.cpp -o myProgram -lmylib
3. Check for Namespace and Scope Issues
If you are using namespaces, make sure the function/variable is in the correct namespace and included in the main file also variables are defined within the scope.
Example
C++
// C++ to show undefined reference error due to incorrect
// namespace
#include <iostream>
namespace MyNamespace {
void myFunction();
}
// Definition inside the namespace
void MyNamespace::myFunction()
{
std::cout << "Hello from MyNamespace::myFunction!\n";
}
int main()
{
myFunction();
return 0;
}
Output
main.cpp: In function ‘int main()’:
main.cpp:17:5: error: ‘myFunction’ was not declared in this scope; did you mean ‘MyNamespace::myFunction’?
17 | myFunction();
| ^~~~~~~~~~
| MyNamespace::myFunction
main.cpp:10:6: note: ‘MyNamespace::myFunction’ declared here
10 | void MyNamespace::myFunction()
Conclusion
We encounter the 'undefined reference' error in C++ mostly due to issues with function or variable definitions, improper linking, or scope/naming problems. By careful checking the code and build process for these common issues, we can successfully resolve this error.
Similar Reads
What is an Undefined Reference Error in C++? In C++, an undefined reference is an error that occurs when the linker can't find the definition of a function or a variable used in the linking stage of the compilation process. This error needs to be resolved for the smooth working of the program. When Does Undefined Reference Error Occur in C++?A
2 min read
How to fix auto keyword error in Dev-C++ The auto keyword in C++ specifies that the type of the variable that is being declared will be automatically deducted from its initializer. In case of functions, if their return type is auto then that will be evaluated by return type expression at runtime. CPP // C++ program to illustrate the auto /
2 min read
How to Get Error Message When ifstream Open Fails in C++? In C++, the std::ifstream class is used to open a file for input operations. It associates an input stream to the file. However, due to some reasons, it may be unable to open the requested file. In this article, we will learn how to show an error message when the ifstream class fails to open the fil
2 min read
Reason of runtime error in C/C++ In this article, we will discuss the reason for the run-time error and its solution. Runtime Error: A runtime error in a program is an error that occurs while the program is running after being successfully compiled. Below are some methods to identify the reason behind Runtime errors:Â Method 1: Whe
3 min read
How to Resolve a Name Conflict in C++? In C++, naming conflict occurs when two identifiers in the same scope have the same name and the compiler cannot infer which identifier to refer to when it is mentioned in the program. In this article, we will discuss what are name conflicts, what are its causes, and how to resolve the name conflict
4 min read
How to Fix Segmentation Fault in C++? A segmentation fault is a specific kind of error caused by accessing memory that does not belong to you. It's a common issue in C++ and other low-level languages, while working with arrays and pointers. In this article, we'll explore common causes of segmentation faults and provide strategies to ide
4 min read
Overloads of the Different References in C++ This article focuses on function/method overloads by references, as well as the types of arguments that can be passed. Prerequisites: l-value references.r-value references.Move semantics - std::move(). Overview:l-value refers to a memory location that identifies an object. r-value refers to the data
12 min read
Different ways to use Const with Reference to a Pointer in C++ Before moving forward with using const with Reference to a Pointers, let us first see what they are one by one: Pointers are used to store the address of variables or a memory location. A variable can be declared as a pointer by putting â*â in the declaration. datatype *var_name; Example: CPP // C++
5 min read
How Do I Use 'extern' to Share Variables Between Source Files? In C++, extern is a keyword that helps in sharing variables, and functions declared in one file to all the other files. In this article, we will learn how we can use the extern keyword to share variables between source files in C++. How to Use Extern Keyword in C++The extern keyword in C++ extends t
2 min read
How to Handle Incorrect Values in a Constructor? In C++, constructors are used to construct and initialize the object of its class. If incorrect values are given to the constructor, then it may cause inconsistency in a program. In this article, we will learn how to handle incorrect values in a constructor in C++ Handling Incorrect Arguments for Co
2 min read