如何在C ++中使用外部库?

时间:2012-05-07 15:31:32

标签: c++

我刚刚开始学习C ++(来自Java和Python)并且正在尝试学习如何使用其他库。我找到了一些示例代码(来自boost网站)我想运行(下面),但它给了我这个错误:

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

我认为代码是可靠的(因为它不是由我生成的:-)但我不确定使用另一个库所需的步骤。到目前为止,我刚刚添加了#include,但我是否也应该使用头文件(当我自己创建多个文件但我不确定外部库时,我知道如何使用一个文件)。

我发现了一个类似的问题(using from_string with boost date),这表明我需要将它连接到boost的数据时间,所以我运行了命令(更改了文件名)并得到了这个错误:

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

我认为我正确安装了boost,我在mac上使用Brew并看到它编译了一堆文件(安装花了一个小时)。所以如果安装了,我做错了什么(或者没做错)?

由于

以下是代码:

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

1 个答案:

答案 0 :(得分:3)

增强库通常具有非常时髦的名称,包括构建选项,有时还包括版本。

在我的系统上,我可能会使用-lboost_date_time-mgw45-d-1_47链接到您提到的库。