242

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 Wheezy.

shauryachats
  • 8,610
  • 4
  • 32
  • 47
D1X
  • 3,854
  • 5
  • 19
  • 32

4 Answers4

342

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
shauryachats
  • 8,610
  • 4
  • 32
  • 47
  • 19
    It *can* be used to compile C++ code, the thing is that it doesn't *link* with the C++ library. `gcc` will work just fine if you just add `-lstdc++`. – Some programmer dude Jan 30 '15 at 13:24
  • 6
    Please always include `-Wall` when giving gcc/g++ command line examples - it helps to get noobs into good habits at an early stage and saves everyone time time further down the road. ;-) – Paul R Jan 30 '15 at 13:29
  • You might want to toss in `-Wextra` as well for good measure. I am not sure about the edit though, because `g++` **is** to be preferred over `gcc -lstdc++`... – DevSolar Jan 30 '15 at 13:30
  • since we are here, why not do it right to the end: add a `-Werror` – bolov Jan 30 '15 at 13:30
  • 4
    Since when is iostreams and `std::cout` part of the Standard Template Library? – T.C. Jan 30 '15 at 13:45
  • @T.C. Since SGI's STL became conflated with the standard library? – sjdowling Jan 30 '15 at 13:48
  • 1
    Why is -Werror needed? I have revised the documentation and if I understand well will make the warnings errors and will make my projects less easy to compile. – D1X Jan 31 '15 at 15:02
  • 9
    @D1X: Because there's a nasty habit among programmers to *ignore* warnings. Virtually everything that `-Wall` and even `-Wextra` warn about is either a very real problem, or sloppy coding that can very easily be fixed. The message here is to get into a habit where you consider compiler warnings a helpful pointer to where your code could be improved, instead of a nuisance. There are *hundreds* of questions here on SO that wouldn't have been necessary in the first place if the OP had used `-Wall -Wextra`. `-Werror` is simply reinforcing that. – DevSolar Feb 02 '15 at 11:54
  • @shauryachats, re:"g++ should be preferred over gcc" Is the exception that you may prefer gcc because it compiles .c files as C and .cpp files as C++, whereas g++ will compile both file types as C++? So, you might have institutional conventions or practical reasons (maybe speed?) for preferring your .c files to compile as C rather than as C++? – Kaleb Coberly May 21 '21 at 20:18
56

Yes, using g++ command worked for me:

g++ my_source_code.cpp
shauryachats
  • 8,610
  • 4
  • 32
  • 47
A B
  • 660
  • 5
  • 4
4

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
iggy12345
  • 794
  • 5
  • 15
4

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.

losnihciL
  • 61
  • 4