C++ 关于编译出现“undefined reference to `std::cout‘“的问题

1、问题概述

        在使用gcc编译c++代码时会出现undefined reference to `std::cout',如编译如下代码:

#include<iostream>
using namespace std;
int main()
{
    cout<<"Hello world!";
    return 0;
}

然而,gcc下编译出现的问题是:

root@LAPTOP-LUU3AAQE:/mnt/d/C_Code/cpp_code# gcc 1.cpp -o main -lm
/tmp/ccTCBs3r.o: In function `main':
1.cpp:(.text+0xe): undefined reference to `std::cout'
1.cpp:(.text+0x13): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/ccTCBs3r.o: In function `__static_initialization_and_destruction_0(int, int)':
1.cpp:(.text+0x43): undefined reference to `std::ios_base::Init::Init()'
1.cpp:(.text+0x58): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status

2、解决方法

使用g++编译,g++是专门针对c++文件编译的,如:

root@LAPTOP-LUU3AAQE:/mnt/d/C_Code/cpp_code# g++ 1.cpp -o main 
root@LAPTOP-LUU3AAQE:/mnt/d/C_Code/cpp_code# ./main
Hello world!root@LAPTOP-LUU3AAQE:/mnt/d/C_Code/cpp_code#

这样用g++编译就通过了。

总结:在linux系统下编译c++代码(*.cpp文件)使用g++。

 

  • 21
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值