【Bug记录】编译c++程序报错undefined reference to `std::ios_base::Init::Init()‘

描述

在linux下编译c++程序时报错:

/usr/bin/ld: /tmp/ccjbNBGi.o: in function __static_initialization_and_destruction_0(int, int)‘:
/usr/include/c++/9/iostream:74: undefined reference to std::ios_base::Init::Init()
/usr/bin/ld: /usr/include/c++/9/iostream:74: undefined reference to std::ios_base::Init::~Init()’
collect2: error: ld returned 1 exit status
Build finished with error(s).

原因

使用的是gcc编译器,gcc是c语言编译器,默认链接的库是libc.so,要使其链接到c++的库libstdc++.so。

解决方法

方法1 在命令后添加-lstdc++

gcc helloworld.cpp -lstdc++

方法2 在vscode的tasks.json中添加参数

若使用vscode作为IDE,可在tasks.json的“args”中添加链接库参数:

  "-l",
  "stdc++"

在这里插入图片描述

方法3 将编译器改为g++

无论是.c后缀还是.cpp后缀,g++都认为是c++程序,g++可以自动连接c++标准库。

g++ helloworld.cpp -o HelloWorld

或在vscode的tasks.json中将command改为g++的路径。

"command": "/usr/bin/g++",
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值