简体   繁体   English

MTRand在编译时产生错误

[英]MTRand produces an error on compilation

I am using MTRand (the Mersenne Twister random number generator from http://www.bedaux.net/mtrand/ ) inside a class that I have defined. 我在定义的类中使用MTRand(来自http://www.bedaux.net/mtrand/的Mersenne Twister随机数生成器)。 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... 我是C ++的新手程序员,所以任何帮助都会大有帮助...

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. 据我了解,MTRand无法弄清楚如何初始化种子...但是MTRand类中存在默认的种子,所以我看不出问题出在哪里。

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. 除了使用正确的文件路径在代码中包含mtrand.h标头之外,您还应该将mtrand.cpp添加到项目中,以便它与程序的其他.cpp文件一起编译。

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. 如果您使用的库没有提供任何预编译的二进制文件,例如.lib,.dll或.a文件,那么可以,您必须自己编译该库的源代码(工作量不大吗?),以便使链接器开心。 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. 当然,在两种情况下都应包含头文件,以告诉编译器MTRand_int32和其他新标识符的含义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM