链接到 Boost 库
Linking to a Boost library
我是 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
使用方法一,一切正常。
但是,使用方法 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
),如果您只是在链接器命令行 gcc 上指定 -lboost_regex
默认情况下,共享库优于静态库。防止这种情况的唯一方法是传递 -static
标志,但这会阻止 gcc 链接到任何可能不是您想要的共享库。您也可以从目录中删除共享库,以便 gcc 只能使用静态库。
要在 运行 时解决问题,请确保 /usr/local/lib
列在 /etc/ld.so.conf
中,然后 运行 sudo ldconfig
更新 ldconfig
缓存以便动态链接器知道在 运行 时间去哪里寻找 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
使用方法一,一切正常。 但是,使用方法 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
),如果您只是在链接器命令行 gcc 上指定 -lboost_regex
默认情况下,共享库优于静态库。防止这种情况的唯一方法是传递 -static
标志,但这会阻止 gcc 链接到任何可能不是您想要的共享库。您也可以从目录中删除共享库,以便 gcc 只能使用静态库。
要在 运行 时解决问题,请确保 /usr/local/lib
列在 /etc/ld.so.conf
中,然后 运行 sudo ldconfig
更新 ldconfig
缓存以便动态链接器知道在 运行 时间去哪里寻找 boost 共享库。