C++ MTRand在编译时产生错误

C++ MTRand在编译时产生错误,c++,random,mersenne-twister,C++,Random,Mersenne Twister,我在我定义的类中使用MTRand(来自的Mersenne Twister随机数生成器)。当我试图编译时,我得到了一个无法解码的意外错误。我是一个新手C++程序员,所以任何帮助都会有很长的路…… 以下是我的代码的相关部分: #include<iostream> #include<vector> #include<deque> #include<cmath> #include "./mtrand/mtrand.h" using namespace

我在我定义的类中使用MTRand(来自的Mersenne Twister随机数生成器)。当我试图编译时,我得到了一个无法解码的意外错误。我是一个新手C++程序员,所以任何帮助都会有很长的路…… 以下是我的代码的相关部分:

#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(); 


};
我只是想测试它是否编译,所以我创建了一个完全不执行任何操作的主函数:

int main(){
return 0;
}
在编译时,我得到一个错误

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
我不知道这个错误意味着什么,以及应该如何消除它


据我所知,MTRand无法确定如何初始化种子…但类MTRand中有一个默认种子设定,因此我看不出问题所在。

除了在代码中包含具有正确文件路径的
MTRand.h
头之外,您可能应该将
mtrand.cpp
添加到项目中,以便它与程序的其他.cpp文件一起编译


如果您使用的库没有提供任何预编译的二进制文件,如.lib、.dll或.a文件,那么是的,您必须自己编译库的源代码(不是很多工作吧?),以使链接器满意。但是,如果库确实附带了这些预编译的二进制文件,那么您应该告诉链接器它需要链接哪些文件才能找到库头文件中的声明是如何实际实现的,因为链接器对此一无所知。如何链接预编译的二进制文件取决于您的开发环境。当然,在这两种情况下都应该包含头文件,以告诉编译器MTRand_int32和其他新标识符的含义。

我不确定链接库意味着什么。对于编译,我只需键入
g++nameOfFile.cpp
,就可以得到发布的错误。我还尝试将mtrand的相对路径名替换为类型为
/Users/path/to/mtrand/mtrand.h
的绝对路径,并使用
g++nameOfFile.cpp
再次编译,但仍然得到相同的错误。我尝试编译mtrand附带的测试代码,得到了完全相同的错误,但在我的目录的文件夹中找不到任何.o文件。我非常困惑!是的,这使它编译时没有错误。这是通常做的事情吗?是否同时包含头文件和头文件附带的.cpp文件?@user1544430更新了答案。谢谢!现在我觉得这很有道理。
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