完美編譯運行PCRE on Windows , Solved the __imp__pcre_exec __imp__pcre_compile __imp__pcre_free Errors

來源:互聯網
上載者:User

寫寫英文吧,好長時間不寫都有點不熟練了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>正在產生代碼...1>正在連結...1>Pcre.obj : error LNK2001: 無法解析的外部符號 __imp__pcre_free1>Pcre.obj : error LNK2001: 無法解析的外部符號 __imp__pcre_compile1>Pcre.obj : error LNK2001: 無法解析的外部符號 __imp__pcre_exec1>.\Debug/main.exe : fatal error LNK1120: 3 個無法解析的外部命令1>組建記錄檔儲存在“file://E:\Test\Pcre-Test\Debug\BuildLog.htm”1>main - 4 個錯誤,7 個警告========== 產生: 成功 0 個,失敗 1 個,最新 0 個,跳過 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), with
unwanted results. So in this environment, uncomment the following line. */

//#define PCRE_STATIC

try it ... successful !!!!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.