boostlinux编译
首先把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 <iostream>
#include <string>
#include <boost/thread.hpp>
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前缀去掉就可以了。
B. Boost库 Linux下GCC编译,直接拷出Lib和inc到window下能用吗
不可以的
在linux下获取到的只是linux可以用的库
用Mingwin的才可以在windows上用
静态库看起来是一样的
都是.a
不过本质是不同的
动态库
区别更明显些
linux是.so
而windows是.dll
C. 在linux上运行boost库的问题
我系统是ubuntukylin14.04
然后今天去BOOST下了最新版的boost1.57版
下载下来的压缩文件我解压到/opt目录下即/opt/boost_1_57_0
然后
cd /opt/boost_1_57_0;
./boststrap;
./b2
这里b2命令执行完成后显示:
The Boost C++ Libraries were successfully built!
The following directory should be added to compiler include paths:
/opt/boost_1_57_0
The following directory should be added to linker library paths:
/opt/boost_1_57_0/stage/lib
然后我找了一段例子
C/C++ code?
1
2
3
4
5
6
7
8
9
10
#include<iostream>
#include<boost/bind.hpp>
using namespace std;
using namespace boost;
int fun(int x,int y){return x+y;}
int main(){
int m=1;int n=2;
cout<<boost::bind(fun,_1,_2)(m,n)<<endl;
return 0;
}
用g++编译的时候提示
bst.cxx:2:31: fatal error: boost/bind.hpp: 没有那个文件或目录
#include<boost/bind.hpp>
^
compilation terminated.
D. 如何编译boost linux
linux平台下要编译安装除gcc和gcc-c++之外,还需要两个开发库:bzip2-devel 和python-devel,因此在安装前应该先保证这两个库已经安装:
#yum install gcc gcc-c++ bzip2 bzip2-devel bzip2-libs python-devel -y
然后是去官网下载源码包,按照如下步骤:
#tar xvzf boost_1_50_0.tar.gz
进入boost_1_50_0目录:
#cd boost_1_50_0
然后是编译安装,boost源码包中有配置脚本,直接用就可以:
#sh ./bootstrap.sh
Building Boost.Build engine with toolset gcc... tools/build/v2/engine/bin.linuxx86_64/b2
Detecting Python version... 2.6
Detecting Python root... /usr
Unicode/ICU support for Boost.Regex?... not found.
Generating Boost.Build configuration in project-config.jam...
Bootstrapping is done. To build, run:
./b2
To adjust configuration, edit 'project-config.jam'.
Further information:
- Command line help:
./b2 --help
- Getting started guide:
http://www.boost.org/more/getting_started/unix-variants.html
- Boost.Build documentation:
http://www.boost.org/boost-build2/doc/html/index.html
接下来就是编译,重点关注是否编译成功:
#./b2
E. 编写linux内核程序使用C++时可以使用BOOST,STL类似的函数库么
当然可以,c++是跨平台的,,c++可以用boost,但是得在linux下边重新编译,而stl这个东西可以直接用的。你可以在linux下边找到stl的库函数的。boost编译完了,得把相应的so加到g++路径或者env变量路径里面。想学习了解更多linux知识,请关注《linux就该这么学》官网。
F. linux下eclipse使用boost asio进行网络开发
linux下boost asio并行开发:
1.三种使用方式
1)single thread && single io_service, 最简单, 性能最一般
2)multithread && single io_service
3)io_service per thread. multi io_service.
这三个性能是依次递增的。
2.在使用ASIO时,io_servie应该尽量多,这样可以使其epoll_wait占用的时间片最多,这样可以最大限度的响应IO事件,降低响应时延。但是每个io_servie::run占用一个线程,所以io_servie最佳应该和CPU的核数相同。
3.io_service是一个工作队列的模型。在使用过程中一般有如下几个需要注意的地方:
run函数在io事件完成后会退出,导致后续基于该对象的异步io任务无法执行。
由于io_service并不会主动常见调度线程,需要我们手动分配,常见的方式是给其分配一个线程,然后执行run函数。但run函数在io事件完成后会退出,线程会终止,后续基于该对象的异步io任务无法得到调度。
解决这个问题的方法是通过一个asio::io_service::work对象来守护io_service。这样,即使所有io任务都执行完成,也不会退出,继续等待新的io任务。
boost::asio::io_service io;
boost::asio::io_service::work work(io);
io.run();
G. Linux 编译安装的boost库怎么删除
建议你可以用电脑管家来卸载哟
电脑管家是直接把卸载同时把注册列表也就清理了。(有的重启后会自动删除)
【打开电脑管家——工具箱——卸载】
卸载完之后,会有残留,在实行【强力卸载】
H. linux下编译boost库没有root权限怎么办
命令前面加 sudo 啊
或者 su root
换成 root 用户执行
I. 如何在linux上使用boost:thread-C/C++
首先需要安装boost,步骤如下:
下载到boost_1_49_0.tar.bz2 (当然,其他压缩格式也可以)后,可以把它放在用户目录下,即:~/
解压缩:tar -jxvf boost_1_49_0.tar.bz2
这样,出现文件夹:~/boost_1_49_0
然后进入:$ cd boost_1_49_0
你会发现有一个sh命令:bootstrap.sh
运行它:$ ./bootstrap.sh (boost自己的get start文档中说设置参数 --prefix=dir 其中dir为你想指定的安装文件夹,我建议就不用加这个参数,它会默认安装到/usr/local)
结束后出现一个可执行文件: ~/boost_1_49_0/b2
运行这个文件: $ sudo ./b2 install (Ubuntu用户千万别忘了加sudo,不然安装后将无法完全使用)
编译安装时间比较长,根据不同机器的情况20~40分钟。结束后即安装完毕。
boost::thread的使用
#include<boost/thread.hpp>
#include<iostream>
voidtask1(){
//dostuff
std::cout<<"Thisistask1!"<<std::endl;
}
voidtask2(){
//dostuff
std::cout<<"Thisistask2!"<<std::endl;
}
intmain(intargc,char**argv){
usingnamespaceboost;
threadthread_1=thread(task1);
threadthread_2=thread(task2);
//dootherstuff
thread_2.join();
thread_1.join();
return0;
}
编译时的命令为:
$ g++ -I./inlcude -L./lib example.cpp -lboost_thread -o example
编译之后会出现一个 example 的可执行文件,可以运行:./example , 结果显示:
This is task2!
This is task1!
可能你在运行时会出现这样的错误:error while loading shared libraries: libboost_thread.so.1.49.0: cannot open shared object file: No such file or directory
这是因为要用到的库不在默认的环境变量里,可以使用下面的命令添加:
$ sudo ldconfig /usr/local/lib
添加后,再执行./example,这样你就完成了你的第一个boost::thread程序。