Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
728 views
in Technique[技术] by (71.8m points)

c++ - 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()':
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...