• boost库的使用


    使用boost静态库

    boost静态库的使用方式很简单,在vs工程中设置好boost库头文件的目录以及库文件所在的目录。
    使用时引入对应的头文件即可,boost的auto-link机制将会自动帮我们包含对应的静态lib。
    一个比较有用的宏:

    #define BOOST_LIB_DIAGNOSTIC
    

    该宏让VC在编译时的output窗口中输出程序具体链接了哪些boost库以及链接顺序。

    测试代码:

    #include "stdafx.h"
    #define BOOST_LIB_DIAGNOSTIC
    
    #include <string>
    #include <iostream>
    #include <cassert>
    #include <boost/regex.hpp>
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	boost::regex reg("\d{2,3}");
    	assert(boost::regex_match("234",reg) == true);
    	return 0;	
    }
    

    编译信息如下:
    boost_auto_link

    从编译信息中可以明显看出程序链接了libboost_regex-vc100-mt-sgd-1_55.lib静态库。

    使用boost动态库

    如果想使用dll动态方式链接,需要预先定义宏:

    #define BOOST_ALL_DYN_LINK
    

    同样,此时boost也会默认帮我们包含对应的导入库lib。如果不想使用boost提供的auto-link机制,或者对它的自动链接不太放心的话(其实大可不必担心),可以预先定义宏:

    #define BOOST_ALL_NO_LIB	
    

    注:以上提到的宏必须在包含boost的头文件之前定义。vs工程的话,建议将宏定义在stdafx.h头文件中。

    测试代码:

    //stdafx.h文件
    #include <stdio.h>
    #include <tchar.h>
    #define BOOST_ALL_DYN_LINK		//boost库 使用dll动态链接
    #define BOOST_ALL_NO_LIB		//默认情况下boost的auto-link机制将会自动帮我们包含对应的静态lib 禁用auto-link机制
    #define BOOST_LIB_DIAGNOSTIC	//VC在编译时的output窗口中输出程序具体链接了哪些boost库以及链接顺序
    

    #include "stdafx.h"
    #include <string>
    #include <iostream>
    #include <boost/date_time/gregorian/gregorian.hpp>
    #include <boost/date_time/posix_time/posix_time.hpp>
    
    //引入导入库
    //或者直接在工程属性中添加具体依赖的库名称
    #ifdef _DEBUG
    #pragma comment(lib,"boost_date_time-vc100-mt-gd-1_55.lib")
    #else
    #pragma comment(lib,"boost_date_time-vc100-mt-1_55.lib")
    #endif
    
    using std::endl;
    using std::cout;
    using std::string;
    using namespace boost::gregorian;
    using namespace boost::posix_time;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	date d1(2015,8,25);
    	cout << to_iso_string(d1) << endl;
    	cout << boost::gregorian::to_iso_extended_string(d1) << endl; 
    
    	date current_day = day_clock::local_day();//得到当前日期
    	cout << "current day: " << to_iso_extended_string(current_day) << endl;
    
    	ptime p = second_clock::local_time(); //得到当前时间
    	cout << boost::posix_time::to_iso_extended_string(p) << endl;
    	return 0;
    }
    

    注:必须将相应的dll文件拷贝到可执行文件所在的目录中,否则程序无法执行。

  • 相关阅读:
    mysqllog
    清理:db上面的过期的binlog,释放磁盘空间。 (转)
    linux下shell命令trap
    mvc
    uci随笔
    luci 随笔
    shell脚本 整数比较
    lua学习
    OPENWRT make menuconfig错误之一
    openwrt 中make的使用
  • 原文地址:https://www.cnblogs.com/cmranger/p/4762040.html
Copyright © 2020-2023  润新知