linking to iostream

I'm working on a small openGl program (just a test actually), and need to use standard output (cout) to debug my program. I included iostream (#include <iostream>), and am using namespace std; I'm compiling with:


gcc -o cube main.cpp -lGL -lGLU -lglut

but it's giving me this error:

undefined reference to symbol '_ZNSt8ios_base4InitD1Ev@@GLIBCXX_3.4'
//usr/lib/x86_64-linux-gnu/libstdc++.so.6: error adding symbols: DSO missing from command line

I have build-essential installed, so there shouldn't be a problem linking to iostream.

Any ideas?
Use g++ and not gcc - the latter is for C, the former for both C and C++.
Last edited on
Use g++ and not gcc - the latter is for C, the former for both C and C++.

GCC = GNU Compiler Collections and calls G++ afaik and will compile c or c++. Though I could be wrong
G++ will compile both C and C++, but if you try to compile C++ code with gcc you get (also include g++ after the error):

$ gcc -o maincpp main.cpp
/tmp/ccALsCeT.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*)'
/tmp/ccALsCeT.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

$ g++ -o maincpp main.cpp -std=c++11

$ ./maincpp
Hello world!

Last edited on
You have two different sets of compiler flags enabled :P

Anyways, AFAIK gcc invokes g++ for c++ files as it is just a collection of compilers. Code::blocks uses gcc. You could also use the gcc compiler flag -lstdc++ to tell it that it is a c++ file.
giblit, do a bit more research - this is a common issue caused by different linker arguments passed by gcc than by g++
> Use g++ and not gcc - the latter is for C, the former for both C and C++.
1
2
3
int main(){
   int class = 42;
}
$ gcc bar.c
$ echo $?
0
$ g++ bar.c
bar.c: In function ‘int main()’:
bar.c:2:2: error: expected primary-expression before ‘int’



man gcc wrote:
Options Controlling the Kind of Output
Compilation can involve up to four stages: preprocessing, compilation proper,
assembly and linking, always in that order. (...)

For any given input file, the file name suffix determines what kind of
compilation is done:
I suppose that the last paragraph refer to compilation proper
You may use the `gcc' command for compiling c++ source code.


Edit: here it is
Compiling C++ Programs
C++ source files conventionally use one of the suffixes .C, .cc, .cpp, .CPP,
.c++, .cp, or .cxx; C++ header files often use .hh, .hpp, .H, or (for shared
template code) .tcc; and preprocessed C++ files use the suffix .ii. GCC
recognizes files with these names and compiles them as C++ programs even if you
call the compiler the same way as for compiling C programs (usually with the
name gcc).

However, the use of gcc does not add the C++ library. g++ is a program that
calls GCC and automatically specifies linking against the C++ library.
Last edited on
ne555 and BHX, the program is compiling fine, one needs to make objects and then try to link them to get the error.

Given a main.cpp:
1
2
3
4
5
6
#include <iostream>

int main( void )
{
  std::cout << "Hello\n";
}


and makefile:
1
2
3
4
5
6
7
8
9
10
11
12
sources=main.cpp
objects=$(sources:%.cpp=%.o)
exec=prog

all: $(exec)

$(exec):$(objects)
        gcc -o $@ $^

.PHONY: clean
clean:
        rm $(objects) $(exec)


The output:
1
2
3
4
5
$ make
g++    -c -o main.o main.cpp
gcc -o prog main.o
main.o: In function `main':
main.cpp:(.text+0xa): undefined reference to `std::cout'


Gcc is good at knowing to use g++ for a c++ source file, but it defaults to cc to link the objects. ( or maybe it is make that knows the difference, but at least I get credit for recreating the problem :) )

A correct makefile looks as such:
1
2
3
4
5
6
7
8
9
10
11
12
sources=main.cpp
objects=$(sources:%.cpp=%.o)
exec=prog

all: $(exec)

$(exec):$(objects)
        $(CXX) -o $@ $^

.PHONY: clean
clean:
        rm $(objects) $(exec)

Last edited on
I was responding at LB claim of using g++ for C code


> Gcc is good at knowing to use g++ for a c++ source file, but it defaults to cc to link the objects.
> ( or maybe it is make that knows the difference
¿why did I ever bother to post the man pages?

Edit: Your makefile is using an implicit rule for compilation
Last edited on
Topic archived. No new replies allowed.