pcre __imp__pcr __imp__pcre_compile

本文解决了在使用PCRE库时遇到的静态链接错误问题,通过在头文件开始处定义宏PCRE_STATIC来避免错误的发生,并解释了为何该宏需要在包含其他头文件之前定义。

项目编译报出如下错误:

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

转载于:https://blog.51cto.com/13187574/2083977

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值