对 &gt; 的未定义引用`std::ostream::operator<<(unsigned long)' [英] undefined reference to &gt; `std::ostream::operator&lt;&lt;(unsigned long)&#39;

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

问题描述

我有以下代码:

#include 使用命名空间标准;整数主要的 (){ostringstream oss;无符号长 k = 5;oss<

使用以下参数编译:

<块引用>

/usr/local/gcc-10.2.0/bin/g++ -I/usr/local/gcc-10.2.0/include-L/usr/local/gcc-10.2.0/lib64 -Wl,-rpath,/usr/local/gcc-10.2.0/lib64 -lstdc++ b.cpp

得到以下输出:

<块引用>

/tmp/cclRSXGV.o: 在函数 main': b.cpp:(.text+0x35): 对 std::ostream::operator<<(unsigned long) 的未定义引用'

<块引用>

collect2:错误:ld 返回 1 个退出状态

需要什么才能使其正确编译和链接?

使用 GNU gcc 10.2.0.

解决方案

当您指定要链接的库时,顺序很重要.按这个顺序

-lstdc++ b.cpp

libstdc++ 不会解析 b.cpp 中的任何符号.指定库后记:

b.cpp -lstdc++

I have the following code:

#include <sstream>
using namespace std;

int
main ()
{
        ostringstream oss;
        unsigned long k = 5;
        oss << k;
}

Compiled with the following parameters:

/usr/local/gcc-10.2.0/bin/g++ -I/usr/local/gcc-10.2.0/include -L/usr/local/gcc-10.2.0/lib64 -Wl,-rpath,/usr/local/gcc-10.2.0/lib64 -lstdc++ b.cpp

Got the following output:

/tmp/cclRSXGV.o: In function main': b.cpp:(.text+0x35): undefined reference to std::ostream::operator<<(unsigned long)'

collect2: error: ld returned 1 exit status

What is needed to get it to compile and link correctly?

Using GNU gcc 10.2.0.

解决方案

When you specify what libraries to link the order matters. In this order

-lstdc++ b.cpp

libstdc++ will not resolve any symbols in b.cpp. Specify the library afterwords:

b.cpp -lstdc++ 

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

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