最近在把旧的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里即可