error LNK2019: unresolved external symbol __imp___pctype referenced in function类问题的解决方案

最近在把旧的Visual Studio 2005项目升级到2015时遇到了这种错误:

error LNK2001: unresolved external symbol __imp___iob

error LNK2019: unresolved external symbol __imp___pctype referenced in function

error LNK2019: unresolved external symbol __imp____mb_cur_max referenced in function

这是由于微软提供的标准库在升级时删除或者更新了一些symbol的函数签名或实现方式,这种升级时遇到的不兼容让很多coder在把自己或者公司的项目升级到新版VS时也是非常头疼,微软后来对这种问题也提供了解决方案,估计是收到的抗议不少,就是在Linker的Additional Dependencies添加legacy_stdio_definitions.lib,该静态库提供了一些兼容旧版标准库的定义之类的东西,可以修复一部分找不到symbol的问题,但这个库不全,仍然有一部分symbol在编译时是找不到的,我们可以手动实现这些symbol来解决linker报错的问题,具体代码如下:

stdafx.h:

#pragma once
#define _ACRTEXPT __declspec(dllexport)
/* Macro for perror, strerror, and _strerror, this is optional, you can remove this line */
#define _sys_err_msg(m) _sys_errlist[(((m)<0)||((m)>=_sys_nerr)?_sys_nerr:(m))]
legacy_stdio_defs.cpp:

#include "stdafx.h"
#include <ctype.h>
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

#ifdef _pctype
#undef _pctype
#endif //_pctype
_ACRTEXPT unsigned short const* __cdecl _pctype()
{
	return __pctype_func();
}
_ACRTEXPT unsigned short const** __cdecl __p__pctype()
{
	static unsigned short const* sret = __pctype_func();
	return &sret;
}

#ifdef __mb_cur_max
#undef __mb_cur_max
#endif //__mb_cur_max
_ACRTEXPT int __cdecl __mb_cur_max()
{
	return ___mb_cur_max_func();
}
_ACRTEXPT int* __cdecl __p___mb_cur_max()
{
	static int iret = ___mb_cur_max_func();
	return &iret;
}

_ACRTEXPT FILE _iob[] = { *stdin, *stdout, *stderr };
_ACRTEXPT FILE *__p__iob = _iob;
#ifdef __cplusplus
}
#endif
将该文件编译为一个lib文件加入其他报错的project的additional dependencies里即可

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值