undefined reference to boost::gregorian::greg_month::as_short_string() const

隐身守侯 提交于 2020-01-31 06:46:08

问题


This was asked several times however I don't know what I'm doing wrong. I'm trying to get the current date subtracted by 7. Here's the 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;
}

When I try to compile it I get the following error.

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'

Can anyone help? I thought I included the neccessary files..


回答1:


Boost date_time is not a header-only library. Please build the library and then add it. Simple in gcc:

gcc myapp.cpp -omyapp -lboost_date_time

(Be careful! This library sneakily appears to work as a header-only library at optimization levels -O2 and higher, due to inlining; but it will fail to link when you use lower optimization levels where the compiler's inliner isn't as aggressive.)




回答2:


I think the compiler is complaining about the inclusion of boost lib.

In order to use boost::gregorian(boost::date_time), you need to use bjam to build boost library and then link it against the FileSystem lib.

The reference of boost see click here.

EDIT: According to what you got above, the problem is that the library can't be found, mingw seems like don't know where it is. A re-installation of mingw maybe required or you can try to specify the specific path of the library.

Good luck!




回答3:


you should add the link lib named

libboost_date_time-mgw46-d-1_54.dll.a

(my path D:\My Documents\Downloads\boost_1_54_0\bin.v2\libs\date_time\build\gcc-mingw-4.6.2\debug\libboost_date_time-mgw46-d-1_54.dll.a) to the compiler's path
Good luck




回答4:


The reason of the linking issue is the class grep_month part of implementation is at other cpp file located in file boost_xxx_xx_x\libs\date_time\src\gregorian\greg_month.cpp. So this should be built into a static library or directly built into your target.

The other reason of why the "Release" mode with option "O2" building could pass ok, it should be caused by the final codes has not called the gregorian::greg_month related codes, and the complier ignore linking the unused function into the target, so the building is sneakily passed. So the CyberGuy's comments in the stackoverflow website about the argument of inlining should just be a guess.



来源:https://stackoverflow.com/questions/17477177/undefined-reference-to-boostgregoriangreg-monthas-short-string-const

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!