How to use external libraries in C ++?

I just started learning C ++ (from Java and Python), and I'm trying to learn how to use other libraries. I found some sample code (from the boost site) that I wanted to run (below), but it gave 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 it wasn’t created by me :-), but I'm not sure if you need to use another library. So far, I just added #include, but should I use the header file for this (I know how to use it when I create several files, when I create them myself, but I don’t know about external libraries).

I found a similar question ( using from_string with the afterburner date ) that suggested I bind it to speed up the datatime, so I ran the command (changed file names) 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 it Brewon mac and saw that it compiled a bunch of files (it took an hour to install). So, if it is installed, what am I doing wrong (or not doing)?

thank

Here is 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;
}
+3
source share
1 answer

Accelerator libraries usually have really funky names that include build options, and sometimes a version.

On my system, I can use -lboost_date_time-mgw45-d-1_47to link to the library you specify.

+3
source

All Articles