我一直希望在我的 R 代码中重用 QuantLib 中的日期和日历函数。由于 RQuantLib 不涵盖所有日历,因此我编译并安装了最新的 QuantLib 版本。但是,我无法运行类似问题中提供的示例。如何配置 Rcpp 以正确编译和使用下面的 C++ 代码?
我尝试使用 Rcpp 函数 sourceCpp("myCode.cpp") 运行以下代码(在文件“myCode.cpp”中):
#include <ql/quantlib.hpp>
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::depends(RQuantLib)]]
// [[Rcpp::export]]
double timesTwo(double x) {
QuantLib::Calendar myCal = QuantLib::UnitedKingdom();
QuantLib::Date newYearsEve(31, QuantLib::Dec, 2008);
QuantLib::Rate zc3mQuote = x;
return zc3mQuote * 2;
}
这会导致以下错误:
ld: warning: directory not found for option '-L/usr/local/lib/gcc/i686-apple-darwin8/4.2.3/x86_64'
ld: warning: directory not found for option '-L/usr/local/lib/x86_64'
ld: warning: directory not found for option '-L/usr/local/lib/gcc/i686-apple-darwin8/4.2.3'
ld: library not found for -lgfortran
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [sourceCpp_65848.so] Error 1
使用以下命令在命令行上编译文件“myCode.cpp”时一切正常:
g++ -I/opt/local/include/ -I/opt/local/include/boost \
-I/Library/Frameworks/R.framework/Resources/include -DNDEBUG \
-I/opt/local/include -I/opt/local/include -I/usr/local/include \
-I"/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp/include" \
-I"/Library/Frameworks/R.framework/Versions/3.1/Resources/library/RQuantLib/include" \
-fPIC -mtune=core2 -g -O2 -c QuantLibTest2.cpp -o QuantLibTest2.o \
-L/opt/local/lib/ -lQuantLib