maciek
maciek

Reputation: 2117

MPFR: undefined reference | incorrect installation?

I am trying to use mpfr library on Ubuntu 20.04.

I have installed it with:

sudo apt-get install -y libmpfr-dev libmpfr6

And I have a simple test code (copied from a tutorial somewhere):

#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();
}

It looks as C code but this is just a toy example. In the real program it will be a C++ code therefore I use cpp extension and g++ compiler.

I compile it with:

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

And I get errors as if the library definition files were missing...

/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

Am I doing sth wrong? I know the libraries are there:

$ 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

Upvotes: 3

Views: 1407

Answers (1)

Sami Kuhmonen
Sami Kuhmonen

Reputation: 31193

The problem is in the compilation command line. The order of the arguments matters: the things that depend on others must be before the things they depend on. So if you use this command

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

the compilation will succeed since the libraries are after the source code that needs them.

Upvotes: 3

Related Questions