您的位置:首页 > 编程语言 > C语言/C++

EPD中已自带Mingw,如何安装c++ boost库?

2012-09-06 17:14 316 查看
由于已安装Python的科学计算套件epd-7.2-2-win-x86.msi,里面已自带了GNU(MinGW)的gcc,g++和gfortran等编译器,还想测试一下如何安装c++ boost库?

基本过程是这样的:

1.下载boost_1_49_0

2.解压缩后进入目录boost_1_49_0,在DOS窗口下运行如下命令

[plain]
view plaincopyprint?

REM 生成b2.exe和bjam文件:: bootstrap.bat gcc

REM 生成b2.exe和bjam文件::
bootstrap.bat gcc

3.将boost安装到指定目录

[plain]
view plaincopyprint?

REM 安装到一个指定的目录,比如 C:\boost : bjam install --toolset=gcc --prefix=C:\boost

REM 安装到一个指定的目录,比如 C:\boost :
bjam install --toolset=gcc --prefix=C:\boost


如果要安装到默认目录(c:\boost)只需运行如下命令即可

[plain]
view plaincopyprint?

b2 install --toolset=gcc

b2 install --toolset=gcc

然后是漫长的等待...大概8分钟左右

4.注意include和Lib分别为,include可加入系统的Path变量中,Lib在gcc和g++中需要使用-Llib指定

[sql]
view plaincopyprint?

C:\boost\include\boost-1_49 C:\boost\lib
C:\boost\include\boost-1_49
C:\boost\lib


随便找个例子就可以测试了boost了.

例如编译regex库的例子:

[cpp]
view plaincopyprint?

#include <boost/regex.hpp>

#include <string>
#include <iostream>

int main() {
std::cout << "Enter a regular expression:\n";
std::string s;
std::getline(std::cin, s);
try {
boost::regex reg(s);
std::cout << "Enter a string to be matched:\n";

std::getline(std::cin, s);

if (boost::regex_match(s, reg))
std::cout << "That's right!\n";
else
std::cout << "No, sorry, that doesn't match.\n";
}
catch(const boost::bad_expression & e) {
std::cout << "Invalid Regular Expression!" << std::endl;
std::cout << "Error::" << e.what() << std::endl;
}
return 0;
}

#include <boost/regex.hpp>
#include <string>
#include <iostream>

int main() {
std::cout << "Enter a regular expression:\n";
std::string s;
std::getline(std::cin, s);
try {
boost::regex reg(s);
std::cout << "Enter a string to be matched:\n";

std::getline(std::cin, s);

if (boost::regex_match(s, reg))
std::cout << "That's right!\n";
else
std::cout << "No, sorry, that doesn't match.\n";
}
catch(const boost::bad_expression & e) {
std::cout << "Invalid Regular Expression!" << std::endl;
std::cout << "Error::" << e.what() << std::endl;
}
return 0;
}


regex库在boost安装的时候已经生成了两个链接库文件:

libboost_regex-mgw45-mt-1_49.a

libboost_regex-mgw45-mt-d-1_49.a

可以如下编译:

[plain]
view plaincopyprint?

REM #可以这样编译
g++ -o test test.cpp -IC:\boost\include\boost-1_49 C:\boost\lib\libboost_regex-mgw45-mt-1_49.a
REM #也可以这样编译
g++ test.cpp -IC:\boost\include\boost-1_49 -LC:\boost\lib\ -lboost_regex-mgw45-mt-1_49

REM #可以这样编译
g++ -o test test.cpp -IC:\boost\include\boost-1_49  C:\boost\lib\libboost_regex-mgw45-mt-1_49.a
REM #也可以这样编译
g++ test.cpp -IC:\boost\include\boost-1_49 -LC:\boost\lib\ -lboost_regex-mgw45-mt-1_49

使用g++编译时随便选一个都行,如果不想每次都写-IC:\boost\include\boost-1_49,可以将其加入系统路径,即path变量中.

那么以下就可以了:

[cpp]
view plaincopyprint?

g++ test.cpp -LC:\boost\lib\ -lboost_regex-mgw45-mt-1_49

g++ test.cpp -LC:\boost\lib\ -lboost_regex-mgw45-mt-1_49

记住,采用第二种方法编译时,链接库的文件名前缀(lib)和后缀(.a)是不需要写上的.

将以下文件保存为testbuild.bat文件,用起来和makefile的效果一样,很方便:

[plain]
view plaincopyprint?

set BoostInclude=C:\boost\include\boost-1_49
set BoostLIB=C:\boost\lib
g++ -o test test.cpp -I$(BoostInclude) -L$(BoostLIB) -lboost_regex-mgw45-mt-1_49

set BoostInclude=C:\boost\include\boost-1_49
set BoostLIB=C:\boost\lib
g++ -o test test.cpp -I$(BoostInclude)  -L$(BoostLIB) -lboost_regex-mgw45-mt-1_49


双击一下testbuild.bat就行了.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: