4

I am trying to port some code from linux to windows under cygwin. I find this weird error where 'rand_r is not declared in this scope'. I am using gcc 4.8.1 as x86_64-w64-mingw32 and in it's search path (-v option) it does look in the right directories. The header files are included properly. I am looking for ideas to solve this problem. Is there some fact about cygwin that I am missing and therefore having this problem? Is there a way to check if gcc is in fact touching the required files?

viki.omega9
  • 345
  • 4
  • 12
  • Probably because `rand_r` is a `glibc` function, and at least gcc-mingw doesn't use `glibc` as the C library implementation. – Mats Petersson Aug 27 '13 at 00:05
  • I see that it is not. Do you have a suggestion on which function I should use instead under gcc-mingw? – viki.omega9 Aug 27 '13 at 00:40
  • Also would like to figure out how to detect that the function is missing and provide the implementation calling the replacement only if missing, without having to edit the code for Windows and edit again for Linux; the same code base has to build on both Windows and Linux. – Eric Buist May 17 '23 at 17:25

1 Answers1

4

The rand_r function is considered thread safe, compared to the standard rand function. See man 3 rand_r.

One option is to implement rand_r yourself by wrapping a call to rand. This may or may not be desirable, and as the manual states, rand_r is a fairly weak pseudo-random number generator anyway.

Seeing as you're using C++, why not take a look at the new random number libraries available. They are thread safe, portable, and produce much better random results. This question, while closed as not a real question, still contains some useful information on how to use the library.

If you don't have the C++11 random classes available, Boost also has an implementation.

Community
  • 1
  • 1
Anthony
  • 12,177
  • 9
  • 69
  • 105
  • Thank you. I am going to go ahead and use `rand` until the author of that portion of the code switches to boost. – viki.omega9 Aug 27 '13 at 15:50