Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
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

macrumors 68020
Sep 18, 2001
2,165
5
Denver, CO
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

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
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.
 

alaceo

macrumors member
Feb 21, 2008
32
0
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

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
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

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
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

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
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

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
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

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
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

Code:
#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

macrumors 68020
Sep 18, 2001
2,165
5
Denver, CO
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

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
...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

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.