在使用VS2015下使用libjpeg-turbo静态库,编译时报错了:
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
定义
<code class="language-cpp hljs has-numbering">_ACRTIMP_ALT FILE* __cdecl __acrt_iob_func(<span class="hljs-keyword">unsigned</span>); <span class="hljs-preprocessor">#define stdin (__acrt_iob_func(0))</span> <span class="hljs-preprocessor">#define stdout (__acrt_iob_func(1))</span> <span class="hljs-preprocessor">#define stderr (__acrt_iob_func(2))</span></code>
原来在VS2015中__iob_func
改成了__acrt_iob_func
,所以我参照《【LNK2019】 无法解析的外部符号 __iob》这篇文章的方法在自己的代码中增加了一个名为__iob_func
转换函数:
<code class="language-cpp hljs has-numbering"><span class="hljs-comment">/* * 当libjpeg-turbo为vs2010编译时,vs2015下静态链接libjpeg-turbo会链接出错:找不到__iob_func, * 增加__iob_func到__acrt_iob_func的转换函数解决此问题, * 当libjpeg-turbo用vs2015编译时,不需要此补丁文件 */</span> <span class="hljs-preprocessor">#if _MSC_VER>=1900</span> <span class="hljs-preprocessor">#include "stdio.h" </span> _ACRTIMP_ALT FILE* __cdecl __acrt_iob_func(<span class="hljs-keyword">unsigned</span>); <span class="hljs-preprocessor">#ifdef __cplusplus </span> <span class="hljs-keyword">extern</span> <span class="hljs-string">"C"</span> <span class="hljs-preprocessor">#endif </span> FILE* __cdecl __iob_func(<span class="hljs-keyword">unsigned</span> i) { <span class="hljs-keyword">return</span> __acrt_iob_func(i); } <span class="hljs-preprocessor">#endif /* _MSC_VER>=1900 */</span></code>
再次编译,错误消失。
其实吧,因为我用的libjpeg-turbo静态库是从官网下载编译好的版本(应该是vs2010这样的版本编译的),所以才有这个问题,如果自己重新把libjpeg-turbo的源码在vs2015下重新编译,就不会存在这个问题了。