regex_test.cpp -- learning boost.regex

boost.regex 库的用法,看来这可能是 boost 当中写法最“常规”的库之一了。

regex_test.cpp:

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

using namespace std;

// purpose:
// takes the contents of a file in the form of a string
// and searches for all the C++ class definitions, storing
// their locations in a map of strings/int's

typedef map<string, string::difference_type, less<string> > map_type;

const char* re =
// possibly leading whitespace:
"^[[:space:]]*"
// possible template declaration:
"(template[[:space:]]*<[^;:{]+>[[:space:]]*)?"
// class or struct:
"(class|struct)[[:space:]]*"
// leading declspec macros etc:
"("
"\\<\\w+\\>"
"("
"[[:blank:]]*\\([^)]*\\)"
")?"
"[[:space:]]*"
")*"
// the class name
"(\\<\\w*\\>)[[:space:]]*"
// template specialisation parameters
"(<[^;:{]+>)?[[:space:]]*"
// terminate in { or :
"(\\{|:[^;\\{()]*\\{)";

boost::regex expression(re);
map_type class_index;

bool regex_callback(const boost::match_results<string::const_iterator>& what)
{
// what[0] contains the whole string
// what[5] contains the class name.
// what[6] contains the template specialisation if any.
// add class name and position to map:
class_index[what[5].str() + what[6].str()] = what.position(5);
return true;
}

void load_file(string& s, istream& is)
{
s.erase();
s.reserve(is.rdbuf()->in_avail());
cout << s.capacity();
char c;
while(is.get(c))
{
if(s.capacity() == s.size())
s.reserve(s.capacity() * 3);
s.append(1, c);
}
}

int main(int argc, const char** argv)
{
string text;
for(int i = 1; i < argc; ++i)
{
cout << "Processing file " << argv[i] << endl;
ifstream fs(argv[i]);
load_file(text, fs);
// construct our iterators:
boost::sregex_iterator m1(text.begin(), text.end(), expression);
boost::sregex_iterator m2;
for_each(m1, m2, &regex_callback);
// copy results:
cout << class_index.size() << " matches found" << endl;
map_type::iterator c, d;
c = class_index.begin();
d = class_index.end();
while(c != d)
{
cout << "class \"" << (*c).first << "\" found at index: " << (*c).second << endl;
++c;
}
class_index.erase(class_index.begin(), class_index.end());
}

return 0;
}

//========== For Test =============
class Person
{};

template <class T>
class Temp
{
};

template <>
class Temp<string>
{
};

=================================================================================
cl /EHsc regex_test.cpp

regex_test regex_test.cpp

OUTPUT:

Processing file regex_test.cpp
153 matches found
class "Person" found at index: 2331
class "Temp" found at index: 2368
class "Temp<string>" found at index: 2397

<think>好的,用户现在遇到了一个关于libboost_regex.so的问题,显示这是一个符号链接指向libboost_regex.so.1.85.0。我需要分析这个问题,并给出解决方案。 首先,用户之前的问题是关于libboost_regex.so与aarch64-linux不兼容,现在的问题可能与此相关。用户提供的错误信息表明,系统中的libboost_regex.so是一个符号链接,指向具体的版本文件libboost_regex.so.1.85.0。这本身并不一定是问题,因为符号链接通常用于管理不同版本的库。但用户可能在运行程序时遇到问题,比如程序找不到正确的库版本,或者库的版本不兼容。 接下来,我需要考虑可能的原因。首先,符号链接是否正确指向了存在的库文件?例如,是否存在libboost_regex.so.1.85.0文件?如果不存在,可能是因为安装不完整或路径错误。其次,库文件本身是否与当前系统架构兼容?比如用户之前的问题涉及aarch64架构,可能这个库是为x86_64编译的,导致不兼容。第三,动态链接器是否能找到该库?可能需要检查LD_LIBRARY_PATH或ldconfig缓存。 解决方案方面,首先应该验证符号链接的目标文件是否存在。使用ls -l命令查看链接指向的文件是否存在。如果不存在,可能需要重新安装Boost库,或者手动创建正确的链接。其次,检查库文件的架构,使用file命令确认是否与系统架构匹配。如果不匹配,需要安装或编译对应架构的库。然后,确保动态链接器知道库的位置,可能需要运行ldconfig或者设置LD_LIBRARY_PATH。另外,用户可能需要确认安装的Boost版本是否正确,特别是如果程序依赖特定版本的话。 另外,用户之前的问题提到了手动编译Boost库,可能这次的问题出现在自定义安装的路径中。例如,用户可能将Boost安装到/data/nfs_rt19/ziqi/boost/lib目录,但系统默认路径是/usr/lib,导致程序找不到库。这时候需要确保安装路径被加入到动态链接配置中,或者在编译时指定正确的库路径。 还需要考虑权限问题,确保库文件有正确的读取权限。此外,如果用户是在交叉编译或容器环境中,可能需要检查环境配置是否正确,库是否被正确挂载或安装。 最后,总结可能的解决步骤:检查符号链接目标是否存在,验证库的架构,更新动态链接缓存,重新安装或编译Boost,检查环境变量和配置。同时,根据用户之前的交互,可能需要建议用户安装特定版本的Boost或调整编译参数。 </think>您看到的 `/data/nfs_rt19/ziqi/boost/lib/libboost_regex.so: symbolic link to libboost_regex.so.1.85.0` 是 **正常现象**,因为 Linux 系统中动态链接库通常通过符号链接管理版本。以下是排查和解决潜在问题的步骤: --- ### 1. **确认符号链接目标文件是否存在** - 运行以下命令检查目标文件是否存在: ```bash ls -l /data/nfs_rt19/ziqi/boost/lib/libboost_regex.so* ``` - 如果输出类似: ``` lrwxrwxrwx 1 user group 24 Jan 1 00:00 libboost_regex.so -> libboost_regex.so.1.85.0 -rwxr-xr-x 1 user group ... libboost_regex.so.1.85.0 ``` 则符号链接有效。 - **若目标文件缺失**(如只有 `libboost_regex.so` 而没有 `libboost_regex.so.1.85.0`): - **原因**:Boost 库安装不完整或编译中断。 - **解决**:重新安装或编译 Boost,确保生成完整的库文件。 --- ### 2. **验证库文件的架构兼容性** - 检查 `libboost_regex.so.1.85.0` 是否适配当前系统架构(如 aarch64): ```bash file /data/nfs_rt19/ziqi/boost/lib/libboost_regex.so.1.85.0 ``` - **预期输出**(以 aarch64 为例): ``` ELF 64-bit LSB shared object, ARM aarch64 ``` - **若显示 x86-64 或其他架构**: - **原因**:库文件是为其他架构编译的,导致与当前系统不兼容。 - **解决**:重新编译 Boost 时指定目标架构(见下方步骤)。 --- ### 3. **确保动态链接器识别该库** - **问题场景**:即使库存在且架构正确,程序仍可能因未注册动态链接缓存而报错。 - **解决方法**: 1. 将库路径添加到 `ldconfig` 配置: ```bash echo "/data/nfs_rt19/ziqi/boost/lib" | sudo tee /etc/ld.so.conf.d/boost.conf sudo ldconfig ``` 2. 验证缓存是否生效: ```bash ldconfig -p | grep libboost_regex ``` --- ### 4. **重新编译 Boost 库(适用于 aarch64)** - **下载并解压 Boost 源码**: ```bash wget https://boostorg.jfrog.io/artifactory/main/release/1.85.0/source/boost_1_85_0.tar.gz tar -zxvf boost_1_85_0.tar.gz cd boost_1_85_0 ``` - **指定架构编译**(以 aarch64 为例): ```bash ./bootstrap.sh --prefix=/data/nfs_rt19/ziqi/boost ./b2 architecture=arm address-model=64 link=shared install ``` - **关键参数说明**: - `architecture=arm`:指定 ARM 架构。 - `address-model=64`:生成 64 位库(aarch64)。 - `link=shared`:生成动态链接库(`.so`)。 --- ### 5. **检查环境变量 `LD_LIBRARY_PATH`** - 如果库路径未全局注册,可通过环境变量临时指定: ```bash export LD_LIBRARY_PATH=/data/nfs_rt19/ziqi/boost/lib:$LD_LIBRARY_PATH ``` - **验证**:运行程序前执行上述命令,观察是否解决问题。 --- ### 6. **权限问题排查** - 确保库文件有正确的读取权限: ```bash chmod 755 /data/nfs_rt19/ziqi/boost/lib/libboost_regex.so.1.85.0 ``` --- ### 示例修复流程(Ubuntu aarch64) ```bash # 1. 安装依赖 sudo apt-get update sudo apt-get install build-essential g++ python3-dev # 2. 编译并安装 Boost(指定 aarch64) cd boost_1_85_0 ./bootstrap.sh --prefix=/usr/local ./b2 architecture=arm address-model=64 link=shared install # 3. 更新动态链接缓存 sudo ldconfig # 4. 验证库信息 file /usr/local/lib/libboost_regex.so.1.85.0 ldconfig -p | grep libboost_regex ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值