Question

I am using MTRand (the Mersenne Twister random number generator from http://www.bedaux.net/mtrand/) inside a class that I have defined. And when I try to compile I get an unexpected error that I can't decode. I'm a novice c++ programmer, so any help will go a long way...

Here is the relevant portion of my code:

#include<iostream>
#include<vector>
#include<deque>
#include<cmath>
#include "./mtrand/mtrand.h"


using namespace std;

class mp{
  long double store;
  deque< vector<long double> > stack;
  long double boundary;
  long double dt;
  long double time;
  MTRand_open random;
  long int random_seed;



public:
  void initialize(long int, long double, long double);
  long double get_state(); // return the state at position int
  void update();
  friend long double A(mp*);
  friend long double D(mp*);
  long double normal();
  vector <long double> viewCurrent(); 


};

There is then a function, which, if called, sets a seed for the random number generator

void mp::initialize(long int rand_seed_input, long double bdry_in, long double dt_in){


  time = 0;
  dt = dt_in;

  random_seed = rand_seed_input;

  random.seed(random_seed);

  boundary = bdry_in; 
}

I just want to test if this compiles, so I create a main function that does precisely nothing:

int main(){
return 0;
}

On compile time, I get an error

Undefined symbols:
  "MTRand_int32::seed(unsigned long)", referenced from:
      mp::initialize(int, long, long double, long double)in ccJylsHh.o
  "MTRand_int32::p", referenced from:
      MTRand_int32::rand_int32()       in ccJylsHh.o
      MTRand_int32::rand_int32()       in ccJylsHh.o
      MTRand_int32::rand_int32()       in ccJylsHh.o
  "MTRand_int32::state", referenced from:
      MTRand_int32::rand_int32()       in ccJylsHh.o
  "MTRand_int32::gen_state()", referenced from:
      MTRand_int32::rand_int32()       in ccJylsHh.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

I am not sure what this error means, and how it should be removed.

From what I understand is that MTRand can't figure out how to initialize the seed...but there is a default seeding within the class MTRand so I can't see what the problem is.

Was it helpful?

Solution

In addition to including mtrand.h header in your code with a correct file path, you should probably add mtrand.cpp to your project so that it compiles along with other .cpp files of your program.

If the library you use does not provide any pre-compiled binaries, like .lib, .dll, or .a files, then yes, you have to compile the library's source code by yourself (Not much of work right?) in order to make the linker happy. But if the library does go accompanied with such pre-compiled binaries, then you should tell the linker what files it needs to link against to be able to find how the declarations in the library's header files are actually implemented, because linker has no idea otherwise. How you actually link a pre-compiled binary depends on your development environment. Of course, you should include the header files in both cases to tell compiler what does MTRand_int32 and other new identifiers mean.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top