C GMP库ostream运算符<<编译但不链接?

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

TEST.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

它在链接时找不到运算符<<(ostream&,mpz_class).是什么赋予了?

解决方法:

您需要链接C库以及C库:

g++ -c test.cpp -o test.o -lgmpxx -lgmp
#                         ^^^^^^^
上一篇:python – 具有mpz / mpfr值的numpy数组


下一篇:c – 使用Xcode在MacOS X上安装GMP