搜档网
当前位置:搜档网 › Linux下G++怎么编译使用Boost库的程序

Linux下G++怎么编译使用Boost库的程序

Linux下G++怎么编译使用Boost库的程序
2008-11-24 15:14
沁心读书(阅读 生活 人生智慧)
魔虾网址导航(魔虾网址大全 网址收藏)

首先把Boost库的头文件存放到/usr/include/boost/路径下,再把Lib文件存放到/usr/local/lib/boost/路径下。修改/etc/profile文件,在此文件中增加如下2个环境变量:

BOOST_INCLUDE=/usr/include/boost
export BOOST_INCLUDE

BOOST_LIB=/usr/local/lib/boost
export BOOST_LIB

写一个如下所示的cpp文件。
//samlpe.cpp
#include
#include
#include

using namespace std;

void threadRoutine(void)
{
boost::xtime time;
time.nsec = 0;
time.sec = 20;
cout << "线程函数做一些事情" << endl;
boost::thread::sleep(time);
}

int main(void)
{
string str;
cout << "输入任意字符开始创建一个线程..." << endl;
cin >> str;
boost::thread t(&threadRoutine);
t.join();
cout << "输入任意字符结束运行..." << endl;
cin >> str;
return 0;
}

保存。使用g++编译,命令如下所示:

g++ -o samlpe.out samlpe.cpp -I$BOOST_INCLUDE -L$BOOST_LIB -lboost_thread-gcc-mt

其中-I参数指定Boost头文件路径,-L参数指定Boost库文件路径,-l参数指定使用线程库名。在我使用的这个版本Boost里,到/usr/local/lib/boost路径下,可以看到有关Boost线程库文件,比如:libboost_thread-gcc-mt.a等。注意在用-l参数指定库名时把磁盘文件名前面那个lib前缀去掉就可以了。

相关主题