Lostsoul
Lostsoul

Reputation: 25991

How can I use external libraries in C++?

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;
}

Upvotes: 3

Views: 1504

Answers (1)

Edward Strange
Edward Strange

Reputation: 40849

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.

Upvotes: 3

Related Questions