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

来源:互联网 发布:云南边境贩毒知乎 编辑:程序博客网 时间:2024/06/05 17:20

由于已安装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?
  1. REM 生成b2.exe和bjam文件::  
  2. bootstrap.bat gcc  

3.将boost安装到指定目录

[plain] view plaincopyprint?
  1. REM 安装到一个指定的目录,比如 C:\boost :  
  2. bjam install --toolset=gcc --prefix=C:\boost  


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

[plain] view plaincopyprint?
  1. b2 install --toolset=gcc  

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


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

[sql] view plaincopyprint?
  1. C:\boost\include\boost-1_49  
  2. C:\boost\lib  


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

 例如编译regex库的例子:

[cpp] view plaincopyprint?
  1. #include <boost/regex.hpp>   
  2. #include <string>   
  3. #include <iostream>   
  4.   
  5. int main() {  
  6.     std::cout << "Enter a regular expression:\n";  
  7.     std::string s;  
  8.     std::getline(std::cin, s);  
  9.     try {  
  10.         boost::regex reg(s);  
  11.         std::cout << "Enter a string to be matched:\n";  
  12.   
  13.         std::getline(std::cin, s);  
  14.   
  15.         if (boost::regex_match(s, reg))  
  16.             std::cout << "That's right!\n";  
  17.         else  
  18.             std::cout << "No, sorry, that doesn't match.\n";  
  19.     }  
  20.     catch(const boost::bad_expression & e) {  
  21.         std::cout << "Invalid Regular Expression!" << std::endl;  
  22.         std::cout << "Error::" << e.what() << std::endl;  
  23.     }  
  24.     return 0;  
  25. }  


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

libboost_regex-mgw45-mt-1_49.a

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

可以如下编译:

[plain] view plaincopyprint?
  1. REM #可以这样编译  
  2. g++ -o test test.cpp -IC:\boost\include\boost-1_49  C:\boost\lib\libboost_regex-mgw45-mt-1_49.a  
  3. REM #也可以这样编译  
  4. 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?
  1. g++ test.cpp -LC:\boost\lib\ -lboost_regex-mgw45-mt-1_49  

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

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

[plain] view plaincopyprint?
  1. set BoostInclude=C:\boost\include\boost-1_49  
  2. set BoostLIB=C:\boost\lib  
  3. g++ -o test test.cpp -I$(BoostInclude)  -L$(BoostLIB) -lboost_regex-mgw45-mt-1_49  


双击一下testbuild.bat就行了.

原创粉丝点击