我正在编写一个LLVM脚本引擎,JIT用自定义语言编写脚本代码.我的问题是我无法调用外部函数(即使C99 erf()函数失败).
例如,如果我extern"C"erf函数,
extern "C" double erft(double x){
return erf(x);
}
Run Code Online (Sandbox Code Playgroud)
并创建一个外部链接功能
std::vector<const Type*> Double1(1,Type::getDoubleTy(getGlobalContext()));
FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),Double1,false);
Function *erft = Function::Create(FT,Function::ExternalLinkage,"erft",TheModule);
Run Code Online (Sandbox Code Playgroud)
使用erft(0.0)运行脚本时出现以下错误消息:
LLVM错误:程序使用的外部功能'erft'无法解决!
手动执行映射,
void ExecutionEngine::addGlobalMapping( const GlobalValue * erfF, void * erft);
Run Code Online (Sandbox Code Playgroud)
会得到以下错误:
在类之外的`void llvm :: ExecutionEngine :: addGlobalMapping(const llvm :: GlobalValue*,void*)'的声明不是定义
显然我做错了.任何帮助将非常感激
我想通过使用不同的种子数重置随机序列.运行此测试代码时:
boost::mt19937 gener(1);
boost::normal_distribution<> normal(0,1);
boost::variate_generator<boost::mt19937&,boost::normal_distribution<> > rng(gener, normal);
cout << rng() << endl;
cout << rng() << endl;
cout << rng() << endl;
gener.seed(2);
cout << rng() << endl;
cout << rng() << endl;
gener.seed(1);
cout << rng() << endl;
gener.seed(2);
cout << rng() << endl;
gener.seed(3);
cout << rng() << endl;
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
# seed(1) via constructor
-2.971829031
1.706951063
-0.430498971
# seed(2)
-2.282022417
-0.5887503675
# seed(1)
0.2504171986
# seed(2)
-0.5887503675
# seed(3)
0.2504171986
Run Code Online (Sandbox Code Playgroud)
显然我做错了.我怎么能克服这个问题?