Compiling error C++ - undefined references using PCRE library

若如初见. 提交于 2019-12-12 17:24:23

问题


I'm having problems compiling a code to test if the PCRE library is installed correctly.

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



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

{ 
    const char *error; 
    int   erroffset; 
    pcre *re; 
    int   rc; 
    int   i; 
    int   ovector[100]; 

    char *regex = "From:([^@]+)@([^\r]+)"; 
    char str[]  = "From:regular.expressions@example.com\r\n"\ 
                  "From:exddd@43434.com\r\n"\ 
                  "From:7853456@exgem.com\r\n"; 

    re = pcre_compile (regex,          /* the pattern */ 
                       PCRE_MULTILINE, 
                       &error,         /* for error message */ 
                       &erroffset,     /* for error offset */ 
                       0);             /* use default character tables */ 
    if (!re) 
    { 
        printf("pcre_compile failed (offset: %d), %s\n", erroffset, error); 
        return -1; 
    } 

    unsigned int offset = 0; 
    unsigned int len    = strlen(str); 
    while (offset < len && (rc = pcre_exec(re, 0, str, len, offset, 0, ovector, sizeof(ovector))) >= 0) 
    { 
        for(int i = 0; i < rc; ++i) 
        { 
            printf("%2d: %.*s\n", i, ovector[2*i+1] - ovector[2*i], str + ovector[2*i]); 
        } 
        offset = ovector[1]; 
    } 
    return 1; 
} 

The error returned is:

[Linker error] undefined reference to `_imp__pcre_compile' 

[Linker error] undefined reference to `_imp__pcre_exec' 

I'm pretty sure I'm doing something stupid.

Currently using the IDE Dev-C++ , installed the package using the Dev Package manager.

This is the DevPak package installer: http://www.mediafire.com/?zb3wc6q07sddsac i used to install the library (pcre-6.4.1)

I want some guidance that would lead me to installing this library corretly (So I can work with regular expressions).

If not, I would love a reference to a c++ library to support regular expressions easy to install on this Dev-C++ or CodeBlocks.

Thanks for your help !

EDIT: Thanks for your help, solved this by checking this thread How do I get PCRE to work with C++?

and dumb coders responses in both threads.


回答1:


Are you having name mangling issues? Does pcre.h have anything like this in it?

extern "C" {
   // declarations of c functions for c++
 }

If not wrap your #include with that and it will inform C++ it's calling C functions



来源:https://stackoverflow.com/questions/5568644/compiling-error-c-undefined-references-using-pcre-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!