c++ - MPFR:未定义引用|安装不正确?

标签 c++ g++ shared-libraries ubuntu-20.04 mpfr

我正在尝试使用mpfr Ubuntu 20.04 上的库。

我已经安装了它:

sudo apt-get install -y libmpfr-dev libmpfr6

我有一个简单的测试代码(从某处的教程复制):

#include <iostream>
#include <mpfr.h>

int main() {
    mpfr_t s, t;
    mpfr_init2(s, 2000);
    mpfr_set_d(s, 22, MPFR_RNDD);
    mpfr_init2(t, 2000);
    mpfr_set_d(t, 7, MPFR_RNDD);
    mpfr_div(s, s, t, MPFR_RNDD);
    mpfr_out_str(stdout, 10, 0, s, MPFR_RNDD);
    std::cout << std::endl;
    mpfr_clear(s);
    mpfr_clear(t);
    mpfr_free_cache();
}

它看起来像C代码,但这只是一个玩具示例。在实际程序中,它将是 C++ 代码,因此我使用 cpp 扩展和 g++ 编译器。

我编译它:

g++ -O0 -Wall --std=c++14 -L/usr/lib -lmpfr -lgmp -o test test.cpp

我收到错误,就好像库定义文件丢失一样......

/usr/bin/ld: /tmp/cc1XbFhH.o: in function `main':
test.cpp:(.text+0x28): undefined reference to `mpfr_init2'
/usr/bin/ld: test.cpp:(.text+0x45): undefined reference to `mpfr_set_d'
/usr/bin/ld: test.cpp:(.text+0x56): undefined reference to `mpfr_init2'
/usr/bin/ld: test.cpp:(.text+0x73): undefined reference to `mpfr_set_d'
/usr/bin/ld: test.cpp:(.text+0x8c): undefined reference to `mpfr_div'
/usr/bin/ld: test.cpp:(.text+0xb2): undefined reference to `__gmpfr_out_str'
/usr/bin/ld: test.cpp:(.text+0xd4): undefined reference to `mpfr_clear'
/usr/bin/ld: test.cpp:(.text+0xe0): undefined reference to `mpfr_clear'
/usr/bin/ld: test.cpp:(.text+0xe5): undefined reference to `mpfr_free_cache'
collect2: error: ld returned 1 exit status

我做错了什么吗?我知道图书馆在那里:

$ dpkg -L libmpfr6
/.
/usr
/usr/lib
/usr/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu/libmpfr.so.6.0.2
/usr/share
/usr/share/doc
/usr/share/doc/libmpfr6
/usr/share/doc/libmpfr6/AUTHORS
/usr/share/doc/libmpfr6/BUGS
/usr/share/doc/libmpfr6/NEWS.gz
/usr/share/doc/libmpfr6/README
/usr/share/doc/libmpfr6/TODO.gz
/usr/share/doc/libmpfr6/changelog.Debian.gz
/usr/share/doc/libmpfr6/copyright
/usr/lib/x86_64-linux-gnu/libmpfr.so.6
$ dpkg -L libmpfr-dev
/.
/usr
/usr/include
/usr/include/mpf2mpfr.h
/usr/include/mpfr.h
/usr/lib
/usr/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu/libmpfr.a
/usr/lib/x86_64-linux-gnu/pkgconfig
/usr/lib/x86_64-linux-gnu/pkgconfig/mpfr.pc
/usr/share
/usr/share/doc
/usr/share/doc/libmpfr-dev
/usr/share/doc/libmpfr-dev/copyright
/usr/lib/x86_64-linux-gnu/libmpfr.so
/usr/share/doc/libmpfr-dev/AUTHORS
/usr/share/doc/libmpfr-dev/BUGS
/usr/share/doc/libmpfr-dev/NEWS.gz
/usr/share/doc/libmpfr-dev/README
/usr/share/doc/libmpfr-dev/TODO.gz
/usr/share/doc/libmpfr-dev/changelog.Debian.gz
/usr/share/doc/libmpfr-dev/changelog.gz

最佳答案

问题出在编译命令行上。参数的顺序很重要:依赖于其他事物的事物必须位于它们所依赖的事物之前。所以如果你使用这个命令

g++ -O0 -Wall --std=c++14 -L/usr/lib -o asd asd.cpp -lmpfr -lgmp

编译将会成功,因为库位于需要它们的源代码之后。

关于c++ - MPFR:未定义引用|安装不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65448452/

相关文章:

c++ - stm32f4 g++ 错误涉及newlib _kill_r,_kill, _getpid_r, _getpid,

c++ - 如何从共享库中删除 *ALL* 未使用的符号?

c++ - 将包含指针的 STL::vector 转换为包含常量指针的 STL::vector

c++ - 用于 Python 的 C++ 库的 SWIG 包装器 - 子模块

c++ - g++模板问题

python - 如何配置 VS Code 以便能够进入调试 Python 脚本时加载的共享库 (.so)?

android - 如何缩小共享库的大小?

c++ - QWindow 提示不起作用

c++ - 删除指向不完整类型的指针 'Point' ;没有调用析构函数

c++ - 在 C++ 中为 HashMap 编写有效的复制构造函数