C++

ERROR: Unresolved External Symbol C++

A variety of errors can occur in the code while compiling to any programming language like C++. There is an unresolved external symbol error from many of these errors in C++ so far. It may occur within your code while compilation when you miss some function definition, some library is missing for the usage of definitions, some external variable has been defined in the code, or some unfound file has been included in the code. On the other hand, the unresolved external symbol error is an undefined reference to “symbol” within the Ubuntu system. So, we will see how it can be found and how to resolve it in Ubuntu 20.04. Let’s get started with the simple c++ file creation via the touch query and opening it within Ubuntu’s editor, i.e., nano. Both the instructions have been displayed below.

$ touch error.cc
$ nano error.cc

Example 01:

Our first example is to elaborate on the “error: unresolved external symbol” in C++. So, we have been opening the just created file on the gnu nano editor to add code to it. So, the example code has been started by initializing an “iostream” header library to make our code executable. The “Std” namespace is used to use the code’s standard statements. We have declared the user-defined function show() after the namespace, and the main() method has been calling the function show() within its implementation. The program code doesn’t contain any function definition of the show() method in the code; it will cause an error “unresolved external symbol.” Let’s save and execute our newly made code.

#include<iostream>
using namespace std;
int show();
int main() {
  show();
}

The g++ compiler will be here to compile the c++ code file. The compilation returns the error “undefined reference to ‘show()’ method, which is the same as an unresolved external symbol in Ubuntu 20.04 system. This code won’t work until the error is resolved.

$ g++ error.cc

As a result, the file can’t be executed.

$ ./a.out

To avoid this error, we need to add the definition of the show() function after the main() function. So, we have defined the show() method in the code containing a single cout statement in it. Now, after the call from the main() method, it will be executed successfully.

#include<iostream>
using namespace std;
int show();
int main() {
  show();
}
int show() {
  cout <<Executing show method..."<<endl;
}

After compiling the updated code, the error got resolved.

$ g++ error.cc

On execution, it has executed the show() method as the output demonstrates.

$ ./a.out

Example 02:

Let’s take a look at another example to see the “ERROR: unresolved external symbol” in C++ code. So, we have added the iostream header library and the standard namespace at the first two lines. The pass() method declaration , and “show()” function definition is here. Within the “show()” function, pass() method is called. Within the main() function, the show() function is called. As the pass() function doesn’t contain any definition in the code, it will generate an error “unresolved external symbol” or “undefined reference to show” on the shell.

#include<iostream>
using namespace std;
void pass();
void show() {
  pass();
 }
int main() {
  show();
}

We have got the error “undefined reference to ‘pass()’ on the compilation. On execution, it is unable to execute the file so far.

$ g++ error.cc
$ ./a.out

To resolve this error, simply add the function pass() definition in the code after the main() method as demonstrated in the attached photo below. The pass() function contains 1 cout statement.

#include<iostream>
using namespace std;
void pass();
void show() {
  pass();
 }
int main() {
  show();
}
void pass() {
  cout<<"RUnning Pass function..."<<endl;
}

This time, no errors have been found in the compilation, and the code file has been executed successfully, i.e., executing the pass() method.

$ g++ error.cc
$ ./a.out

Example 03:

Let’s take a look at a different example. So, we have started the code with the same header and namespace. Before the main() function, we have declared a string variable “s” and used the keyword “extern” to declare it as an external variable. Within the main() method, we have been initializing it with some string value “Linux” using the assignment method. The cout statement is utilized here to show the string variable. It will cause an error because the string is external and cannot be initialized within the main() function.

#include<iostream>
using namespace std;
extern string s;
int main() {
  s = "Linux";
  cout<<s<<endl;
}

We have got the same undefined reference to ‘s’ error in the shell on the compilation of this code.

$ g++ error.cc
$ ./a.out

To resolve this issue, we have to declare and initialize the variable string ‘s’ inside the main() function without using the word “extern.” As per the below code, you can see that we have declared and initialized string ‘s’ within the main() function, and cout is used to display it.

#include<iostream>
using namespace std;
int main() {
  string s = "Linux";
  cout<<s<<endl;
}

Upon the updated code compilation, the error is resolved. After the execution, the string ‘s’ value has been displayed on the shell as below.

$ g++ error.cc
$ ./a.out

Conclusion:

In C++, we have a simple explanation to create and resolve the error “unresolved external symbol” or “undefined reference to the symbol.” We have created simple examples to demonstrate it within the Ubuntu 20.04 system. You can make use of all the examples on any other platform as well. We are extremely hopeful for your kind response.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.