PDA

View Full Version : PCRE problem




Soulstorm
Mar 23, 2008, 07:17 AM
I need a regular expression library so, I downloaded PCRE. I installed it using firstly ./configure, then "sudo make install" and it seems to have been installed correctly.

But when I make a C++ command line tool in Xcode 3.0, I type "#include <pcre.h>" but it says it can't find the file? Can anyone tell me what am I doing wrong?



Gelfin
Mar 23, 2008, 12:31 PM
XCode, for whatever odd reason, seems not to be pulling /usr/local/include into its default include search paths. Add it to the header search path in the project settings and it should work, though you really shouldn't have to do that.

Soulstorm
Mar 27, 2008, 02:59 AM
XCode, for whatever odd reason, seems not to be pulling /usr/local/include into its default include search paths. Add it to the header search path in the project settings and it should work, though you really shouldn't have to do that.

Actually, that didn't work. I told it to include "/usr/local/include" but it still can't find the file.

lazydog
Mar 27, 2008, 06:51 AM
I guess if all fails you could revert to the standard stuff in <regex.h>.

b e n

alaceo
Mar 27, 2008, 11:15 AM
You may have already checked for it, but is it <pcre.h> or <pcre/pcre.h>? It could've been created in its own directory in the include folder. If that doesn't work, maybe try putting the flags in the makefile.

lazydog
Mar 27, 2008, 11:29 AM
I just spotlighted pcre.h and found it buried deep in /Developer/SDKs/MacOSX10.5sdk/usr/include/php/ext/pcre/pcrelib. So perhaps you don't need to compile your own version.

b e n

EDIT: Actually I might have installed PHP on my system, in which case forget what I've just said!

Soulstorm
Mar 27, 2008, 12:58 PM
I just spotlighted pcre.h and found it buried deep in /Developer/SDKs/MacOSX10.5sdk/usr/include/php/ext/pcre/pcrelib. So perhaps you don't need to compile your own version.

b e n

EDIT: Actually I might have installed PHP on my system, in which case forget what I've just said!

I need regular expressions for C++, not PHP, unfortunately.

Soulstorm
Mar 27, 2008, 01:40 PM
I fixed the problem. Something was wrong with my project file. I created a new project and I set the header search path to usr/local/include and it worked just fine with pcre.

lazydog
Mar 27, 2008, 01:48 PM
I need regular expressions for C++, not PHP, unfortunately.

Yup I understood that. What I meant was that the pcre header file and library were already installed on my System.

But using Posix regex (which is definitely part of a standard developer installation) is probably more portable anyway… I think.

b e n

Soulstorm
Mar 27, 2008, 02:12 PM
I was too fast to say that it worked. It didn't.

Just including the header works in Xcode, which means that it found the necessary header. However, the following code does not work

#include <iostream>
#include <pcre.h>

#define OVECCOUNT 30

int main (int argc, char * const argv[]) {

pcre *re;
const char *error;
int erroroffset;
int ovector[OVECCOUNT];
int rc;

char regex[] = "^From: ([^@]+)@([^\r]+)";
char data[] = "From: regular.expressions@example.com\r\n";
re = pcre_compile(regex, 0, &error, &erroroffset, NULL);
if (!re) {
fprintf(stderr, "PCRE compilation failed at expression offset %d: %s\n", erroroffset, error);
return 1;
}

rc = pcre_exec(re, NULL, data, strlen(data), 0, 0, ovector, OVECCOUNT);

if (rc < 0) {
switch (rc) {
case PCRE_ERROR_NOMATCH:
printf("No match found in text\n");
break;
default:
printf("Match error %d\n", rc);
break;
}
return 1;
}

if (rc < 3)
{
printf("Match did not catch all the groups\n");
return 1;
}

char *name_start = data + ovector[2];
int name_length = ovector[3] - ovector[2];
char *domain_start = data + ovector[4];
int domain_length = ovector[5] - ovector[4];

printf("Mail from: %.*s domain: %.*s\n", name_length, name_start, domain_length, domain_start);
return 0;
}


It prompts me with 2 linker errors. It says that it is unable to find the symbols "pcre_exec" and "pcre_compile". However, code completion works for those functions!

I am stuck again.

Gelfin
Mar 27, 2008, 02:31 PM
You'll also need to add /usr/local/lib to your library paths and make sure you're linking to the appropriate PCRE library.

Soulstorm
Mar 27, 2008, 05:05 PM
...and make sure you're linking to the appropriate PCRE library.

how do I do that? Actually, I also included /usr/local/lib to the library search paths but the problem persists.

And why do I need those things to compile the program anyway? Why does the terminal encounter no errors but Xcode does? IDEs are supposed to make things easier, not more difficult!

lazydog
Mar 28, 2008, 07:51 AM
how do I do that? Actually, I also included /usr/local/lib to the library search paths but the problem persists.

And why do I need those things to compile the program anyway? Why does the terminal encounter no errors but Xcode does? IDEs are supposed to make things easier, not more difficult!

Well, to produce your executable you need to link to the pcre library, either statically or dynamically. Static libraries are included in your final object code, dynamic libraries are linked in at runtime as and when they are needed.

I think the problem is again down to library include paths. From the cl, /usr/... is there by default. Have a look at project info. You could also try dragging the pcre library into the project tree… that might do the trick.

b e n