【发布时间】:2011-11-10 14:29:30
【问题描述】:
我正在尝试学习 Boost 线程。 我正在使用在线教程中的代码,在出现一些错误后,我意识到我需要更新版本的 Boost,因此我将其最新版本下载到目录中,解压缩并使用以下命令安装:
./bootstrap.sh
./bjam install
我试图运行的示例代码是这样的:
#include <boost/thread.hpp>
#include <iostream>
using namespace std;
using namespace boost;
void threader()
{
for (int i = 0; i < 5; ++i)
{
sleep(1);
cout << boost::this_thread::get_id() << "-" << i << endl;
//cout << "-" << i << endl;
}
}
int main()
{
thread t(threader);
sleep(1);
thread u(threader);
t.join();
u.join();
}
我使用与旧版本 Boost(1.33 与 Centos 作为标准)相同的行编译:
g++ -Wall -L/usr/local/lib -lboost_thread threadtest.cpp -o threadtest
它编译时没有错误(与旧版本的 Boost 不同),但是当我运行 threadtest 时,我得到:
./threadtest: error while loading shared libraries: libboost_thread.so.1.47.0: cannot open shared object file: No such file or directory
查看 /usr/local/lib 目录我可以看到以下内容:
-rw-r--r-- 1 root root 217270 Nov 10 12:50 libboost_thread.a
lrwxrwxrwx 1 root root 25 Nov 10 12:43 libboost_thread.so -> libboost_thread.so.1.47.0
-rwxr-xr-x 1 root root 138719 Nov 10 12:43 libboost_thread.so.1.47.0
所以我看不出它为什么不起作用。 我认为这与编译行的 -lboost_thread 部分有关。 我尝试使用以下方式直接链接到库:
g++ -Wall -L/usr/local/lib libboost_thread.a threadtest.cpp -o threadtest
但它又找不到文件了。 有人可以帮忙吗?
【问题讨论】: