PCRE problem

Discussion in 'Mac Programming' started by Soulstorm, Mar 23, 2008.

  1. macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #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?
     
  2. macrumors 68020

    Gelfin

    Joined:
    Sep 18, 2001
    Location:
    San Francisco, CA
    #2
    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.
     
  3. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #3
    Actually, that didn't work. I told it to include "/usr/local/include" but it still can't find the file.
     
  4. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #4
    I guess if all fails you could revert to the standard stuff in <regex.h>.

    b e n
     
  5. macrumors member

    Joined:
    Feb 21, 2008
    #5
    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.
     
  6. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #6
    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!
     
  7. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #7
    I need regular expressions for C++, not PHP, unfortunately.
     
  8. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #8
    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.
     
  9. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #9
    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
     
  10. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #10
    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.
     
  11. macrumors 68020

    Gelfin

    Joined:
    Sep 18, 2001
    Location:
    San Francisco, CA
    #11
    You'll also need to add /usr/local/lib to your library paths and make sure you're linking to the appropriate PCRE library.
     
  12. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #12
    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!
     
  13. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #13
    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
     

Share This Page