c++ - 使用 Rcpp 使用最新的 QuantLib 代码

标签 c++ r macos rcpp quantlib

我一直希望在我的 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/

相关文章:

c++ - Visual C++ 中 DLL 的 DLL 导出和接口(interface)

c++ - 无法将迭代器传递给 C++ 中的类方法

r - 使用 RCurl/httr 进行 Github 基本授权

r - 如何从 R 脚本调用可执行文件

r - 将 4 个列表保存为 1 个列表中的子列表元素

javascript - 在 Safari for Mac 中使用 Ctrl+Click 时如何禁用上下文菜单事件中的单击事件?

objective-c - 在 OSX 中编写 IDE 或文本编辑器

c++ - 是否可以列出所有可以通过 `.Call()` 加载的可用名称? .Call ("function"错误)

c++ - 如何将一个类的嵌套枚举用作另一个类的嵌套枚举?

python - Mac 上的 wxpython - 按钮样式