未定义的引用boost :: gregorian :: greg_month :: as_short_string()const

FRu*_*les 22 c++ boost date

这被问了好几次然而我不知道我做错了什么.我试图将当前日期减去7.这是Main:

#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;
}
Run Code Online (Sandbox Code Playgroud)

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

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'
Run Code Online (Sandbox Code Playgroud)

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

Cyb*_*Guy 42

Boost date_time不是仅限标头的库.请构建库,然后添加它.简单的gcc:

gcc myapp.cpp -omyapp -lboost_date_time
Run Code Online (Sandbox Code Playgroud)

(小心!由于内联,这个库偷偷地看起来像优化级别-O2和更高级别的头文件库;但是当你使用编译器内联不那么激进的较低优化级别时,它将无法链接.)

  • @DominikNitschmann看起来你需要一台体面的机器.boost库的编译时间应该更短. (2认同)