Search code examples
c++compiler-errorsstatic-linkingmpfrzkcm

zkcm-library not finding the mpfr-library


I just installed the zkcm library on my kubuntu machine and I'm having trouble compiling c++ code.

I have installed the gmp and mpfr libraries and checked that they work; the code

mpfr_t m1, m2, m3; 
mpfr_init(m1); mpfr_init(m2); mpfr_init(m3); 
mpfr_mul(m1, m2, m3, MPFR_RNDN);

compiles and runs.

I then try to use zkcm; I try compliling the line

zkcm_matrix m;

and get a bunch of errors seemingly about zkcm not finding mpfr; here is the beginning of the output:

/usr/local/lib/libzkcm.a(zkcm_c.o): In function `zkcm_init_ri(zkcm*, double, double)':
/home/jorgen/Downloads/zkcm_lib-0.4.0/srcs/zkcm_c.c:126: undefined reference to `mpfr_inits'
/home/jorgen/Downloads/zkcm_lib-0.4.0/srcs/zkcm_c.c:127: undefined reference to `mpfr_set_d'
/usr/local/lib/libzkcm.a(zkcm_c.o): In function `zkcm_init_ri_str(zkcm*, char const*, char const*)':
/home/jorgen/Downloads/zkcm_lib-0.4.0/srcs/zkcm_c.c:132: undefined reference to `mpfr_inits'
/home/jorgen/Downloads/zkcm_lib-0.4.0/srcs/zkcm_c.c:136: undefined reference to `mpfr_set_str'
/home/jorgen/Downloads/zkcm_lib-0.4.0/srcs/zkcm_c.c:134: undefined reference to `mpfr_set_ui'

I have tried to follow the instructions infrom zkcm; I have the lines

#include "mpfr.h"
#include "zkcm.hpp"

and I compile the code (called test.cpp) using

g++ test.cpp -msse2 -std=c++11 -O2 -lm -lmpfr -lgmp -lgmpxx -lzkcm -o test

Any ideas?


Solution

  • The library order is incorrect: according to the error message, zkcm uses MPFR, so that -lzkcm should be put before -lmpfr (which itself should be put before -lgmp because MPFR uses GMP).

    Otherwise the following happens: If the linker founds a MPFR symbol that is not used by test.cpp (or some dependency), it will drop it. And if such a symbol is used by zkcm, this will yield an error since -lzkcm comes later in the command line. This also explains why you may get errors for some MPFR symbols and not others (and errors may appear and disappear when the test.cpp code and the zkcm code change).

    This should be sufficient to solve the problem here. But look at this answer to "Linker order - GCC" for more general rules (this answer also deals with cyclic dependencies).