sunysen
sunysen

Reputation: 2351

gcc undefined reference to `std::ios_base::Init::Init()'

Write a boost test whether the installation was successful demo

#include<iostream>
#include<boost/lexical_cast.hpp>
int main(){
    int a = boost::lexical_cast<int>("123456");
    std::cout << a <<std::endl;
    return 0;
}

Compile error

test.cpp:(.text+0x24): undefined reference to `std::cout'
test.cpp:(.text+0x29): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(int)'
test.cpp:(.text+0x31): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
test.cpp:(.text+0x39): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
/tmp/ccG8Wb2k.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x61): undefined reference to `std::ios_base::Init::Init()'
test.cpp:(.text+0x66): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccG8Wb2k.o: In function `std::exception::exception()':

Upvotes: 3

Views: 18199

Answers (3)

user3920237
user3920237

Reputation:

If you use gcc instead of g++, the C++ library is not automatically linked. This is from man g++:

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. It treats .c, .h and .i files as C++ source files instead of C source files unless -x is used. This program is also useful when precompiling a C header file with a .h extension for use in C++ compilations. On many systems, g++ is also installed with the name c++.

As others have stated, either use g++ directly or link -lstdc++ at the end of your invocation. Something like gcc main.cpp -lstdc++.

Upvotes: 8

Martin Zamora
Martin Zamora

Reputation: 1

Use this: g++ fileName.cpp -o Filename That will output the file to be run. I hope this will help you. Regards Martin Z

Upvotes: -1

Paul Evans
Paul Evans

Reputation: 27567

This compiles and runs no problem with g++ 4.8.1. Outputs:

123456

Upvotes: 0

Related Questions