1

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?

jorgen
  • 3,425
  • 4
  • 31
  • 53
  • 1
    Have you tried changing the order you pass the library names to the compiler? – Cameron Jan 14 '15 at 17:01
  • I tried it a bit now, with no results.. Any particular order in mind? I'd rather not try all 120:). The order in the OP is the one from the zkcm README. – jorgen Jan 14 '15 at 17:11
  • 1
    I've seen things fail to link if the dependent libraries of one library were put in the wrong order relative to the library itself (so, just two orders to test). But it seems not to be the case this time. – Cameron Jan 14 '15 at 18:24
  • 1
    If zkcm uses MPFR, then perhaps `-lzkcm` should be put before `-lmpfr`. – vinc17 Jan 15 '15 at 09:03

1 Answers1

1

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).

Community
  • 1
  • 1
vinc17
  • 2,829
  • 17
  • 23