C++ GMP 库 ostream 运算符<<编译但不链接?
C++ GMP Library ostream operator<< compiles but doesn't link?
$ 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
在链接时找不到operator<<(ostream&, mpz_class)
。到底发生了什么事?
您需要链接c++库以及C库:
g++ -c test.cpp -o test.o -lgmpxx -lgmp
# ^^^^^^^
除了Kerrek SB的答案之外,我还可以从我的实验中确认两件事:
-
-lgmp
和-lgmpxx
的内含物相同,因为g++ -M main.cpp -lgmp
的输出与g++ -M main.cpp -lgmpxx
的输出相同 - g++/gcc为这两个标志使用不同的库,因为
g++ main.cpp -Wl,-t -lgmp
不同于g++ main.cpp -Wl,-t -lgmpxx
,只有最后一个有效
我没有GMP的经验,但由于这些目录是硬编码在gcc配置中,至少在这个Ubuntu构建中,你需要使gcc输出更详细,并使用很多耐心来解析所有输出,也许你会发现真正的原因。
相关文章:
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- CMake-按正确顺序将项目与C运行时对象文件链接
- 从链接列表c++中删除一个项目
- 有根的二进制搜索树.保留与其父级的链接
- 读取文件的最后一行并输入到链接列表时出错
- 静态数据成员的问题-修复链接错误会导致编译器错误
- node-gyp 在 macOS 上未正确链接库
- 基于boost的程序的静态链接——zlib问题
- 无法链接 CMake 中的本地库
- 内联函数中具有内部链接的全局变量
- 链接阶段在Ubuntu上失败,但在MacOS上失败
- 使用gcc从静态链接的文件中查找可选符号
- 我可以将一个用clang c++11编译的对象与另一个用c++17编译的对象链接起来吗
- 将--whole archive链接器选项与CMake和具有其他库依赖项的库一起使用
- 下面是我为检测链接列表中的循环而制作的代码
- 有了gcc,是否可以链接库,但前提是它存在
- 使用C链接在函数内部创建C++模板
- Visual Studio mkl_link_tool.exe链接错误
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- 在尝试超载&lt;&lt;时链接错误2005和1169操作员