3 minute read

Introduction

When programming in C++, we often use compilers in order to convert our high-level code into binary code which our computer can finally read and interpret. In other words, compilers are used to transform code into binary files (.exe in Windows).

However, they are also responsible for many errors that we come across sometimes. Let’s talk about the undefined reference to std::cout error.

Error

The undefined reference to std::cout error is a common error that occurs when using the C++ Standard Template Library (STL) and is caused by a missing link between the program and the library.

Let’s see an example of this error, suppose we have the following code below:

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello World!";
    return 0;
}

Next, we compile this code using gcc (GNU Compiler Collection) by typing the following command: gcc main.cpp.

However, we will get the:

undefined reference to std::cout error

Why do we get undefined reference to std::cout?

This error is primarily caused because the compiler does not have access to the STL library (from C++). In other words, it cannot find the std::cout function, despite including <iostream> at the code header.

In fact, most undefined reference errors occurr because our compiler is not able to find the appropiate libraries in order to successfully compile our program.

Next, it is important to note that gcc is used to compile C programs which is why it cannot find the C++ libraries. The gcc compiler does not have the necessary C++ libraries in order to successfully compile our code, thus we get the undefined reference to std::cout error.

How to fix the undefined reference to std::cout error?

Fortunately, there is an easy fix for this.

To start, we will use g++ by using the g++ command instead. Unlike gcc, g++ is mainly used to compile C++ programs and it has the necessary C++ libraries (including the iostream module).

Therefore, we can finally compile our code using it.

In order to fix this error and compile our program using g++, we will need to type: g++ main.cpp.

Now, we should have fixed the undefined reference to std::cout error. In other words, our binary file should have the right dependencies to compile and execute the binary file properly.

Keep in mind that, while g++ can compile any .c or .cpp files, it only has access to the C++ library. This means that these files will be interpreted as C++ only.

Conclusion

In conclusion, the undefined reference to std::cout error is a common error that occurs when using the C++ Standard Template Library (STL) and is caused by a missing link between the program and the library.

This error is primarily caused because the compiler does not have access to the STL library (from C++).

The gcc compiler does not have the necessary C++ libraries in order to successfully compile our code, thus we get the undefined reference to std::cout error.

However, this error can be easily fixed by using the g++ compiler instead, which has the necessary C++ libraries and can successfully compile C++ programs. Therefore, when encountering this error, it is recommended to use the g++ command to fix and compile the program properly.