【原创&交流】在CodeBlocks环境下使用boost库
链接:在CodeBlocks环境下使用boost库
首先请先编译或安装boost库,使用CodeBlocks编译boost库具体见:Boost库在CodeBlocks环境下的编译。
以下内容主要翻译自:BoostWindows Quick Ref ,我所用的编译环境为Win 7家庭版,CodeBlocks V10.05, mingw32 v4.4.1,boostv1.42
1. 为boost库创建一个CodeBlocks全局变量
1)打开“Settings”菜单——>"Global variables..."菜单项,弹出如下对话框,单击下图的对话框上的New按钮,
2) 在弹出的对话框上输入你要新建的全部变量名,例如boost,如下图:
3)设置全局变量的base目录、lib目录和include目录,具体如下图:
Base目录为你的boost安装目录,也是你在编译boost库的—prefix选项的参数值,如:
你的bjam命令为:
bjam install--toolset=gcc--prefix="C:\zjc\PluginFramework\boost_1_42_0"--build-type=complete
那么base目录为C:\zjc\PluginFramework\boost_1_42_0
Include和lib目录很容易理解,就是boost库的头文件和库文件所在的目录。
现在我们使用一下这个全局变量,
1. CodeBlocks新建一个控制台工程TestConsole,敲入如下代码:
#include <stdlib.h> #include <iostream> using std::cout; using std::wcout; using std::endl; #include <string> using std::string; using std::wstring; #include <boost/algorithm/string.hpp> #include <boost/filesystem/path.hpp> #include "boost/filesystem/operations.hpp" #include <boost/format.hpp>int main(){ // ANSI字符的格式化 cout << boost::format( "%1% %2%" ) % "Hell" % "Low" <<endl; string s1 = boost::str( boost::format( "%2% %1%" ) % "Hell" % "Low" ); cout << s1 << endl; // UNICODE字符的格式化 wcout << boost::wformat( L"%s %X" ) % L"-1 is" % -1 << endl; wstring s2 = boost::str( boost::wformat( L"%2$s %1$.2f" ) % 3.141592 % L"Version" ); wcout << s2 << endl; // 获取应用程序所在目录(ANSI字符),注意是boost::filesystem::path string AnsiPath = boost::filesystem::initial_path<boost::filesystem::path>().string(); cout<<AnsiPath<<endl; // 获取应用程序所在目录(UNICODE字符),注意是boost::filesystem::wpath wstring UnicodePath = boost::filesystem::initial_path<boost::filesystem::wpath>().string(); wcout<<UnicodePath<<endl; system("PAUSE"); return 0;}