<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<span style="font-family:Calibri, Arial, Helvetica, sans-serif;font-size:12pt;color:rgb(0, 0, 0);background-color:rgb(255, 255, 255)" class="elementToProof">Peter,</span>
<div style="font-family:Calibri, Arial, Helvetica, sans-serif;font-size:12pt;color:rgb(0, 0, 0);background-color:rgb(255, 255, 255)">
<br>
</div>
<div style="font-family:Calibri, Arial, Helvetica, sans-serif;font-size:12pt;color:rgb(0, 0, 0);background-color:rgb(255, 255, 255)">
Thank you for considering this admittedly somewhat obscure error.</div>
<div style="font-family:Calibri, Arial, Helvetica, sans-serif;font-size:12pt;color:rgb(0, 0, 0);background-color:rgb(255, 255, 255)">
<br>
</div>
<div class="elementToProof"><span style="font-family:Calibri, Arial, Helvetica, sans-serif;font-size:12pt;color:rgb(0, 0, 0);background-color:rgb(255, 255, 255)">I have updated the gist to include a call to the library. The error is unchanged.<br>
</span></div>
<div class="elementToProof"><span style="font-family:Calibri, Arial, Helvetica, sans-serif;font-size:12pt;color:rgb(0, 0, 0);background-color:rgb(255, 255, 255)"><br>
</span></div>
<div class="elementToProof"><span style="font-family:Calibri, Arial, Helvetica, sans-serif;font-size:12pt;color:rgb(0, 0, 0);background-color:rgb(255, 255, 255)">As to the why: the rmath library includes a wide range of non-uniform random numbers, however the
 provided uniform random number generator is of poor quality. Using static linking, it is possible to over-ride (or shadow) those two C functions to provide a user-defined uniform random number generator (see my initial post for a URL with details).<br>
</span></div>
<div class="elementToProof"><span style="font-family:Calibri, Arial, Helvetica, sans-serif;font-size:12pt;color:rgb(0, 0, 0);background-color:rgb(255, 255, 255)"><br>
</span></div>
<div class="elementToProof"><span style="font-family:Calibri, Arial, Helvetica, sans-serif;font-size:12pt;color:rgb(0, 0, 0);background-color:rgb(255, 255, 255)">A further question is why one would want non-uniform random numbers in Mercury. I am gradually
 writing code for doing discrete event simulations in Mercury -- but this is, as much as anything, a distraction from grant writing:).<br>
</span></div>
<div class="elementToProof"><span style="font-family:Calibri, Arial, Helvetica, sans-serif;font-size:12pt;color:rgb(0, 0, 0);background-color:rgb(255, 255, 255)"><br>
</span></div>
<div class="elementToProof"><span style="font-family:Calibri, Arial, Helvetica, sans-serif;font-size:12pt;color:rgb(0, 0, 0);background-color:rgb(255, 255, 255)">Sincerely, Mark.<br>
</span></div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Peter Wang <novalazy@gmail.com><br>
<b>Sent:</b> 13 February 2023 01:14<br>
<b>To:</b> Mark Clements <mark.clements@ki.se><br>
<b>Cc:</b> users <users@lists.mercurylang.org><br>
<b>Subject:</b> Re: [m-users.] Error with static linking for the rmath library</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">[You don't often get email from novalazy@gmail.com. Learn why this is important at
<a href="https://aka.ms/LearnAboutSenderIdentification">https://aka.ms/LearnAboutSenderIdentification</a> ]<br>
<br>
On Sat, 11 Feb 2023 11:30:30 +0000 Mark Clements <mark.clements@ki.se> wrote:<br>
> For the rmath library in Mercury (<a href="https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fmclements%2Fmercury-rmath&data=05%7C01%7Cmark.clements%40ki.se%7C89726bfe110a4f93222d08db0d5748ca%7Cbff7eef1cf4b4f32be3da1dda043c05d%7C0%7C0%7C638118440755417933%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=Y9HAeIxrHvPRDwwa%2BOB4YIfkLNSpFjgJb%2BzqYwf6qXs%3D&reserved=0">https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fmclements%2Fmercury-rmath&data=05%7C01%7Cmark.clements%40ki.se%7C89726bfe110a4f93222d08db0d5748ca%7Cbff7eef1cf4b4f32be3da1dda043c05d%7C0%7C0%7C638118440755417933%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=Y9HAeIxrHvPRDwwa%2BOB4YIfkLNSpFjgJb%2BzqYwf6qXs%3D&reserved=0</a>),
 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
<a href="https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Frstudio.github.io%2Fr-manuals%2Fr-admin%2FThe-standalone-Rmath-library.html&data=05%7C01%7Cmark.clements%40ki.se%7C89726bfe110a4f93222d08db0d5748ca%7Cbff7eef1cf4b4f32be3da1dda043c05d%7C0%7C0%7C638118440755417933%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=9p38pNc2FOO1IUTZMQJvdaxu7dHNKLRLicw1%2BdJrraw%3D&reserved=0">
https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Frstudio.github.io%2Fr-manuals%2Fr-admin%2FThe-standalone-Rmath-library.html&data=05%7C01%7Cmark.clements%40ki.se%7C89726bfe110a4f93222d08db0d5748ca%7Cbff7eef1cf4b4f32be3da1dda043c05d%7C0%7C0%7C638118440755417933%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=9p38pNc2FOO1IUTZMQJvdaxu7dHNKLRLicw1%2BdJrraw%3D&reserved=0</a>.<br>
><br>
> 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
<a href="https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgist.github.com%2Fmclements%2F68aa486f6ef4cfc9634076dad357e483&data=05%7C01%7Cmark.clements%40ki.se%7C89726bfe110a4f93222d08db0d5748ca%7Cbff7eef1cf4b4f32be3da1dda043c05d%7C0%7C0%7C638118440755417933%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=sK7vCrngePILn8a7lmnFilUpyZGCIv%2BUdb%2BpB1mR28M%3D&reserved=0)">
https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgist.github.com%2Fmclements%2F68aa486f6ef4cfc9634076dad357e483&data=05%7C01%7Cmark.clements%40ki.se%7C89726bfe110a4f93222d08db0d5748ca%7Cbff7eef1cf4b4f32be3da1dda043c05d%7C0%7C0%7C638118440755417933%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=sK7vCrngePILn8a7lmnFilUpyZGCIv%2BUdb%2BpB1mR28M%3D&reserved=0)</a>.
 I then get a linking error (make minimal_fails):<br>
><br>
> /usr/bin/ld: /usr/lib/libRmath.a(std_unif.o): in function `set_seed':<br>
> (.text+0x0): multiple definition of `set_seed'; Mercury/os/minimal_fails.o:minimal_fails.c:(.text+0xb0): first defined here<br>
> /usr/bin/ld: /usr/lib/libRmath.a(std_unif.o): in function `unif_rand':<br>
> (.text+0x40): multiple definition of `unif_rand'; Mercury/os/minimal_fails.o:minimal_fails.c:(.text+0xc0): first defined here<br>
><br>
> If I move those functions to the implementation section, then the linking is okay (make minimal_ok).<br>
><br>
> Can anyone suggest why this fails and then works?<br>
<br>
Your minimal_ok.m doesn't use dwilcox anywhere, so the code for it is<br>
completely missing from the minimal_ok.c file. The linker probably has<br>
no need to pull in librmath at all (or a specific .o file in the archive),<br>
thus avoiding the multiple definition error.<br>
<br>
Why are you defining set_seed and unif_rand anyway? Multiple definitions<br>
of the same symbol name are not normally allowed.<br>
<br>
Peter<br>
</div>
</span></font></div>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Generator" content="Cocoa HTML Writer">
<meta name="CocoaVersion" content="1561.6">
<style type="text/css">
    p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 14.0px; font: 12.0px Times; color: #000000; -webkit-text-stroke: #000000; min-height: 14.0px}
    p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 14.0px; font: 12.0px Times; color: #000000; -webkit-text-stroke: #000000}
    span.s1 {font-kerning: none}
    span.s2 {text-decoration: underline ; font-kerning: none; color: #0000ee; -webkit-text-stroke: 0px #0000ee}
  </style>
<p class="p1"><span class="s1"></span><br>
</p>
<p class="p1"><span class="s1"></span><br>
</p>
<p class="p2"><span class="s1"><i>När du skickar e-post till Karolinska Institutet (KI) innebär detta att KI kommer att behandla dina personuppgifter.
</i><a href="https://ki.se/medarbetare/integritetsskyddspolicy"><span class="s2">Här finns information om hur KI behandlar personuppgifter</span></a>.<span class="Apple-converted-space"> </span></span></p>
<p class="p1"><span class="s1"></span><br>
</p>
<p class="p2"><span class="s1"><i>Sending email to Karolinska Institutet (KI) will result in KI processing your personal data.</i>
<a href="https://ki.se/en/staff/data-protection-policy"><span class="s2">You can read more about KI’s processing of personal data here</span></a>.<span class="Apple-converted-space"> </span></span></p>
</body>
</html>