如何让 PCRE 与 C++ 一起使用?
这是一个新手问题,但我希望我能尽可能清楚地表达我的问题。
我正在尝试用 C++ 进行模式匹配。
我从 此处 下载了 PCRE 的 Win32 版本,并将下载的 pcre3 放置在.dll 和 pcreposix3.dll 文件放入 Dev-CPP 的 lib 文件夹中(我使用的是 Bloodshed Dev-C++ 4.9.9 IDE)。
我还下载了一个 pcrecpp.h 头文件,并将其放在我正在编写以下代码的同一目录中(实际上不是编写。我正在处理名为 PCRE-Perl Compatible Regular Express 的 PDF 教程中的示例代码)。
但我无法让它发挥作用。代码如下:
#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";
}
当我编译代码时,Dev-C++给了我很多错误,包括:“`pcrecpp'尚未声明”和“RE”未声明。
我应该如何处理下载的文件并解决我的问题?或者我遗漏了一些明显的东西?
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++.
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).
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).
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.
How should I deal with the downloaded files and fix my problem? Or is there something obvious that I'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用尖括号 (
<>
) 为#include
指定文件,那么编译器将仅在外部库的位置中查找该标头,因此只要编译器知道它们。如果您改为使用引号 (
""
),则编译器还将查找当前项目的位置,通常包括当前目录。解决当前问题的快速方法是使用另一种方法是告诉编译器在哪里可以找到 PCRE 库的标头。您必须告诉编译器在哪里可以找到 PCRE 库的标头。
如何做到这一点因构建系统而异,但如果您使用的是 IDE,那么应该有一个选项来指定“包含目录”。您可以在此处添加 PCRE 标头的目录(带有完整路径)。
旁注:当编译器向您提供大量错误和警告时,请始终从修复第一个错误和警告开始。我猜想在这种情况下它类似于“无法找到标头:pcrecpp.h”。
通常情况下,如果编译器在遇到问题后尝试继续,则会发现更多问题,这些问题是第一个问题的后续问题。当第一个问题解决后,这些问题也会神奇地消失。
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.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 useThe alternative is to tell the compiler where it can find the headers of the PCRE library.You will have to tell the compiler where it can find the headers of the PCRE library.
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'. This is where you add the directory of the PCRE headers (with full path).
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".
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.
你需要将'-lpcrecpp'添加到g++命令中
you need to add '-lpcrecpp' to g++ command
我刚刚复制了你的代码并尝试编译它。我遇到了与您报告的相同的错误。
问题是您放入 cout 的字符串未正确开始/结束。您应该使用真正的 " 而不是看起来像双引号 (") 的标记,但事实并非如此。如果修复它,您的代码应该可以编译,不会出现任何错误。
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. 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.
您已包含
要检查的第一点,但文件位于代码的包含路径中。你下载了可安装的吗?检查它已安装在您的计算机上的位置。
第二点是检查是否定义了库路径,以便在编译和链接期间可以解析它们。
You have included
1st point to check But is file in the inlcude path of your code. 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.