如何链接到提升 date_time [英] How to link to boost date_time

查看:31
本文介绍了如何链接到提升 date_time的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Rcpp 函数,我想在其中调用 boost::posix_time::time_from_string().

I have an Rcpp function where I would like to call boost::posix_time::time_from_string().

我从 boost 文档并使用 Rcpp 将其转换为 C++ 函数

I've taken the example code from the boost documentation and translated it to a c++ function using Rcpp

library(Rcpp)

cppFunction(
  includes = '
    #include <boost/date_time/posix_time/posix_time.hpp>
  ',
  code = '
    void time_test() {
      std::string t = "2002-01-20 23:59:59.000";
      boost::posix_time::ptime pt(boost::posix_time::time_from_string(t));
      Rcpp::Rcout << "time from string: " << pt << std::endl;
    }
  ',
  depends = "BH"
)

然而,这不会编译.

我看到一些评论说您需要链接到 -lboost_date_time,例如 这一行在德克的RcppBDT

I've seen a few comments saying you need to link to -lboost_date_time, such as this line in Dirk's RcppBDT library

// The next function uses the non-stream-based parsing in Boost Date_Time
// and requires _linking_ with -lboost_date_time which makes the (otherwise
// header-only) build more complicate
// // [ [ Rcpp::export ] ]
// Rcpp::DatetimeVector charToPOSIXctNS(Rcpp::CharacterVector sv) {
//   ... code omitted ...
// }

问题

除了包含 posix_time.hpp 标头之外,您如何提供到 lboost_date_time 的适当链接,以便可以使用 time_from_string()>?

Question

How do you provide the appropriate links to lboost_date_time, other than including the posix_time.hpp header, so that one can use time_from_string()?

可以使用 boost/date_time 库中的其他函数,正如这个函数所展示的那样,那么是什么让 time_from_string() 与众不同?

It's possible to use other functions from the boost/date_time library, as demonstrated by this function, so what makes time_from_string() different?

cppFunction(
  includes = '
    #include <boost/date_time/posix_time/posix_time.hpp>
  ',
  code = '
    void time_test() {
      Rcpp::Datetime dt("2002-01-20 23:59:59.000");
      boost::posix_time::hours h( dt.getHours() );
      boost::posix_time::minutes m( dt.getMinutes() );
      boost::posix_time::seconds s( dt.getSeconds() );

      Rcpp::Rcout << h << std::endl;
      Rcpp::Rcout << m << std::endl;
      Rcpp::Rcout << s << std::endl;
    }
  ',
  depends = "BH"
)

time_test() 

# 12:00:00
# 00:59:00
# 00:00:59

推荐答案

正如您已经发现的,您需要在系统级别链接与 boost.BH 包是不够的.所以首先你必须安装所需的boost库.在 Debian(派生)Linux 系统上,这可以通过

As you already found out, you need to link with boost at the system level. The BH package is not sufficient. So first you have to install the required boost library. On Debian (dervied) Linux systems this can be done via

sudo apt-get install libboost-date-time-dev

然后你需要告诉 R 添加 -I/path/to/boost/headers-L/path/to/boost/libraries -lboost_date_time 到编译器标志.您可以通过设置适当的环境变量来做到这一点:

Then you need to tell R to add -I/path/to/boost/headers and -L/path/to/boost/libraries -lboost_date_time to the compiler flags. You can do this by setting appropriate environment variables:

library(Rcpp)

Sys.setenv(PKG_LIBS="-L/usr/lib -lboost_date_time", PKG_CPPFLAGS="-I/usr/include")

cppFunction(
  includes = '
    #include <boost/date_time/posix_time/posix_time.hpp>
  ',
  code = '
    void time_test() {
      std::string t = "2002-01-20 23:59:59.000";
      boost::posix_time::ptime pt(boost::posix_time::time_from_string(t));
      Rcpp::Rcout << "time from string: " << pt << std::endl;
    }
  '
)

注意事项:

  • 还可以为此定义一个 Rcpp 插件.
  • 在我的情况下 -I...-L... 是不必要的,因为库安装在标准位置.不过,在其他情况下确实需要这些标志.
  • One could also define a Rcpp plugin for this.
  • In my case -I... and -L... are unnecessary, since the library is installed in a standard location. One does need these flags in other cases, though.

这篇关于如何链接到提升 date_time的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆