gpt4 book ai didi

c++ - gcc 未定义对 `std::ios_base::Init::Init()' 的引用

转载 作者:太空狗 更新时间:2023-10-29 20:42:24 25 4
gpt4 key购买 nike

写一个boost测试是否安装成功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++:

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

关于c++ - gcc 未定义对 `std::ios_base::Init::Init()' 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18698059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com