Error "undefined reference to 'std::cout'"

C++C++11GccCout

C++ Problem Overview


Shall this be the example:

#include <iostream>
using namespace std;

int main()
{
    cout << "Hola, moondo.\n";
}

It throws the error:

gcc -c main.cpp gcc -o edit main.o  main.o: In function `main':
main.cpp:(.text+0xa): undefined reference to `std::cout'
main.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char,std::char_traits<char> >& std::operator<< <std::char_traits<char>>(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
main.o: In function `__static_initialization_and_destruction_0(int,int)':
main.cpp:(.text+0x3d): undefined reference to `std::ios_base::Init::Init()'
main.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::~Init()' collect2: error: ld
returned 1 exit status make: *** [qs] Error 1

Also, this example:

#include <iostream>

int main()
{
    std::cout << "Hola, moondo.\n";
}

throws the error:

gcc -c main.cpp gcc -o edit main.o  main.o: In function `main':
main.cpp:(.text+0xa): undefined reference to `std::cout'
main.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char,std::char_traits<char> >& std::operator<<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char> >&, char const*)'
main.o: In function `__static_initialization_and_destruction_0(int,int)': main.cpp:(.text+0x3d): undefined reference to `std::ios_base::Init::Init()'
main.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::~Init()' collect2: error: ld
returned 1 exit status make: *** [qs] Error 1

Note: I am using Debian 7 (Wheezy).

C++ Solutions


Solution 1 - C++

Compile the program with:

g++ -Wall -Wextra -Werror -c main.cpp -o main.o
     ^^^^^^^^^^^^^^^^^^^^ <- For listing all warnings when your code is compiled.

as cout is present in the C++ standard library, which would need explicit linking with -lstdc++ when using gcc; g++ links the standard library by default.

With gcc, (g++ should be preferred over gcc)

gcc main.cpp -lstdc++ -o main.o

Solution 2 - C++

Yes, using g++ command worked for me:

g++ my_source_code.cpp

Solution 3 - C++

Assuming code.cpp is the source code, the following will not throw errors:

make code
./code

Here the first command compiles the code and creates an executable with the same name, and the second command runs it. There is no need to specify g++ keyword in this case.

Solution 4 - C++

Makefiles

If you're working with a makefile and you ended up here like me, then this is probably what you're looking or:

If you're using a makefile, then you need to change cc as shown below

my_executable : main.o
    cc -o my_executable main.o

to

CC = g++

my_executable : main.o
    $(CC) -o my_executable main.o

Solution 5 - C++

Adding the following line in your CMake makes gcc link with std and therefore recognize std::cout

target_link_libraries(your_project
        PRIVATE
        -lstdc++
        )

Solution 6 - C++

FWIW, if you want a makefile, here is how you can do either answer by switching the compiler at the top.

# links stdc++ library by default
# CC := g++
# or
CC := cc

all: hello

util.o: util.cc
        $(CC) -c -o util.o  util.cc

main.o: main.cc
        $(CC) -c -o main.o  main.cc

# notice -lstd++ is after the .o files
hello: main.o util.o
        $(CC) -o hello main.o util.o -lstdc++

clean:
        -rm util.o main.o hello

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionD1XView Question on Stackoverflow
Solution 1 - C++shauryachatsView Answer on Stackoverflow
Solution 2 - C++A BView Answer on Stackoverflow
Solution 3 - C++losnihciLView Answer on Stackoverflow
Solution 4 - C++iggy12345View Answer on Stackoverflow
Solution 5 - C++SerendipityView Answer on Stackoverflow
Solution 6 - C++netskinkView Answer on Stackoverflow