0

I don't have formal VS training, and I usually use it to program simple tools for my research. (I'm a faculty member).

I'm currently working on a C++ library for Python using SWIG, so I followed the steps suggested in How to create a DLL with SWIG from Visual Studio 2010?

Step no. 25 says "You can't build the Debug version unless you build a debug version of Python itself", but I thought one should be able to build a debug version of the C++ stuff by writing a main that uses the library from C++ itself, without touching Python or involving Python at all. (Please let me know if I'm wrong.)

A while ago I tried creating two projects in one solution (one for the library, one for a testing app), but I wasn't quite convinced with the result, so I thought it was time to try configurations. I modified the Debug config for my SWIG project following the suggestions in Redifining C/C++ entry point in Microsoft Visual Studio 2015 and the comments (changed configuration type, extension, and entry point, and added additional dependencies vcruntimed.lib and ucrtd.lib, also excluded from build the .i and the _wrap.cxx files).

The project compiles and runs, but the methods/functions in the standard <random> C++ library are returning non-random numbers. Update/clarification: In the following code,

std::normal_distribution<double> rand::distn(0, 1);
std::uniform_real_distribution<double> rand::distu(0, 1);
std::mt19937_64 rand::generator;

void rand::init() {
    generator.seed((unsigned long)time(NULL));
}

double rand::u01()
{
    return distu(generator);
}

the function u01() returns 0.0 always, while when calling it from Python it works as expected.

I checked the code and the generator is being seeded correctly. Also the library is still working fine from Python, so I tend to think this is not a coding but a configuration issue.

I know this would make a better question if I posted a minimal working example, but before investing time (which I think I don't have) on it I was wandering if there is something obvious I'm missing, that a more knowledgeable VS user could easily spot. Please don't get me wrong, if I'm mistaken and the answer is not so apparent, I'll really try to make the time.

Thanks in advance.

Rafael
  • 129
  • 5
  • Are you saying you get the same sequence of "random" numbers every time? This is probably due to mixing C runtime libraries...setting the seed in one version of the library and generating the random numbers in another. This is the reason for the "build the debug version of Python" comment in my "How to" answer. You *can* build a non-optimized DLL with symbols that uses the release version of the C runtime to match Python and call it the "Debug" version if you want, however, although recent Pythons now come with the debug version as an install option. – Mark Tolonen Jan 21 '19 at 18:41
  • @MarkTolonen No, sorry. I didn't know how to put it. Specifically I get all zeroes from a `std::uniform_real_distribution rand::distu(0, 1);` – Rafael Jan 21 '19 at 18:44
  • 1
    Please provide a [mcve], then. – Mark Tolonen Jan 21 '19 at 18:48
  • @MarkTolonen, thanks, I'll take that as a "there is nothing obvious or easy to spot" then. But just to be sure, could you spare a couple of minutes and see if my last two [revisions](https://stackoverflow.com/posts/54291974/revisions) do give any new clue to you? I'd really appreciate that. – Rafael Jan 21 '19 at 19:14

0 Answers0