【原创】VS2010下编译的库在VC6下使用问题

    之前因为工作需要,深入研究了 rabbitmq 的 C 代码,并以 libevent 为基础将其改造成事件驱动的单线程模型。由于一直都是自己写库封装和 demo 代码来测试,所以上述库都以 VS2010 的静态库(.lib)形式提供。前天,终于有业务人员要基于这个 demo 写东西了,但却发现编译链接都不行,究其原因是由于所用的编译器 VC6 太老旧的缘故。 

库的跨编译器使用问题属于老生常谈,网上的资料也很多,这里出现的内容网上也都有,可以对比参阅。 

起因: 
我提供了 rabbitmq 和 libevent 的静态库,以及相关头文件;业务人员在 VC6 中完整业务逻辑编写。

现象: 
编译后,报如下错误 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
      对于第一类错误 -  RTC 错误 是 VS2005 及以上版本编译器的 Runtime Check 运行时检查 导致的。 在编译 lib 时,如果选择了“运行时检查”,那么别的应用程序在链接这个库文件的时候,就会报这样的错。 为了能让 lib 库文件在 VS2010 和 VC6 上通用,需要用以下的方法,再重编一下库: 
属性  -> 配置属性 -> C/C++ -> 代码生成 -> 基本运行时检查(Basic Runtime Checks) 改为“默认(Default)” 

      对于第二类错误 -  security 错误,是由于缓冲区安全检查导致的,需做如下设置: 
属性 -> 配置属性 -> C/C++ > 代码生成 > 缓冲区安全检查,将值改为“否(/GS-)” 

      对于第三类错误 - 运行时库未实现相应函数,或实现的函数名字不同导致的错误,在使用静态库的情况下未找到合适的办法解决。 

      由于上面第三类错误未能够解决,所以只能改变库的提供方式,改成提供相应库的 dll 版本,而这种方式提供的库,上述三种错误都不会发生,即不需要按上面的方式设置各种值。 
上一篇:小程序开发如何为企业带来效益?


下一篇:群集之一 Windows Server 2008故障转移群集上部署Exchange Server 2007 SP1 CCR群集