[m-users.] Error with static linking for the rmath library

Zoltan Somogyi zoltan.somogyi at runbox.com
Tue Feb 14 01:15:24 AEDT 2023


2023-02-11 22:30 GMT+11:00 "Mark Clements" <mark.clements at ki.se>:
> For the rmath library in Mercury (https://github.com/mclements/mercury-rmath), I am trying to use C code for the Mersenne Twister random number generator to replace the default random number generator. This involves replacing two C functions: double unif_rand(void) and void set_seed(unsigned int, unsigned int); for details, see https://rstudio.github.io/r-manuals/r-admin/The-standalone-Rmath-library.html.
> 
> I have got this working -- _except_ if any of four functions (dwilcox, pwilcox, qwilcox, rwilcox) are included in the interface section (see the gist at https://gist.github.com/mclements/68aa486f6ef4cfc9634076dad357e483). I then get a linking error (make minimal_fails):
> 
> /usr/bin/ld: /usr/lib/libRmath.a(std_unif.o): in function `set_seed':
> (.text+0x0): multiple definition of `set_seed'; Mercury/os/minimal_fails.o:minimal_fails.c:(.text+0xb0): first defined here
> /usr/bin/ld: /usr/lib/libRmath.a(std_unif.o): in function `unif_rand':
> (.text+0x40): multiple definition of `unif_rand'; Mercury/os/minimal_fails.o:minimal_fails.c:(.text+0xc0): first defined here
> 
> If I move those functions to the implementation section, then the linking is okay (make minimal_ok).
> 
> Can anyone suggest why this fails and then works?

The Mercury compiler has a pass that does dead code elimination.
This pass deletes from the compiler's representation of the module
being compiled any procedure that is not reachable from the code of
the exported predicates and functions of the module. (A procedure
is one mode of a predicate or function.) Those deleted procedures
won't show up in the generated target language code.

This criterion of "is it reachable from the interface" means that
an unused predicate that is declared in the interface section
will have its translated code show up in the target language file
(minimal_fails.c), while if its declaration is moved to the implementation
section, its translated code won't show up there (minimal_ok.c).
(In fact, its code won't even be translated.)

Zoltan.


More information about the users mailing list