我一直希望在我的 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
使用以下命令在命令行上编译文件“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
最佳答案
您是否使用了 CRAN 资源,或者 GitHub 上的资源?
看起来设置中的“非 Windows”分支需要一些工作。目前是
## 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)
}
quantlib-config
是否在您的 PATH 中,它是否返回正确的值?
我在过去几天启用/扩展了对你想做的事情的支持——通过 Rcpp 插件使用 RQuantLib,所以请尝试当前的 GitHub 资源。它们在 Linux 上运行良好,但我没有要测试的 OS X 系统。
编辑:感谢您接受答案。为了完整起见,您的原始代码使用来自 GitHub 源的 RQuantLib 在我的盒子上按原样工作:
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>
关于c++ - 使用 Rcpp 使用最新的 QuantLib 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26590901/