9

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?

3
  • On an unrelated note, I'd advise you to get a more updated IDE (and probably also a more recent version of MinGW, if you're using the one bundled with it); Dev-C++ development stopped few years ago, and AFAIK there are several known bugs. Consider moving to Code::Blocks/CodeLite/MSVC++ Express/..., all of them are good, free IDEs and ship with recent compilers. Oct 13, 2010 at 10:41
  • @Matteo Italia, thanks for the recommdendations. I'll take a look at them.
    – Mike
    Oct 14, 2010 at 0:08
  • This is a wrapper of the new PCRE2 (revised version of PCRE) library. It requires C++11 though.
    – Jahid
    Jan 1, 2016 at 15:40

4 Answers 4

6

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 use

#include "pcrecpp.h"

The 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.

9
  • @Bart van Ingen Schen, thanks for the guidance :) I'm very new to C++. Well, I tried changing the brackets to the double quotes, but it wouldn't work. The error messages include "\/pcrecpp.h E:\E pcrecpparg.h: No such file or directory. " I think maybe the problem is I don't know how to link the PCRE library to the compiler or something similar. I've downloaded the the developer's libraries of PCRE and placed all the downloaded files in the Dev-C++'s lib folder but maybe this is the wrong place or I need to do something else...
    – Mike
    Oct 13, 2010 at 10:13
  • @Mike: Apparently, the pcrecpp.h file uses the #include <> syntax as well to pull in more headers. I have adapted my answer. What you have to do is tell the compiler where to find the relevant headers. Oct 13, 2010 at 10:28
  • @Bart van Ingen Schenau, you're right. The problem had something to do with the include directory! I made the mistake of placing the header files in the Dev-C++'s lib directory rather than the include directory. Now I've corrected this problem. But I'm receiving new error messages from the compiler. The first one is "[Linker error] undefined reference to `pcrecpp::no_arg'". Frustrated :( But thanks for the help!
    – Mike
    Oct 13, 2010 at 12:38
  • @Mike: You should have one or more .lib files with the PCRE library. Add those to your project. Unlike in languages like Java, C and C++ compilers don't try to guess where files are located or which libraries you need. You have to explicitly specify it all. Oct 13, 2010 at 12:59
  • @Bart van Ingen Schenau, I've explicitly specified the PCRE library by adding the downloaded lib files ("pcreposix-bcc.lib","libpcre.dll.a","libpcreposix.dll.a","pcre.lib","pcre-bcc.lib","pcreposix.lib") as the Linker parameters in the Dev-C++ Project Options. Now the Linker error problem is solved. Thanks:) But I'm receiving 3 error messages: 1. File format not recognized 2. ld returned 1 exit status 3. E:\Makefile.win [Build Error] [Project1.exe] Error 1.
    – Mike
    Oct 14, 2010 at 0:06
2

g++ -lpcrecpp ......

you need to add '-lpcrecpp' to g++ command

2
         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. 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.

0

You have included

#include <pcrecpp.h> 

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.

4
  • thanks for the guidance. and no there's no installable. I've downloaded the binaries and the developer's libraries and I've placed all the downloaded files in the Dev-C++'s lib folder. But maybe this is the wrong place. But I don't how to link the library...still trying to figure out...
    – Mike
    Oct 13, 2010 at 10:15
  • +1! I made the mistake of copying the downloaded files into the Dev-C++'s lib folder rather than the include folder. You're absolutely right. When I tried replacing them into the Dev-C++'s include folder, the original error messages just disappear. BIG THANKS for pointing this out! I didn't realize that until now. Silly me! Well, there're new problems though. When I corrected the path problem and even commented out the PCRE error handling line, I'm now receving new error messages. They include this: " [Linker error] undefined reference to `pcrecpp::no_arg' ".
    – Mike
    Oct 13, 2010 at 12:34
  • That is a linking error. Did you check the path of the lib files also ? Your compiler isn't able to find the proper libraries to link to. Set the path to the proper lib files, and it should go away.
    – DumbCoder
    Oct 13, 2010 at 12:37
  • this time I've added the downloaded lib files ("pcreposix-bcc.lib","libpcre.dll.a","libpcreposix.dll.a","pcre.lib","pcre-bcc.lib","pcreposix.lib") as the Linker parameters in the Dev-C++ Project Options. And the Linker errors are gone just as you foresaw it:) But now I'm receiving "File format not recognized" error. Any ideas as to what else I'm doing wrong. Thanks.
    – Mike
    Oct 13, 2010 at 23:58

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.