简体   繁体   English

如何让PCRE与C ++一起使用?

[英]How do I get PCRE to work with C++?

This is a newbie question but I hope I can express my question as clearly as possible. 这是一个新手问题,但我希望我能尽可能清楚地表达我的问题。

I'm trying to do pattern matching in C++. 我正在尝试用C ++进行模式匹配。

I've downloaded the Win32 version of PCRE from here and I've placed the downloaded pcre3.dll and pcreposix3.dll files into the folder of Dev-CPP's lib folder (I'm using Bloodshed Dev-C++ 4.9.9 IDE). 我从这里下载了WinRE版本的PCRE,并将下载的pcre3.dll和pcreposix3.dll文件放入Dev-CPP的lib文件夹(我使用的是Bloodshed Dev-C ++ 4.9.9 IDE)。

I've also downloaded a pcrecpp.h header file and have it in the same directory I'm writing the following code (not writing actually. I'm coping example code from a PDF tutorial named PCRE- Perl Compatible Regular Express). 我还下载了一个pcrecpp.h头文件并将它放在同一个目录中我正在编写以下代码(实际上并没有写。我正在处理来自PDF教程的示例代码,名为PCRE-Perl Compatible Regular Express)。

But I can't get it to work. 但我无法让它发挥作用。 The code is as follows: 代码如下:

    #include <iostream>
    #include <string>
    #include <pcrecpp.h>

    using namespace std;

    int main()
    {
       int i;
       string s;
       pcrecpp::RE re("(\\w+):(\\d+)");
       if (re.error().length() > 0) {
          cout << "PCRE compilation failed with error: " << re.error() << "\n";
       }
       if (re.PartialMatch("root:1234", &s, &i))
       cout << s << " : " << i << "\n";
    }

When I compile the code, Dev-C++ gives me a lot of errors including: "`pcrecpp' has not been declared" and "RE" undeclared. 当我编译代码时,Dev-C ++给了我很多错误,包括:“`pcrecpp'尚未声明”和“RE”未声明。

How should I deal with the downloaded files and fix my problem? 我该如何处理下载的文件并修复我的问题? Or is there something obvious that I'm missing? 或者有什么明显的东西让我失踪?

If you specify the file for #include with angle brackets ( <> ), then the compiler will only look for that header in the locations for external libraries, in so far as the compiler is aware of them. 如果为#include指定带有尖括号( <> )的文件,那么只要编译器知道它们,编译器就只会在外部库的位置中查找该标题。
If you instead use quotation marks ( "" ), then the compiler will also look in the locations for the current project, which typically includes the current directory. 如果您改为使用引号( "" ),则编译器还将查找当前项目的位置,该项目通常包括当前目录。

The quick fix for your current problem is to use 您当前问题的快速解决方法是使用

 #include "pcrecpp.h" 

The alternative is to tell the compiler where it can find the headers of the PCRE library. 另一种方法是告诉编译器它可以在哪里找到PCRE库的头文件。
You will have to tell the compiler where it can find the headers of the PCRE library. 您必须告诉编译器它可以在哪里找到PCRE库的头文件。 How to do this differs from build system to build system, but if you are using an IDE, then there should be an option somewhere to specify the 'Include directories'. 如何做到这一点不同于构建系统到构建系统,但如果您使用的是IDE,那么应该有一个选项来指定“包含目录”。 This is where you add the directory of the PCRE headers (with full path). 您可以在此处添加PCRE标头的目录(带完整路径)。


As a side-note: When the compiler gives you a large number of errors and warnings, always start with fixing the first one. 作为旁注:当编译器为您提供大量错误和警告时,请始终先修复第一个错误和警告。 I would guess that in this case it was something like "unable to find header: pcrecpp.h". 我猜想在这种情况下它就像“无法找到标题:pcrecpp.h”。
It is often the case that, if the compiler tries to continue after encountering a problem, more problems are found that are follow-on problems of the first one. 通常的情况是,如果编译器在遇到问题后尝试继续,则会发现更多问题,这些问题是第一个问题的后续问题。 When the first problem is fixed, these also magically disappear. 当第一个问题得到解决时,这些问题也会神奇地消失。

         cout << “PCRE compilation failed with error: “ << re.error() << “\n”;

I just copied your code and tried to compile it. 我刚刚复制了你的代码并尝试编译它。 I got the same error as you reported. 我收到了与您报告的相同的错误。 The problem is that string you put to cout is not properly started/ended. 问题是你输入cout的字符串没有正确启动/结束。 You should use real " instead of marks which looks like double quotes (") but it is not. 你应该使用真正的“而不是看起来像双引号(”)的标记,但事实并非如此。 If you fix it, your code should compile w/o any error. 如果你修复它,你的代码应该编译没有任何错误。

g++ -lpcrecpp ...... g ++ -lpcrecpp ......

you need to add '-lpcrecpp' to g++ command 你需要在g ++命令中添加'-lpcrecpp'

You have included 你已经包括在内

#include <pcrecpp.h> 

1st point to check But is file in the inlcude path of your code. 要检查的第一点但是在代码的inlcude路径中是文件。 Did you download the installable ? 你下载了可安装的吗? Check where it has been installed on your machine. 检查机器上的安装位置。

2nd point is to check do you have the library paths defined, so that they can be resolved during compiling and linking. 第二点是检查是否定义了库路径,以便在编译和链接期间解析它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM