boost c++ lib on linux (1) - regex example with binary boost lib Regex

在上一篇boost学习文章《boost c++ library on linux初体验》中,主要讲了boost的非二进制库的使用,并实现了一个helloworld程序,此外还简单介绍了boost库的基本使用方法以及我在搭建自己的boost c++ on linux的编程环境过程中所遇到的问题和解决方案。

在本篇文章中,简要记录一下boost c++官网Getting started文档中的boost install以及在需要用到binary library时的demo程序。

boost installation & building

boost编译和安装还是比较简单的,有两种方式:easy install以及custom install。easy install就是默认安装,将会编译安装所有的模块库文件。custom install可以根据用户自己的需要选择安装部分的二进制库文件即可。安装过程的基本原理是boost的安装脚本调用g++编译器编译出lib的二进制文件,然后将二进制的.so,.a的文件拷贝到lib目录下面。
关于boost的编译和安装还有部分东西没高明白,貌似安装过程中有部分组建的安装出错了,还得详细研究一下。不过可以到/usr/local(默认安装路径)目录下的lib文件夹下看看有哪些boost二进制库已经成功安装。均为libboost_开头的二进制文件。

regex example

/*************************************************************************
    > File Name: regex_example.cpp
    > Author: Liu Xin
    > Mail: liu_x_0625@126.com 
    > Created Time: 2012年07月23日 星期一 21时08分28秒
	> Description: 
		> 提取邮件文本中的主题在console中输出
 ************************************************************************/


#include<boost/regex.hpp>
#include<iostream>
#include<string>
using namespace std;


int main()
{
	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[0]: " << matches[0] << std::endl;
			std::cout << "matches[1]: " << matches[1] << std::endl;
			std::cout << "matches[2]: " << matches[2] << std::endl;
		}else{
			std::cout << "mail text line: " << line << " doesn't match the subject pattern" << std::endl;
		}
	}
	return 0;
}

原教程中的编译命令为:
$ c++ -I path/to/boost_1_50_0 example.cpp -o example  ~/boost/stage/lib/libboost_regex-gcc34-mt-d-1_36.a

或者
$ c++ -I path/to/boost_1_50_0 example.cpp -o example -L~/boost/stage/lib/ -lboost_regex-gcc34-mt-d-1_36

前者应该链接的静态库文件,后者是链接的动态链接库文件。但是,我在编译链接时发现提示找不到lib文件,于是查看/usr/local/lib下面并不存在该文件,只有libboost_regex.so和libboost_regex.a文件,于是改成这两个文件后编译成功。原因可能是教程使用的boost的版本比较老的缘故,lib的组织方式和命名方式也有所不同了。
我的编译命令:
g++ -I /usr/local/boost_1_50_0/ regex_example.cpp  -o regex_example /usr/local/boost_1_50_0/stage/lib/libboost_regex.a

编译完后,在regex_example.cpp所在的文件夹下面生成了二进制文件regex_example的可执行程序。在当前文件夹下面新建一个文本文件作为测试的邮件文本内容:
To: George Shmidlap
From: Rita Marlowe
Subject: Will Sucess Spoil Rock Hunter?
---
See subject.
保存为mail.txt

运行程序:
 ./regex_example < mail.txt
程序输出:
mail text line: To: George Shmidlap doesn't match the subject pattern
mail text line: From: Rita Marlowe doesn't match the subject pattern
matches[0]: Subject: Will Sucess Spoil Rock Hunter?
matches[1]: 
matches[2]:  Will Sucess Spoil Rock Hunter?
mail text line: --- doesn't match the subject pattern
mail text line: See subject. doesn't match the subject pattern
mail text line:  doesn't match the subject pattern

程序逐行读取文件内容,用正则表达式匹配行,提取邮件主题信息。

以上是今天2小时的boost学习内容,问题是boost安装还没有弄得的太清楚,有些error log。不过暂时不影响当前的使用。
至此,已经将Boost Getting started已经学玩,接下来我下载了一本讲boost开发的电子书《Boost程序库完全开发指南——深入C++标准库》,将逐步深入学习boost常用的库的使用。

最近在改一个基于linux下面c++开发的对比测试工具,现学现卖学习了Makefile的简单的编写,在本例中自己试着编写了一个Makefile文件如下:
LDFLAGS=/usr/local/lib/libboost_regex.a

CPPFLAGS=-I /usr/local/boost_1_50_0/

OBJS=regex_example.o

PROGRAM=regex_example

$(PROGRAM): regex_example.o
	g++ $(OBJS) -o $(PROGRAM) $(LDFLAGS)

regex_example.o: regex_example.cpp
	g++ $(CPPFLAGS) -c regex_example.cpp -o $(OBJS)

clean:
	rm *.o $(PROGRAM)

The following variables are supported and can be set either from the command line as , or via or :cmake -DVARIABLE=VALUE ..ccmakecmake-gui BOOST_INCLUDE_LIBRARIES A semicolon-separated list of libraries to include into the build (and installation.) Defaults to empty, which means "all libraries". Example: .filesystem;regex BOOST_EXCLUDE_LIBRARIES A semicolon-separated list of libraries to exclude from the build (and installation.) This is useful if a library causes an error in the CMake configure phase. BOOST_ENABLE_MPI Set to ON if Boost libraries depending on MPI should be built. BOOST_ENABLE_PYTHON Set to ON if Boost libraries depending on Python should be built. CMAKE_BUILD_TYPE For single-configuration generators such as Makefile and Ninja (the typical case under POSIX operating systems), controls the build variant (Debug or Release.) The default when building Boost is set to Release. For multi-configuration generators such as the Visual Studio generators, is ignored; the desired configuration is set at build (or install) time, with the option to and .CMAKE_BUILD_TYPE--configcmake --buildcmake --install For more information, see the CMake documentation on build configurations. CMAKE_INSTALL_PREFIX A standard CMake variable that determines where the headers and libraries should be installed. The default when building Boost is set to under Windows, otherwise.C:/Boost/usr/local CMAKE_INSTALL_INCLUDEDIR Directory in which to install the header files. Can be relative to . Default .CMAKE_INSTALL_PREFIXinclude CMAKE_INSTALL_BINDIR Directory in which to install the binary artifacts (executables and Windows DLLs.) Can be relative to . Default .CMAKE_INSTALL_PREFIXbin CMAKE_INSTALL_LIBDIR Directory in which to install the compiled libraries. Can be relative to . Default .CMAKE_INSTALL_PREFIXlib CMAKE_INSTALL_DATADIR Directory in which to install the data files (e.g. debugger visualizers). Can be relative to . Default .CMAKE_INSTALL_PREFIXshare BOOST_INSTALL_CMAKEDIR Directory in which to install the CMake configuration files. Default .lib/cmake BOOST_INSTALL_LAYOUT Boost installation layout. Can be one of , , or . The default is under Windows, and otherwise.systemtaggedversionedversionedsystem versioned produces library names of the form , containing the toolset (compiler) name and version, encoded build settings, and the Boost version. (The extension is under Windows, or under Linux, and or under macOS.)libboost_timer-vc143-mt-gd-x64-1_82.lib.lib.a.so.a.dylib tagged produces library names of the form ; only the build settings are encoded in the name, the toolset and the Boost version are not.libboost_timer-mt-gd-x64.lib system produces library names of the form (or , , .)libboost_timer.liblibboost_timer.alibboost_timer.solibboost_timer.dylib BOOST_INSTALL_INCLUDE_SUBDIR When is , headers are installed in a subdirectory of (to enable several Boost releases being installed at the same time.) The default for release e.g. 1.81 is .)BOOST_INSTALL_LAYOUTversionedCMAKE_INSTALL_INCLUDEDIR/boost-1_81 BOOST_RUNTIME_LINK Whether to use the static or the shared C++ runtime libraries under Microsoft Visual C++ and compatible compilers. (The available values are and and the default is .)sharedstaticshared BUILD_TESTING A standard CMake variable; when ON, tests are configured and built. Defaults to OFF. BUILD_SHARED_LIBS A standard CMake variable that determines whether to build shared or static libraries. Defaults to OFF. BOOST_STAGEDIR The directory in which to place the build outputs. Defaults to the subdirectory of the current CMake binary directory.stage The standard CMake variables CMAKE_RUNTIME_OUTPUT_DIRECTORY, CMAKE_LIBRARY_OUTPUT_DIRECTORY, and CMAKE_ARCHIVE_OUTPUT_DIRECTORY are set by default to , , and , respectively.${BOOST_STAGEDIR}/bin${BOOST_STAGEDIR}/lib${BOOST_STAGEDIR}/lib CMAKE_CXX_VISIBILITY_PRESET C++ symbol visibility (one of , , , ). The default is set to to match .defaulthiddenprotectedinternalhiddenb2 CMAKE_C_VISIBILITY_PRESET C symbol visibility (one of , , , ). The default is set to to match .defaulthiddenprotectedinternalhiddenb2 CMAKE_VISIBILITY_INLINES_HIDDEN Whether inline functions should have hidden visibility. The default is set to to match .ONb2 cmake如何根据这些英文文档配置boost
最新发布
01-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值