Example
我有一个 Rcpp 函数,我想调用它boost::posix_time::time_from_string()
.
我从以下位置获取了示例代码增强文档 https://www.boost.org/doc/libs/1_65_0/doc/html/date_time/posix_time.html并使用 Rcpp 将其转换为 C++ 函数
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
, 例如这条线 https://github.com/eddelbuettel/rcppbdt/blob/master/src/toPOSIXct.cpp#L250在德克的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 ...
// }
Question
您如何提供适当的链接lboost_date_time
,除了包括posix_time.hpp
标题,以便可以使用time_from_string()
?
额外信息
可以使用其他功能boost/date_time
库,正如这个函数所演示的,那么是什么使得time_from_string()
不同的?
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