链接错误提升库

Linking error Boost libraries

本文关键字:错误 链接      更新时间:2023-10-16

我在这里用指南构建了 BOOST 库:http://www.boost.org/doc/libs/1_65_1/more/getting_started/unix-variants.html然后我想编译以下代码:

#include <boost/timer/timer.hpp>
#include <cmath>
int main()
{
  boost::timer::auto_cpu_timer t;
  for (long i = 0; i < 100000000; ++i)
    std::sqrt(123.456L); // burn some time
  return 0;
}

我在终端中发出了以下命令

$ c++ -o program main.cpp -I /Users/miszo97/Desktop/boost_1_65_0 -L /Users/miszo97/Desktop/boost_1_65_0/stage/lib

然后它屈服了

Undefined symbols for architecture x86_64:
  "boost::timer::auto_cpu_timer::auto_cpu_timer(short)", referenced from:
      _main in main-a716e4.o
  "boost::timer::auto_cpu_timer::~auto_cpu_timer()", referenced from:
      _main in main-a716e4.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我想链接出了点问题。也许单独编译的 Boost 库没有正确构建?

有两个主要选项用于链接。

首先是-L选项,它添加一个目录来搜索库。

但是,链接

器不知道要自动链接哪些库,它不会遍历所有可能的库来找到匹配的库。这就是第二个选项的用武之地:-l(小写L)选项,它告诉链接器与特定库链接。

使用

-l 指定的库只是库名称,而不是完整的文件名,链接器使用-L添加的路径来查找它们。

在使用 Boost 计时器库的情况下,库名称应boost_timer ,因此您需要在链接时添加选项-lboost_timer

相关文章: