没有找到-lboost_system的库

library not found for -lboost_system

本文关键字:system 的库 -lboost      更新时间:2023-10-16

我使用macports安装boost。文件出现在/opt/local/include/boost/

目录下

我的makefile不再工作,我得到以下错误

Undefined symbols:
"boost::system::generic_category()", referenced from:
  __static_initialization_and_destruction_0(int, int)in client.o
  __static_initialization_and_destruction_0(int, int)in client.o
"boost::system::system_category()", referenced from:
  boost::asio::error::get_system_category()    in client.o
  boost::system::error_code::error_code()in client.o
  __static_initialization_and_destruction_0(int, int)in client.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [client] Error 1

在学校的解决方案是使用-lboostrongystem作为g++的参数,但现在我已经把项目带回家了我的mac,这不起作用。我认为这主要是因为在学校时,boost文件位于usr/local/lib(或类似的地方)。

当我添加-lboostrongystem参数时,我得到以下消息

g++ -I/opt/local/include -lboost_system -o client client.o Packet.o
ld: library not found for -lboost_system
collect2: ld returned 1 exit status
make: *** [client] Error 1

我试过使用-L和-L的一些变化,但我似乎找不到一个有效的组合。在学校我也不用-L。我在这里读过一些关于类似问题的其他帖子,但他们通过添加-l标志来修复它,这对我不起作用。

帮助!谢谢!

您缺少一个-L/opt/local/lib。您应该能够在Makefile中设置LDFLAGS:

LDFLAGS=-L/opt/local/lib

这里假设Boost库在/opt/local/lib中。

如果您没有在Makefile中使用通常的CXXFLAGSLDFLAGS变量,那么直接在最终规则中添加-L/opt/local/lib:

client: client.o Packet.o
    g++ -L/opt/local/lib -o client client.o Packet.o -lboost_system

-I只告诉编译器头文件在哪里,链接器需要库,你使用-L

您可以尝试在您的系统中查找它,像这样:

/sbin/ldconfig -p | grep boost_system | cut -d> -f2

如果安装了库,那么它应该显示如下内容:

/usr/lib/x86_64-linux-gnu/libboost_system.so.1.58.0

或者只显示空行

在您的情况下,似乎boost安装在另一个地方,因此需要额外的链接器信息,因此需要-L开关,如果您在/usr/lib中有它,因为我不需要在makefile

中添加额外的信息