编译单独Cpp文件千万别忘了加参数。
g++ Client.cpp -lpthread -lboost_system
这个也是,ubuntu下使用集成开发环境时,需要的library,可以添加的哦,使用项目的属性添加即可。
error while loading shared libraries的解决方法
./tests:error while loading sharedlibraries: xxx.so.0:cannot openshared object file: No such file or directory
那就表示系统不知道xxx.so 放在哪个目录下。
这个时候就要在/etc/ld.so.conf中加入xxx.so所在的目录。
一般而言,有很多so档会在/usr/local/lib这个目录下,所以在/etc/ld.so.conf中加入/usr/local/lib这一行,可以解决此问题。
将 /etc/ld.so.conf存档后,还要执行「/sbin/ldconfig –v」来更新一下才会生效。
使用boost库编写多线程程序:
#define BOOST_DATA_TIME_SOURCE
#define BOOST_THREAD_NO_LIB
#include <boost/thread/thread.hpp>
#include <iostream>
using namespace std;
using namespace boost;
void hello()
{
std::cout <<
"Hello world, I'm a thread!"
<< std::endl;
}
int main(int argc, char* argv[])
{
boost::thread thrd(&hello);
// 等待线程结束
thrd.join();
return 0;
}
编译时控制台提示错误:
Description Resource Path Location Type
undefined reference to `boost::thread::detach()' NewXScada line 174, external location: /usr/local/include/boost/thread/detail/thread.hpp C/C++ Problem
undefined reference to `vtable for boost::detail::thread_data_base' NewXScada line 113, external location: /usr/local/include/boost/thread/pthread/thread_data.hpp C/C++ Problem
undefined reference to `boost::detail::thread_data_base::~thread_data_base()' NewXScada line 50, external location: /usr/local/include/boost/thread/detail/thread.hpp C/C++ Problem
undefined reference to `boost::thread::start_thread()' NewXScada line 218, external location: /usr/local/include/boost/thread/detail/thread.hpp C/C++ Problem
undefined reference to `boost::thread::join()' main.cpp /NewXScada line 22 C/C++ Problem
undefined reference to `boost::thread::join()' main.cpp /NewXScada line 21 C/C++ Problem
解决办法:
在工程上点击右键->Properties
打开如下设置界面:
点开左侧的C/C++ Build,在Tool Settings 下面的列表中找到MinGW C++ Linker -->Libraries,在上面的Libraries中点击右上角的加号图标,添加“ boost_thread ”,如图:
点击“OK”,点击“Apply”,然后再点击“OK”退出。重新编译,没有问题了。