$ 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/