tags:

views:

70

answers:

2

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?

Thanks for any guidance :)

A: 

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.

DumbCoder
@DumbCoder, 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
Why the downvote ? And what is wrong here ?
DumbCoder
+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
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
@DumbCoder, 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
+1  A: 
Bart van Ingen Schenau
@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
@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.
Bart van Ingen Schenau
@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
@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.
Bart van Ingen Schenau
@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
@Mike: That means the libraries you downloaded are not being understood by your compiler/linker. What surprises me a bit is that you have both .lib files and .a files. .lib files are typically used on Windows, while .a files are typical for Linux/unis systems. It might be you downloaded a mixed-platform library. The most important issue is the "File format not recognized" error. Try removing the files that cause that error (if they are not mentioned in the message, you will have to experiment a bit which files it refers to).
Bart van Ingen Schenau
@Bart, I've done a little experiment. If I link only "pcre-bcc.lib", there will be no linker error but "File format not recognized" error. So it seems to me "pcre-bcc.lib" is the only relevant file but judging from its file name, might it be a lib for bcc compiler?
Mike
The PCRE.org says this: The developer sub-package <package>-<version>-lib.zip contains include files and libraries for developing programs. The libraries are: a static GCC library (suffix: .a) and a GCC (suffix: .dll.a) import library for using the dynamic link library (DLL) that is contained in the binary sub-package.For developing applications with the libraries, you will usually also need the LibGwC library.
Mike
But you just mention that .a files are normally for Unix-like systems. Is there something that I'm still missing? Well, it's still frustrating but I'm glad that I've now learned how to link libraries :) Thanks for the patient guidance :)
Mike