c++ - 对 boost::gregorian::greg_month::as_short_string() const 的 undefined reference

标签 c++ date boost

这被问了好几次,但我不知道我做错了什么。我正在尝试将当前日期减去 7。这是主要的:

#include <iostream>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/date_formatting.hpp>
#include <boost/date_time/gregorian/greg_month.hpp>


using namespace std;
using namespace boost::gregorian;

int main(int argc, char **argv) {

    time_t rawtime;
    struct tm *timeinfo;

    time (&rawtime);
    timeinfo = localtime (&rawtime);

    date cdate(timeinfo->tm_year+1900, timeinfo->tm_mon+1, timeinfo->tm_mday);
    cdate += date_duration(-7);

    string date = to_iso_string(cdate);
    cout << date << endl;
    return 0;
}

当我尝试编译它时,我收到以下错误。

E:/include/boost/date_time/date_formatting.hpp:44: undefined reference to `boost::gregorian::greg_month::as_short_string() const'
E:/include/boost/date_time/date_formatting.hpp:49: undefined reference to `boost::gregorian::greg_month::as_long_string() const'

有人可以帮忙吗?我以为我包含了必要的文件..

最佳答案

Boost date_time 不是一个只有头文件的库。请构建库,然后添加它。在 gcc 中很简单:

gcc myapp.cpp -omyapp -lboost_date_time

(小心!由于内联,这个库偷偷地似乎在优化级别 -O2 和更高级别上作为仅 header 库工作;但它会失败当您使用编译器的内联器不那么激进的较低优化级别时链接。)

关于c++ - 对 boost::gregorian::greg_month::as_short_string() const 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17477177/

相关文章:

c++ - 二叉搜索树 C++(父级)

c++ - CGAL 错误:计算没有给定点的半空间交集无法编译(boost::none?)

c - GNU ld 不能与 libboost 静态链接,但可以动态链接

c++ - 使用 C++ 将一个 XML 文档转换为另一个

c++ - 在没有new关键字的情况下创建的c++中结构对象的范围

c++ - 不能用通告包含转发声明

node.js - 如何比较 nodejs 中的两个 fileStat.mtime?

mongodb - ReactJS - 如何从 MongoDB Date.now 更改日期格式

Excel计算 "7/6/2012 10:26:42"单元格与今天的日期差

c++ - ubuntu 中是否有库的默认路径?