VS2015下解决:无法解析的外部符号 __imp___vsnprintf 及__iob_func




1、解决:无法解析的外部符号 __imp___vsnprintf



在 vs2015 工程选项,链接器附加依赖项里面添加 legacy_stdio_definitions.lib 即可。

出现这个问题的原因是 vs2015 默认编译时将许多标准库采用内联方式处理,因而没有可以链接的标准库文件,所以要专门添加标准库文件来链接标准库中的函数。



2、解决:无法解析的外部符号__iob_func


在使用 VS2015 下使用 libjpeg-turbo 静态库,编译时报错了:

  1. error LNK2019: 无法解析的外部符号 __iob_func,该符号在函数 output_message 中被引用  
error LNK2019: 无法解析的外部符号 __iob_func,该符号在函数 output_message 中被引用

根据关键字在网上找到一些文章描述了类似的错误,大都是找不到外部符号 __iob ,原因是VS2010上使用了 VC6 编译的 DLL 。虽然与我的情况不同,但是原理是一样的,我遇到的这个问题的原因是 VS2015 下使用VS2010编译的静态库,因为我用的libjpeg-turbo静态库是从官网下载编译好的版本(应该是vs2010这样的版本编译的)。

其实 __iob_func 和 __iob 都是用来定义 stdin,stdout,stderr,只是不同的VC版本实现方式不同。

下面是VS2015的头文件corecrt_wstdio.h中对stdin,stdout,stderr定义


  1. ACRTIMP_ALT FILE* __cdecl __acrt_iob_func(unsigned);  
  2.   
  3. #define stdin  (__acrt_iob_func(0))  
  4. #define stdout (__acrt_iob_func(1))  
  5. #define stderr (__acrt_iob_func(2))  
ACRTIMP_ALT FILE* __cdecl __acrt_iob_func(unsigned);

#define stdin  (__acrt_iob_func(0))
#define stdout (__acrt_iob_func(1))
#define stderr (__acrt_iob_func(2))

原来在 VS2015 中 __iob_func 改成了 __acrt_iob_func ,所以我参照《【LNK2019】 无法解析的外部符号 __iob》这篇文章的方法在自己的代码中增加了一个名为 __iob_func 转换函数:


  1. /* 
  2.  * 当libjpeg-turbo为vs2010编译时,vs2015下静态链接libjpeg-turbo会链接出错:找不到__iob_func, 
  3.  * 增加__iob_func到__acrt_iob_func的转换函数解决此问题, 
  4.  * 当libjpeg-turbo用vs2015编译时,不需要此补丁文件 
  5.  */  
  6. #if _MSC_VER>=1900  
  7. #include "stdio.h"   
  8. _ACRTIMP_ALT FILE* __cdecl __acrt_iob_func(unsigned);   
  9. #ifdef __cplusplus   
  10. extern "C"   
  11. #endif   
  12. FILE* __cdecl __iob_func(unsigned i) {   
  13.     return __acrt_iob_func(i);   
  14. }  
  15. #endif /* _MSC_VER>=1900 */  
/*
 * 当libjpeg-turbo为vs2010编译时,vs2015下静态链接libjpeg-turbo会链接出错:找不到__iob_func,
 * 增加__iob_func到__acrt_iob_func的转换函数解决此问题,
 * 当libjpeg-turbo用vs2015编译时,不需要此补丁文件
 */
#if _MSC_VER>=1900
#include "stdio.h" 
_ACRTIMP_ALT FILE* __cdecl __acrt_iob_func(unsigned); 
#ifdef __cplusplus 
extern "C" 
#endif 
FILE* __cdecl __iob_func(unsigned i) { 
    return __acrt_iob_func(i); 
}
#endif /* _MSC_VER>=1900 */

再次编译,错误消失。


        此次错误是我在编译librdkafka静态库时候,根据以上文章,处理错误是在“我的代码”--》rdkafka.c的末尾添加如下代码:

/*
* 当libjpeg-turbo为vs2010编译时,vs2015下静态链接libjpeg-turbo会链接出错:找不到__iob_func,
* 增加__iob_func到__acrt_iob_func的转换函数解决此问题,
* 当libjpeg-turbo用vs2015编译时,不需要此补丁文件
*/
#if _MSC_VER>=1900
#include "stdio.h" 
_ACRTIMP_ALT FILE* __cdecl __acrt_iob_func(unsigned);
#ifdef __cplusplus 
extern "C"
#endif 
FILE* __cdecl __iob_func(unsigned i) {
	return __acrt_iob_func(i);
}
#endif /* _MSC_VER>=1900 */
添加完成后,编译即可解决编译报错问题;

猜你喜欢

转载自blog.csdn.net/lixiang987654321/article/details/79386395