首页 > 技术文章 > linux下编译boost的多线程程序

sigma0- 2015-08-24 17:39 原文

linux下面用boost库进行多线程编程,一开始总是编译不成功,花了好多的时间。
下面是一段小示例代码:

//start from the very beginning,and to create greatness
//@author: Chuangwei Lin
//@E-mail:979951191@qq.com
//@brief: boost多线程编译的示例
#include <boost/thread.hpp>
#include <iostream>
void lcw1()
{ 

    std::cout << "lcw1 is working!" << std::endl;
}

void lcw2() 
{ 

    std::cout << "lcw2 is working!" << std::endl;
}

int main (int argc, char ** argv)
{
    using namespace boost; 
    thread thread_1 = thread(lcw1);
    thread thread_2 = thread(lcw2);

    thread_2.join();
    thread_1.join();
    return 0;
}

编译命令如下;

 g++ -I /usr/local/inlcude -L /usr/lib lcw.cpp -lboost_system -lboost_thread -o lcw

上述的编译命令要包含库的路径还有链接库的名称,不然编译无法通过
(之前脑残忘记是c++然后一直用gcc编译。。。。。)
但是,虽然编译成功,还是出现如下的问题:
这里写图片描述
那就表示系统不知道xxx.so放在哪个目录下,这个时候就要在/etc/ld.so.conf中加入xxx.so所在的目录,我们先前安装的时候是把.so文件都搬到了/usr/lib里面,所以在/etc/ld.so.conf中加入/usr/lib这一行,可以解决此问题。将/etc/ld.so.conf存档后,还要执行/etc/ldconfig 来更新一下才会生效。最后终于可以执行,结果如下;
这里写图片描述

推荐阅读