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

Peter Wang novalazy at gmail.com
Mon Feb 13 11:14:29 AEDT 2023


On Sat, 11 Feb 2023 11:30:30 +0000 Mark Clements <mark.clements at ki.se> wrote:
> 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?

Your minimal_ok.m doesn't use dwilcox anywhere, so the code for it is
completely missing from the minimal_ok.c file. The linker probably has
no need to pull in librmath at all (or a specific .o file in the archive),
thus avoiding the multiple definition error.

Why are you defining set_seed and unif_rand anyway? Multiple definitions
of the same symbol name are not normally allowed.

Peter


More information about the users mailing list