gcc未定义引用`std :: ios_base :: Init :: Init()' [英] gcc undefined reference to `std::ios_base::Init::Init()'

查看:2145
本文介绍了gcc未定义引用`std :: ios_base :: Init :: Init()'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写boost测试安装是否成功演示

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;
}

编译错误

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()':


推荐答案

如果使用 gcc 而不是 g ++ ,C ++库不会自动链接。这是来自 man g ++

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


但是,使用gcc不会添加C ++库。 g ++是一个
程序,它调用GCC并自动指定与
C ++库的链接。它将.c,.h和.i文件视为C ++源文件
,而不是C源文件,除非使用-x。这个程序也是
有用的预编译的C头文件扩展名为.h,在C ++编译中使用
。在许多系统上,g ++也安装了
名称c ++。

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

正如其他人所说,使用 g ++ 或在调用结束时链接 -lstdc ++ 。像 gcc main.cpp -lstdc ++

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

这篇关于gcc未定义引用`std :: ios_base :: Init :: Init()'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆