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

Linux下G++怎么编译使用Boost库的程序,第1张

首先饥空把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前缀去掉罩册就可以了。

下载Boost库,这里我选择下载boost_1_55_0.zip

解压boost文件到本地目录(如G:\boost_1_55_0),可以发现解压后的文件中有一个bootstrap.bat文件。

然后以管理员身份打开cmd窗口岩唤,

上述命令执行完毕后可以发现G:\boost_1_55_0下新生成了一个bjam.exe文件

在命令窗口中输入语句:bjam.exe

此过程将默认根据系统已经安装好的编译工具(VS2008,2010,2012,2013)等编译相应的Lib文件、头文件等。(此步骤大概需要圆简10分钟)

可以看到msvc 12.0,这是因为我系统中已经安装过了VS2013

msvc : 8.0是VS2005

msvc : 10.0是VS2010

msvc : 12.0是VS2012、VS2013

第5步执行成功后会有如下信息提示

至此我们已经完成了boost库的安装,下面需要配置一下VS2013了。新建一个VS2013控制台应用程序(工程名为boostest),添加如下代码

#include "stdafx.h"

#include <boost/lexical_cast.hpp>

#include <iostream>

using namespace std

int main()

{

using boost::lexical_cast

int a = lexical_cast<int>("123")

double b = lexical_cast<double>("123.0123456789")

string s0 = lexical_cast<string>(a)

string s1 = lexical_cast<string>(b)

cout <<"number: " <<a <<" " <<b <<endl

cout <<"string: " <<s0 <<" " <<s1 <<endl

int c = 0

try{

c = lexical_cast<橘枣裤int>("abcd")

}

catch (boost::bad_lexical_cast&e){

cout <<e.what() <<endl

}

return 0

}

添加boostest工程的包含目录和库目录

包含目录添加 G:\boost_1_55_0

库目录添加G:\boost_1_55_0\stage\lib

进入代码窗口编译并成功运行说明BOOST库确实已经配置成功,可以放心使用。

一、 下载boost

boost_1_51_0.zip 下载并解压到C盘根文件夹

二、编译boost

1、生成生命行程序

执行bootstrap.bat

2、编译

执行b2.exe,完成后显示:

The Boost C++ Libraries were successfully built!

The following directory should be added to compiler include paths:

C:/boost_1_51_0

The following directory should be added to linker library paths:

C:\boost_1_51_0\stage\lib

三、使用boost

1、创建一个win32 console

2、引用bootst

C/C++ ->Additional Include Directories: C:\boost_1_51_0

Linker->Additional Library Directories: C:\boost_1_51_0\stage\lib

 答早 Linker->Input->Additional Dependencies :libboost_signals-vc110-mt-gd-1_51.liblibboost_regex-vc110-mt-gd-1_51.lib

3、Code如下:

#include "stdafx.h"

#include <boost/regex.hpp>

#include <boost/signals.hpp>

#include <boost/lambda/lambda.hpp>

#include <iostream>

#include <卖中cassert>

struct print_sum {

void operator()(int x, int y) const { std::cout <<x+y <<std::endl}

}

struct print_product {

void operator()(int x, int y) const { std::cout <<x*y <<std::endl}

}

int _tmain(int argc, _TCHAR* argv[])

{

boost::signal2<void, int, int, boost::last_value<void>, std::string>sig

sig.connect(print_sum())

sig.connect(print_product())

sig(3, 5)

std::string line

boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" )

while (std::cin)

{

std::getline(std::cin, line)

boost::smatch matches

if (boost::regex_match(line, matches, pat))

std::cout <<matches[2] <<std::endl

}

return 0

}

示例程序在vs2012下通过,输中举山出:

8

15


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/8221027.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-14
下一篇 2023-04-14

发表评论

登录后才能评论

评论列表(0条)

保存