$ apt-cache show libgmp10
Package: libgmp10
...
Version: 2:5.0.2+dfsg-2ubuntu2
TEST.CPP:C++ GMP库ostream运算符<<编译但不链接?
#include <gmpxx.h>
#include <iostream>
using namespace std;
int main()
{
mpz_class x = 42;
cout << x;
}
编译:
$ g++ -c test.cpp -o test.o
$
OK
链接:
$ 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)
。是什么赋予了?
有疑问时只需验证与pkg配置正确的选项(如果LIB使用pkg配置) – user1824407
@ user1824407:'pkg配置libgmp10'没有返回值? –
它比您认为软件包名称与lib-name不同的更常见,有时甚至标志中的名称也不相同。我建议'pkg-config --list-all | grep -i gmp'从结果中选择'rightName'并从结果中选择'pkg-config --cflags rightName',为libs – user1824407