C++ GMP 库 ostream operator<< 编译但不链接?

标签 c++ linux gmp

$ apt-cache show libgmp10
Package: libgmp10
...
Version: 2:5.0.2+dfsg-2ubuntu2

测试.cpp:

#include <gmpxx.h>
#include <iostream>

using namespace std;

int main()
{
    mpz_class x = 42;

    cout << x;
}

编译:

$ g++ -c test.cpp -o test.o
$

好的

链接:

$ g++ test.o -lgmp
test.o: In function `std::ostream& operator<<
    <__mpz_struct [1]>(std::ostream&,
         __gmp_expr<__mpz_struct [1],
              __mpz_struct [1]> const&)':

test.cpp:(.text._ZlsIA1_12__mpz_structERSoS2_RK10__gmp_exprIT_S4_E[_ZlsIA1_12__mpz_structERSoS2_RK10__gmp_exprIT_S4_E]+0x2a):

undefined reference to `operator<<(std::ostream&, __mpz_struct const*)'
collect2: error: ld returned 1 exit status

找不到operator<<(ostream&, mpz_class)在链接时。给了什么?

最佳答案

您需要链接 C++ 库和 C 库:

g++ -c test.cpp -o test.o -lgmpxx -lgmp
#                         ^^^^^^^

关于C++ GMP 库 ostream operator<< 编译但不链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14164535/

相关文章:

c++ - 通过 std::function 传递成员函数

linux - 使用分支预测器了解 Spectre 漏洞

c++ - MSYS2 MinGW64 在 Windows 上构建 GMP/MPFR 作为静态库,并将它们链接到用 CL 编译的 MSVC 项目中

python - NesC/GMP undefined symbol

c - 测量 C 代码执行时间 (Linux)

c - GMP mpz_sizeinbase 以 10 为基数返回 9 的大小 2

c++ - 为什么我不能在类中初始化非常量静态成员或静态数组?

c++ - 为数组重载运算符<<

c++ - WNDPROC声明问题,从C转C++

linux - 在 bash 中删除另一个文件中存在的内容