3

I just started learning C++(coming from Java & Python) and am trying to learn how to use other libraries. I found some sample code(from the boost site) I wanted to run(below) but its giving me this error:

`Undefined symbols for architecture x86_64:
  "boost::gregorian::greg_month::get_month_map_ptr()", referenced from:
      unsigned short boost::date_time::month_str_to_ushort<boost::gregorian::greg_month>(std::string const&) in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)`

I think the code is solid(since its not produced by me :-) but I’m not sure the steps required to use another library. So far, I just added the #include but should I also be using a header file for this(I know how to use one when I create multiple files when I create them myself but unsure about external libraries).

I found a similar question ( using from_string with boost date ), that suggested I need to link it boost’s datatime, so I ran the command(changed filenames) and got this error:

`ld: library not found for -lboost_date_time
collect2: ld returned 1 exit status`

I think I installed boost correctly, I used Brew on the mac and saw it compile a bunch of files(it took like an hour to install). So if it is installed, What am I doing wrong(or not doing)?

Thanks

Here’s the code:

#include "boost/date_time/gregorian/gregorian.hpp"
#include <iostream>

int
main() 
{

    using namespace boost::gregorian;
    std::string s;
    std::cout << "Enter birth day YYYY-MM-DD (eg: 2002-02-01): ";
    std::cin >> s;
    try {
        date birthday(from_simple_string(s));
        date today = day_clock::local_day();
        days days_alive = today - birthday;
        days one_day(1);
        if (days_alive == one_day) {
            std::cout << "Born yesterday, very funny" << std::endl;
        }
        else if (days_alive < days(0)) {
            std::cout << "Not born yet, hmm: " << days_alive.days() 
            << " days" <<std::endl;
        }
        else {
            std::cout << "Days alive: " << days_alive.days() << std::endl;
        }

    }
    catch(...) {
        std::cout << "Bad date entered: " << s << std::endl;
    }
    return 0;
}
Community
  • 1
  • 1
Lostsoul
  • 25,013
  • 48
  • 144
  • 239
  • @BenjaminLindley it'll sky rocket once I learn how to use external libraries :-) – Lostsoul May 07 '12 at 15:35
  • Setting this up varies with the compiler and (if you're using one) IDE as well as (often) the OS. – Jerry Coffin May 07 '12 at 15:35
  • @JerryCoffin I'm using Xcode as the IDE for development but plan to run it on linux. I was trying to learn boost because I heard its portable and won't require much change between platforms. – Lostsoul May 07 '12 at 15:37
  • Boost itself is quite portable, but the steps to get your IDE to use it aren't. – Jerry Coffin May 07 '12 at 15:41

1 Answers1

3

The boost libraries generally have really funky names that include the build options and sometimes the version.

On my system I might use -lboost_date_time-mgw45-d-1_47 to link to the library you mention.

Edward Strange
  • 40,307
  • 7
  • 73
  • 125