c++ - 我安装了 libboost 但无法链接到它

标签 c++ boost linker

我已经通过apt-get安装了libboost-dev,它放在/usr/lib中。

/usr/lib$ ls | grep boost
libboost_filesystem.so.1.46.1
libboost_iostreams.so.1.46.1
libboost_serialization.so.1.46.1
libboost_system.so.1.46.1
libboost_thread.so.1.46.1
libboost_wserialization.so.1.46.1

但是当我尝试编译使用 boost_thread 的源代码时,我仍然遇到错误。

$ g++ tcp_echo.cpp -o tcp_echo -L/usr/lib -llibboost_thread
/usr/bin/ld: cannot find -lboost_thread
collect2: ld returned 1 exit status
$ g++ tcp_echo.cpp -o tcp_echo -L/usr/lib -lboost_thread
/usr/bin/ld: cannot find -lboost_thread
collect2: ld returned 1 exit status

安装和链接到 libboost 的正确方法是什么?

最佳答案

One thing I notice is that you do have no libboost_thread.so. You have the versioned 1.46.1 file but usually libraries will create a symbolic link to the versioned copy with the undecorated name. That might not be it but it's one thing I noticed. (This is typically done by the installer.) – Omaha

我认为这是重点。这意味着我以错误的方式安装了 libboost。其实我只安装了libboost-dev:

sudo apt-get install libboost-dev 

但是我应该做的是:

sudo apt-get install libboost-dev libboost1.46-doc libboost-date-time1.46-dev ibboost-filesystem1.46-dev libboost-graph1.46-dev libboost-iostreams1.46-dev libboost-math1.46-dev libboost-program-options1.46-dev libboost-python1.46-dev libboost-random1.46-dev libboost-regex1.46-dev libboost-serialization1.46-dev libboost-signals1.46-dev libboost-system1.46-dev libboost-test1.46-dev libboost-thread1.46-dev libboost-wave1.46-dev

(或者,在我的特殊情况下,至少安装 libboost-system1.46-dev libboost-thread1.46-dev)

一旦你正确安装它们,/usr/lib 中应该有 .a.so

/usr/lib$ ls | grep boost
libboost_date_time.a
libboost_date_time-mt.a
libboost_date_time-mt.so
libboost_date_time.so
libboost_date_time.so.1.46.1
libboost_filesystem.a
libboost_filesystem-mt.a
... and so on ...

关于c++ - 我安装了 libboost 但无法链接到它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11137398/

相关文章:

c++ - 逗号运算符如何工作

c++ - 函数原型(prototype)中的 const 参数

c++ - 从构造函数中抛出异常

c++ - boost::bind 会导致开销吗?

c++ - 错误 C2660 : 'std::allocator<char>::allocate' : function does not take 2 arguments

mongodb - 使用 mongodb 驱动程序编译时 Rust 链接错误

c - STM32F7 : hard fault when jumping to application from custom bootloader

c++ - AttachConsole 和 QProcess::readAll()

c++ - 不能在非 Boost 版本的 Asio 中使用 asio::placeholders::error

c - 链接器错误 undefined reference ... c 中包含的库