c++ - 链接到 Boost 库
问题描述
我是 Boost 库使用的新手,我无法弄清楚为什么以下内容不起作用。
我想从库的文档中编译以下代码:
#include <boost/regex.hpp>
#include <iostream>
#include <string>
int main() {
std::string line;
boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );
while (std::cin) {
std::getline(std::cin, line);
boost::smatch matches;
if (boost::regex_match(line, matches, pat))
std::cout << matches[2] << std::endl;
}
}
如文档所述,我们可以使用两种方法进行编译。
方法一:(好的)
g++ -I /usr/local/include/boost/main.cpp -o main /usr/local/lib/libboost_regex.a
方法二:(不行)
g++ -I /usr/local/include/boost/main.cpp -o main -L/usr/local/lib/ -lboost_regex
使用方法1,一切都很好。但是,使用方法 2,当我运行可执行文件时,我有:
./main: error while loading shared libraries: libboost_regex.so.1.75.0: cannot open shared object file: No such file or directory
我在这里想念什么?
提前致谢。
解决方案
大概/usr/local/lib/
包含一个静态库(libboost_regex.a
)和一个共享库(libboost_regex.so
),如果您只是-lboost_regex
在链接器命令行上指定 gcc 默认情况下更喜欢共享库而不是静态库。防止这种情况的唯一方法是传递-static
标志,但这会阻止 gcc 链接到任何可能不是您想要的共享库。您还可以从目录中删除共享库,以便 gcc 只能使用静态库。
要在运行时解决问题,请确保在运行时列出该问题以/usr/local/lib
更新缓存,以便动态链接器知道在运行时在哪里查找 boost 共享库。/etc/ld.so.conf
sudo ldconfig
ldconfig
推荐阅读
- mysql - SQL:使用 SORT 将相同的值组合在一起,同时随机混合不同的值
- c# - .NET Core 中的自定义“CoreLib”?
- c# - 为什么我无法检测到发送到 CommonDialog 的 Windows 消息?拦截它们的正确方法是什么?
- swift - 将数据从 Firebase 下载到 Xcode UITableView
- html - Ratina 5K/4K 屏幕页脚下方的额外空间(与最大宽度相关)
- azure - 尝试添加托管存储磁盘时出现 Terraform 错误
- r - 边缘未正确从 data.frame 转换为 geom_net 相关图
- php - 嵌入式pdo在php中失去连接
- swift - 如何在 Swift 中为 SKLabelNode 下划线?
- python - 如何解决错误:只能将 str(不是“int”)连接到 str 但我正在将字符串添加到字符串