这被问了好几次,但我不知道我做错了什么。我正在尝试将当前日期减去 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/