之前因为工作需要,深入研究了 rabbitmq 的 C 代码,并以 libevent 为基础将其改造成事件驱动的单线程模型。由于一直都是自己写库封装和 demo 代码来测试,所以上述库都以 VS2010 的静态库(.lib)形式提供。前天,终于有业务人员要基于这个 demo 写东西了,但却发现编译链接都不行,究其原因是由于所用的编译器 VC6 太老旧的缘故。
库的跨编译器使用问题属于老生常谈,网上的资料也很多,这里出现的内容网上也都有,可以对比参阅。
起因:
我提供了 rabbitmq 和 libevent 的静态库,以及相关头文件;业务人员在 VC6 中完整业务逻辑编写。
现象:
编译后,报如下错误
Linking...
Creating library ..\..\..\..\10-common\version\debug\win32\mos\mos.lib and object ..\..\..\..\10-common\version\debug\win32\mos\mos.exp
libevent-1.4.13.lib(evutil.obj) : error LNK2001: unresolved external symbol __RTC_CheckEsp
libevent-1.4.13.lib(signal.obj) : error LNK2001: unresolved external symbol __RTC_CheckEsp
librabbitmq.lib(amqp_table.obj) : error LNK2001: unresolved external symbol __RTC_CheckEsp
libevent-1.4.13.lib(event.obj) : error LNK2001: unresolved external symbol __RTC_CheckEsp
libevent-1.4.13.lib(win32.obj) : error LNK2001: unresolved external symbol __RTC_CheckEsp
libevent-1.4.13.lib(log.obj) : error LNK2001: unresolved external symbol __RTC_CheckEsp
librabbitmq.lib(amqp_timer.obj) : error LNK2001: unresolved external symbol __RTC_CheckEsp
librabbitmq.lib(amqp_mem.obj) : error LNK2001: unresolved external symbol __RTC_CheckEsp
librabbitmq.lib(amqp_framing.obj) : error LNK2001: unresolved external symbol __RTC_CheckEsp
librabbitmq.lib(amqp_mq.obj) : error LNK2001: unresolved external symbol __RTC_CheckEsp
librabbitmq.lib(amqp_connection.obj) : error LNK2001: unresolved external symbol __RTC_CheckEsp
librabbitmq.lib(amqp_socket.obj) : error LNK2001: unresolved external symbol __RTC_CheckEsp
librabbitmq.lib(amqp_tcp_socket.obj) : error LNK2001: unresolved external symbol __RTC_CheckEsp
librabbitmq.lib(amqp_api.obj) : error LNK2001: unresolved external symbol __RTC_CheckEsp
libevent-1.4.13.lib(evutil.obj) : error LNK2001: unresolved external symbol __RTC_Shutdown
libevent-1.4.13.lib(signal.obj) : error LNK2001: unresolved external symbol __RTC_Shutdown
librabbitmq.lib(amqp_table.obj) : error LNK2001: unresolved external symbol __RTC_Shutdown
libevent-1.4.13.lib(event.obj) : error LNK2001: unresolved external symbol __RTC_Shutdown
libevent-1.4.13.lib(win32.obj) : error LNK2001: unresolved external symbol __RTC_Shutdown
libevent-1.4.13.lib(log.obj) : error LNK2001: unresolved external symbol __RTC_Shutdown
librabbitmq.lib(amqp_timer.obj) : error LNK2001: unresolved external symbol __RTC_Shutdown
librabbitmq.lib(amqp_mem.obj) : error LNK2001: unresolved external symbol __RTC_Shutdown
librabbitmq.lib(amqp_framing.obj) : error LNK2001: unresolved external symbol __RTC_Shutdown
librabbitmq.lib(amqp_mq.obj) : error LNK2001: unresolved external symbol __RTC_Shutdown
librabbitmq.lib(amqp_connection.obj) : error LNK2001: unresolved external symbol __RTC_Shutdown
librabbitmq.lib(amqp_socket.obj) : error LNK2001: unresolved external symbol __RTC_Shutdown
librabbitmq.lib(amqp_tcp_socket.obj) : error LNK2001: unresolved external symbol __RTC_Shutdown
librabbitmq.lib(amqp_api.obj) : error LNK2001: unresolved external symbol __RTC_Shutdown
libevent-1.4.13.lib(evutil.obj) : error LNK2001: unresolved external symbol __RTC_InitBase
libevent-1.4.13.lib(signal.obj) : error LNK2001: unresolved external symbol __RTC_InitBase
librabbitmq.lib(amqp_table.obj) : error LNK2001: unresolved external symbol __RTC_InitBase
libevent-1.4.13.lib(event.obj) : error LNK2001: unresolved external symbol __RTC_InitBase
libevent-1.4.13.lib(win32.obj) : error LNK2001: unresolved external symbol __RTC_InitBase
libevent-1.4.13.lib(log.obj) : error LNK2001: unresolved external symbol __RTC_InitBase
librabbitmq.lib(amqp_timer.obj) : error LNK2001: unresolved external symbol __RTC_InitBase
librabbitmq.lib(amqp_mem.obj) : error LNK2001: unresolved external symbol __RTC_InitBase
librabbitmq.lib(amqp_framing.obj) : error LNK2001: unresolved external symbol __RTC_InitBase
librabbitmq.lib(amqp_mq.obj) : error LNK2001: unresolved external symbol __RTC_InitBase
librabbitmq.lib(amqp_connection.obj) : error LNK2001: unresolved external symbol __RTC_InitBase
librabbitmq.lib(amqp_socket.obj) : error LNK2001: unresolved external symbol __RTC_InitBase
librabbitmq.lib(amqp_tcp_socket.obj) : error LNK2001: unresolved external symbol __RTC_InitBase
librabbitmq.lib(amqp_api.obj) : error LNK2001: unresolved external symbol __RTC_InitBase
libevent-1.4.13.lib(log.obj) : error LNK2001: unresolved external symbol __imp____iob_func
librabbitmq.lib(amqp_connection.obj) : error LNK2001: unresolved external symbol __imp____iob_func
librabbitmq.lib(amqp_socket.obj) : error LNK2001: unresolved external symbol __imp____iob_func
librabbitmq.lib(amqp_api.obj) : error LNK2001: unresolved external symbol __imp____iob_func
librabbitmq.lib(amqp_mq.obj) : error LNK2001: unresolved external symbol __imp____iob_func
libevent-1.4.13.lib(event.obj) : error LNK2001: unresolved external symbol @_RTC_CheckStackVars@8
libevent-1.4.13.lib(win32.obj) : error LNK2001: unresolved external symbol @_RTC_CheckStackVars@8
libevent-1.4.13.lib(log.obj) : error LNK2001: unresolved external symbol @_RTC_CheckStackVars@8
libevent-1.4.13.lib(evutil.obj) : error LNK2001: unresolved external symbol @_RTC_CheckStackVars@8
librabbitmq.lib(amqp_timer.obj) : error LNK2001: unresolved external symbol @_RTC_CheckStackVars@8
librabbitmq.lib(amqp_mem.obj) : error LNK2001: unresolved external symbol @_RTC_CheckStackVars@8
librabbitmq.lib(amqp_framing.obj) : error LNK2001: unresolved external symbol @_RTC_CheckStackVars@8
librabbitmq.lib(amqp_table.obj) : error LNK2001: unresolved external symbol @_RTC_CheckStackVars@8
librabbitmq.lib(amqp_connection.obj) : error LNK2001: unresolved external symbol @_RTC_CheckStackVars@8
librabbitmq.lib(amqp_socket.obj) : error LNK2001: unresolved external symbol @_RTC_CheckStackVars@8
librabbitmq.lib(amqp_tcp_socket.obj) : error LNK2001: unresolved external symbol @_RTC_CheckStackVars@8
librabbitmq.lib(amqp_api.obj) : error LNK2001: unresolved external symbol @_RTC_CheckStackVars@8
librabbitmq.lib(amqp_table.obj) : error LNK2001: unresolved external symbol ___security_cookie
libevent-1.4.13.lib(log.obj) : error LNK2001: unresolved external symbol ___security_cookie
libevent-1.4.13.lib(evutil.obj) : error LNK2001: unresolved external symbol ___security_cookie
librabbitmq.lib(amqp_connection.obj) : error LNK2001: unresolved external symbol ___security_cookie
librabbitmq.lib(amqp_socket.obj) : error LNK2001: unresolved external symbol ___security_cookie
librabbitmq.lib(amqp_api.obj) : error LNK2001: unresolved external symbol ___security_cookie
librabbitmq.lib(amqp_framing.obj) : error LNK2001: unresolved external symbol ___security_cookie
librabbitmq.lib(amqp_table.obj) : error LNK2001: unresolved external symbol @__security_check_cookie@4
libevent-1.4.13.lib(log.obj) : error LNK2001: unresolved external symbol @__security_check_cookie@4
libevent-1.4.13.lib(evutil.obj) : error LNK2001: unresolved external symbol @__security_check_cookie@4
librabbitmq.lib(amqp_connection.obj) : error LNK2001: unresolved external symbol @__security_check_cookie@4
librabbitmq.lib(amqp_socket.obj) : error LNK2001: unresolved external symbol @__security_check_cookie@4
librabbitmq.lib(amqp_api.obj) : error LNK2001: unresolved external symbol @__security_check_cookie@4
librabbitmq.lib(amqp_framing.obj) : error LNK2001: unresolved external symbol @__security_check_cookie@4
libevent-1.4.13.lib(signal.obj) : error LNK2001: unresolved external symbol __imp___wassert
librabbitmq.lib(amqp_socket.obj) : error LNK2001: unresolved external symbol __imp___wassert
librabbitmq.lib(amqp_mem.obj) : error LNK2001: unresolved external symbol __imp___wassert
libevent-1.4.13.lib(event.obj) : error LNK2001: unresolved external symbol __imp___wassert
libevent-1.4.13.lib(win32.obj) : error LNK2001: unresolved external symbol __imp___wassert
librabbitmq.lib(amqp_socket.obj) : error LNK2001: unresolved external symbol __imp__freeaddrinfo@4
librabbitmq.lib(amqp_socket.obj) : error LNK2001: unresolved external symbol __imp__getaddrinfo@16
librabbitmq.lib(amqp_socket.obj) : error LNK2001: unresolved external symbol __RTC_UninitUse
libevent-1.4.13.lib(evutil.obj) : error LNK2001: unresolved external symbol __imp___strtoi64
libevent-1.4.13.lib(evutil.obj) : error LNK2001: unresolved external symbol __imp___ftime64
libevent-1.4.13.lib(evutil.obj) : error LNK2001: unresolved external symbol __imp___vscprintf
..\..\..\..\10-common\version\debug\win32\mos\mos.exe : fatal error LNK1120: 14 unresolved externals
Error executing link.exe.
mos.exe - 85 error(s), 0 warning(s)
上述错误大致可以分为3类:
- error LNK2001: unresolved external symbol __RTC_xxx
- error LNK2001: unresolved external symbol @__security_xxx
- error LNK2001: unresolved external symbol __imp__xxx
属性 -> 配置属性 -> C/C++ -> 代码生成 -> 基本运行时检查(Basic Runtime Checks) 改为“默认(Default)”
对于第二类错误 - security 错误,是由于缓冲区安全检查导致的,需做如下设置:
属性 -> 配置属性 -> C/C++ > 代码生成 > 缓冲区安全检查,将值改为“否(/GS-)”
对于第三类错误 - 运行时库未实现相应函数,或实现的函数名字不同导致的错误,在使用静态库的情况下未找到合适的办法解决。
由于上面第三类错误未能够解决,所以只能改变库的提供方式,改成提供相应库的 dll 版本,而这种方式提供的库,上述三种错误都不会发生,即不需要按上面的方式设置各种值。
建议:
- 跨(windows)编译器的情况下最好不要使用静态库方式,应该使用动态库方式提供。
- 库的编译中使用的运行时最好保持一致。
来源:oschina
链接:https://my.oschina.net/u/617889/blog/168927