Write in English. If you don't write for a long time, you are a little unskillful. When I compile the following codes which test the PCRE, I got the unreferenced error _ imp _ pcre_exec _ imp _ pcre_compile _ imp _ pcre_free errors,My codes are follows:
//packing pcre usual functions.#ifndef _PCRE_H_#define _PCRE_H_#include <pcre.h>#include <string>#include <vector>#include <string.h>using namespace std;const int VECSIZE = 30;struct MatchResult{string name;vector<string> value;};class Pcre{public:Pcre();~Pcre();//Add a regrex, pass in name and regrexint AddRule(const string &name, const string &patten);//clear all the regrexvoid ClearRules();//match all the regrex, also return all the string match to every regrexvector<MatchResult> MatchAllRule(const char content[]);private:const char *error;int erroffset;int ovector[VECSIZE];vector<pcre*> re_arr;vector<string> patten_name;};#endif
PCRE. cpp
#include "Pcre.h"#include <stdio.h>Pcre::Pcre(){re_arr.clear();patten_name.clear();}Pcre::~Pcre(){for(int i=0; i<re_arr.size(); i++) { pcre_free(re_arr[i]); }}//Add a regrex patten and compile it.int Pcre::AddRule(const string &name, const string &patten){pcre *re = pcre_compile(patten.c_str(), PCRE_MULTILINE|PCRE_UTF8|PCRE_NO_AUTO_CAPTURE, &error, &erroffset, NULL); if(re == NULL) { printf("pcre compile failed, offset %d: %s\n", erroffset, error);return -1; }else{re_arr.push_back(re);patten_name.push_back(name);}}//clear all the rulevoid Pcre::ClearRules(){for(int i=0; i<re_arr.size(); i++){pcre_free(re_arr[i]); }re_arr.clear();patten_name.clear();}//match all regrex, if any match, return the matched patten name and it's valuesvector<MatchResult> Pcre::MatchAllRule(const char content[]){vector<MatchResult> result_arr;int length = strlen(content);char buf[512];for(int i=0; i<re_arr.size(); i++){MatchResult result;result.name = patten_name[i];int cur = 0;int rc; while(cur<length && (rc = pcre_exec(re_arr[i], NULL, content, length, cur, PCRE_NOTEMPTY, ovector, VECSIZE)) >= 0) { for(int j=0; j<rc; j++) {memset(buf, 0, sizeof(buf));strncpy(buf, content+ovector[2*j], ovector[2*j+1]-ovector[2*j]); result.value.push_back(buf); } cur = ovector[1];}if(result.value.size() > 0)result_arr.push_back(result);}return result_arr;}
Main. cpp
1> generating code... 1> link... 1> PCRE. OBJ: Error lnk2001: external symbols that cannot be parsed _ imp _ pcre_free1> PCRE. OBJ: Error lnk2001: external symbols that cannot be parsed _ imp _ pcre_compile1> PCRE. OBJ: Error lnk2001: external symbols that cannot be parsed _ imp _ pcre_exec1>. \ debug/main.exe: Fatal error lnk1120: 3 External commands that cannot be parsed. 1> the generated logs are saved in "file: // E: \ test \ PCRE-test \ debug \ buildlog.htm "1> main-4 errors, 7 warnings ============ generated: 0 errors are successfully generated, 1 failed, latest 0, skipped 0 ============
Google it, I found the thread about the error, a comment gives the solutions:
Building under Windows:
If you want to statically link this program against a non-DLL. A file, you must
Define pcre_static before including PCRE. H, otherwise the pcre_malloc () and
Pcre_free () exported functions will be declared _ declspec (dllimport),
Unwanted results. So in this environment, uncomment the following line .*/
// # Define pcre_static
Try it... successful !!!!