我一直希望在我的R代码中重用QuantLib中的日期和日历功能。由于RQuantLib未涵盖我编译并安装了最新QuantLib版本的所有日历。但是,我无法运行similar question中提供的示例。如何配置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
编译文件&#34; myCode.cpp&#34;在命令行上使用以下命令:
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
答案 0 :(得分:1)
您使用过CRAN来源,还是使用GitHub上的内容?
它看起来像&#34;不是Windows&#34;设置中的分支需要一些工作。它目前正在
## on Linux and OS X, see if we have quantlib-config which may well be
## false in the case of prebuild binary packages as eg r-cran-rquantlib
## on Debian / Ubuntu as well as the OS X package from CRAN
qc <- system("bash -c 'type -p quantlib-config'", ignore.stderr=TRUE, intern=TRUE)
if (is.character(qc) && nchar(qc) > 1) {
qlcflags <- system(paste(qc, "--cflags"), intern = TRUE)
qllibs <- system(paste(qc, "--libs"), intern = TRUE)
}
PATH中是quantlib-config
,是否会返回正确的值?
我启用/扩展了对你想要做的事情的支持 - 在过去的几天里通过Rcpp插件使用RQuantLib,所以请尝试使用当前的GitHub源代码。它们在Linux上运行良好,但我没有OS X系统可以测试。
修改:谢谢您接受答案。为了完整起见,您的原始代码使用GitHub源中的RQuantLib在我的盒子上 as-is :
R> system("cat /tmp/rql.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;
}
R> sourceCpp("/tmp/rql.cpp") # takes a few seconds
R> timesTwo(21)
[1] 42
R>