Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

date

boost

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..

like image 249
FRules Avatar asked Jul 04 '13 19:07

FRules


2 Answers

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.)

like image 131
CyberGuy Avatar answered Nov 15 '22 15:11

CyberGuy


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!

like image 1
Imemmaw Avatar answered Nov 15 '22 13:11

Imemmaw