VS2015下解决:error LNK2019: 无法解析的外部符号 __iob_func解决方案

26 篇文章 1 订阅
12 篇文章 0 订阅

在使用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下重新编译,就不会存在这个问题了。



错误信息 "error LNK2019: 无法解析外部符号 __imp_clock" 表明在程序中使用了函数 __imp_clock,但编译器和链接器无法找到该函数的定义或实现。这种错误通常是由于缺失符号的定义、编译器搜索路径错误或编译器/链接器选项错误引起的。 解决这个错误的方法有以下几种: 1. 确保包含了正确的头文件:检查代码中是否包含了正确的头文件,特别是包含了声明 __imp_clock 的头文件。如果没有包含正确的头文件,编译器将无法找到函数的定义。 2. 检查编译器的搜索路径:编译器需要知道在哪里查找函数的定义。确保编译器的搜索路径设置正确,以便能够找到函数的定义。可以通过设置编译器的选项或环境变量来修改搜索路径。 3. 检查编译器/链接器选项:检查编译器和链接器的选项设置,确保没有设置错误的选项。有时候,错误的选项设置可能导致编译器无法找到函数的定义。 下面是一个示例代码,演示了如何使用函数 __imp_clock: ```c++ #include <iostream> #include <ctime> int main() { std::clock_t start = std::clock(); // 执行一些操作 std::clock_t end = std::clock(); double duration = (end - start) / (double) CLOCKS_PER_SEC; std::cout << "程序执行时间:" << duration << " 秒" << std::endl; return 0; } ``` 这段代码使用了函数 std::clock() 来计算程序的执行时间。确保在代码中包含了正确的头文件,并且编译器能够找到函数的定义。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值