pcre __imp__pcr __imp__pcre_compile

項目編譯報出如下錯誤:

Pcre.obj:error LNK2001:無法解析的外部符號 __imp__pcre_free
Pcre.obj:error LNK2001:無法解析的外部符號 __imp__pcre_compile
Pcre.obj:error LNK2001:無法解析的外部符號 __imp__pcre_exec

解決辦法是在頭文件的最開頭加上宏定義#define PCRE_STATIC

疑問:爲什麼必須在其他#include之前呢?

請看pcre.h中48-66行

/* When an application links to a PCRE DLL in Windows, the symbols that are
imported have to be identified as such. When building PCRE, the appropriate
export setting is defined in pcre_internal.h, which includes this file. So we
don't change existing definitions of PCRE_EXP_DECL and PCRECPP_EXP_DECL. */

#if defined(_WIN32) && !defined(PCRE_STATIC)
#  ifndef PCRE_EXP_DECL
#    define PCRE_EXP_DECL  extern __declspec(dllimport)
#  endif
#  ifdef __cplusplus
#    ifndef PCRECPP_EXP_DECL
#      define PCRECPP_EXP_DECL  extern __declspec(dllimport)
#    endif
#    ifndef PCRECPP_EXP_DEFN
#      define PCRECPP_EXP_DEFN  __declspec(dllimport)
#    endif
#  endif
#endif

參考:

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

參考鏈接:http://www.xuebuyuan.com/659173.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章