Windows 和 Unix 下动态链接库的区别
最近慢慢将开发环境转向了 freebsd ,渐渐的发现了许多东西跟 Windows 下不太一样。我指的是,跟我以前想当然的理解不太一样。比如,unix 下对动态链接库 so 的处理,和 Windows 下的 DLL 就不太相同。
之前,我对 Windows 下更为了解一些。早几年做 Windows 开发,老是为动态链接,静态链接这些问题纠缠不清;慢慢的才有了比较清晰的理解,现在基本上不会为这类问题困绕了。前段时间,碰到一些朋友写 lua 的扩展库(windows 版本)时遇到的几个 bug ,就是因为链接问题导致的。公司内部的一个项目,在服务器程序上也碰到了错误链接 lua 引起的 bug ,在内部 maillist 上争论了好久。当时,作为 windows 程序员,跟同事中的 unix 程序员争论看似相同的问题时,却老说不到一起去;今天才发现,原来是相互之间对动态链接库的理解不同导致的。
今天,写下本文,或许可以让以后跨平台开发的朋友少走点弯路。
动态链接库在 unix 下,习惯以 .so 为文件名结尾(通常还以 lib 开头)。而 Windows 下是以 .DLL 为文件后缀。Windows 在处理 dll 上还有一些细节容易被人忽略,我曾经为这个写过一篇 Blog。
如果需要运行时主动加载一个动态链接库,windows 下可以使用 LoadLibrary 这个 kernel API (在 kernel32.dll 中);unix 下是用 dlopen 。Windows 下找到 dll 中导出符号的地址,可以用 GetProcAddress ,而 unix 也有对应的 api ...
这些相互对应的 api ,似乎预示着对等的功能,但事实上是有区别的。
DLL 事实上和 EXE 文件一样,同属 PE 格式的执行文件。对于隐式的引用外部符号,需要把外部符号所在的位置写在 PE 头上。PE 加载器将从 PE 头上找到依赖的符号表,并加载依赖的其它 DLL 文件。
但是,unix 上并非如此,so 文件大多为 elf 执行文件格式。当它们需要的外部符号,可以不写明这些符号所在的位置。也就是说,通常 so 文件本身并不知道它依赖的那些符号在哪些 so 里面。这些符号是由调用 dlopen 的进程运行时提供的。而 unix 下的执行文件本身会暴露自己静态链接的符号,(可以是自己本身实现的,或者是从静态库 .a 文件里链入的)。dlopen 将把这些符号通报给 dlopen 加载的 .so 文件,最终完成动态链接。(事实上 dlopen 还可以指定 mode ,完成更复杂的操作)
因为这个区别,unix 下的 lua 解释器可以完全静态链接所有的 lua api ;我们为 lua 扩展的库,以 so 的形式存在被运行时加载不会有任何隐患。而 Windows 下,必须生成一个 luacore 的 DLL 文件,由 lua 解释器于扩展库共享 lua api (还包括 crt 的实现) 才不会出问题。
也因为这个区别,VC 下才会有让 windows 开发新手困惑不解的动态链接 CRT ,静态链接 CRT ,多线程库,单线程库,等等的选项。没点点 windows 开发功力的人,多少都要在上面栽几个跟头。
从动态链接库的这个设计上来看,我个人感觉,Windows 弄的真是糟糕透顶。尤其是对开发者来说是这样。至少我们在 windows 下做一个 dll 文件给大家使用还需要携带一个 .lib 文件;而 unix 下一般只需要有相应的头文件就够了。对于编写新的 .so ,找不到的符号可以就让它在那里,直到最终执行文件来把所有需要的符号联合到一起。windows 可以存在一个 dll 对另一个 dll 的隐式依赖;而 unix 下一般不需要让 so 和 so 有隐式依赖关系。这让我们全局替换类似 CRT 的东西变的困难许多;而以我自己的编程经验来看,好处却没有多少。
顺便一提的是 unix 下需要用 ldconfig 来管理动态库,这比 windows 下 copy DLL 到当前目录下就可以用的方式,无疑提高了系统的安全性。
Comments
Posted by: mayao11 | (118) March 31, 2012 08:21 AM
Posted by: zzz654321 | (117) July 6, 2009 05:25 PM
Posted by: Anonymous | (116) July 6, 2009 05:24 PM
Posted by: 山猫 | (115) July 10, 2008 02:34 AM
Posted by: Cloud | (114) June 2, 2008 10:11 PM
Posted by: est | (113) June 2, 2008 08:04 PM
Posted by: 1 | (112) June 1, 2007 07:06 PM
Posted by: Atry | (111) March 19, 2007 11:15 AM
Posted by: chen3feng | (110) January 19, 2007 12:41 AM
Posted by: Cloud | (109) December 9, 2006 05:58 PM
Posted by: luoyi | (108) December 9, 2006 11:40 AM
Posted by: xx | (107) December 3, 2006 02:16 PM
Posted by: 极光炫影 | (106) November 20, 2006 12:11 AM
Posted by: 發條陳 | (105) November 11, 2006 02:28 PM
Posted by: 星染流云 | (104) November 11, 2006 08:42 AM
Posted by: madlax | (103) November 9, 2006 07:58 PM
Posted by: Anonymous | (102) November 9, 2006 07:56 PM
Posted by: Zenberg | (101) November 9, 2006 01:24 PM
Posted by: sunway | (100) November 9, 2006 12:56 PM
Posted by: Bennie | (99) November 9, 2006 12:15 PM
Posted by: Zenberg | (98) November 9, 2006 10:38 AM
Posted by: mike | (97) November 9, 2006 10:29 AM
Posted by: Bennie | (96) November 9, 2006 10:26 AM
Posted by: Zenberg | (95) November 9, 2006 10:16 AM
Posted by: Bennie | (94) November 8, 2006 09:11 PM
Posted by: Cloud | (93) November 8, 2006 07:12 PM
Posted by: Cloud | (92) November 8, 2006 06:29 PM
Posted by: Cloud | (91) November 8, 2006 06:23 PM
Posted by: Cloud | (90) November 8, 2006 06:04 PM
Posted by: enterprisebeing | (89) November 8, 2006 05:37 PM
Posted by: Anonymous | (88) November 8, 2006 05:36 PM
Posted by: Anonymous | (87) November 8, 2006 04:48 PM
Posted by: Cloud | (86) November 8, 2006 02:37 PM
Posted by: Zenberg | (85) November 8, 2006 01:59 PM
Posted by: wuyu | (84) November 8, 2006 01:14 PM
Posted by: Cloud | (83) November 8, 2006 12:07 PM
Posted by: Anonymous | (82) November 8, 2006 10:17 AM
Posted by: Atry | (81) November 8, 2006 08:40 AM
Posted by: gamemake | (80) November 7, 2006 10:04 PM
Posted by: gamemake | (79) November 7, 2006 09:59 PM
Posted by: gamemake | (78) November 7, 2006 09:56 PM
Posted by: Zenberg | (77) November 7, 2006 09:44 PM
Posted by: Bennie | (76) November 7, 2006 09:37 PM
Posted by: Zenberg | (75) November 7, 2006 09:14 PM
Posted by: Zenberg | (74) November 7, 2006 09:10 PM
Posted by: Bennie | (73) November 7, 2006 09:04 PM
Posted by: Zenberg | (72) November 7, 2006 08:50 PM
Posted by: Bennie | (71) November 7, 2006 08:26 PM
Posted by: Bennie | (70) November 7, 2006 08:21 PM
Posted by: mike | (69) November 7, 2006 07:49 PM
Posted by: Cloud | (68) November 7, 2006 07:04 PM
Posted by: enterprisebeing | (67) November 7, 2006 06:23 PM
Posted by: enterprisebeing | (66) November 7, 2006 06:14 PM
Posted by: Cloud | (65) November 7, 2006 04:30 PM
Posted by: sunway | (64) November 7, 2006 03:45 PM
Posted by: Atry | (63) November 7, 2006 03:11 PM
Posted by: Atry | (62) November 7, 2006 02:57 PM
Posted by: Zenberg | (61) November 7, 2006 02:52 PM
Posted by: Atry | (60) November 7, 2006 02:51 PM
Posted by: Cloud | (59) November 7, 2006 02:01 PM
Posted by: enterprisebeing | (58) November 7, 2006 09:49 AM
Posted by: Bennie | (57) November 7, 2006 09:34 AM
Posted by: enterprisebeing | (56) November 7, 2006 01:36 AM
Posted by: enterprisebeing | (55) November 7, 2006 01:23 AM
Posted by: enterprisebeing | (54) November 7, 2006 01:21 AM
Posted by: Cloud | (53) November 6, 2006 11:01 PM
Posted by: sunway | (52) November 6, 2006 09:39 PM
Posted by: jhj | (51) November 6, 2006 08:49 PM
Posted by: jhj | (50) November 6, 2006 08:35 PM
Posted by: 星染流云 | (49) November 6, 2006 06:53 PM
Posted by: enterprisebeing | (48) November 6, 2006 01:57 PM
Posted by: Bennie | (47) November 6, 2006 01:17 PM
Posted by: enterprisebeing | (46) November 6, 2006 01:02 PM
Posted by: Bennie | (45) November 6, 2006 10:46 AM
Posted by: Anonymous | (44) November 6, 2006 08:45 AM
Posted by: Cloud | (43) November 5, 2006 11:07 PM
Posted by: jhj | (42) November 5, 2006 10:38 PM
Posted by: sunway | (41) November 5, 2006 10:34 PM
Posted by: pi1ot | (40) November 5, 2006 10:18 PM
Posted by: enterprisebeing | (39) November 5, 2006 10:12 PM
Posted by: enterprisebeing | (38) November 5, 2006 10:03 PM
Posted by: Cloud | (37) November 5, 2006 08:44 PM
Posted by: N/A | (36) November 5, 2006 03:51 PM
Posted by: Bennie | (35) November 5, 2006 02:05 PM
Posted by: Bennie | (34) November 5, 2006 02:02 PM
Posted by: stone | (33) November 5, 2006 01:48 PM
Posted by: enterprisebeing | (32) November 5, 2006 01:03 PM
Posted by: enterprisebeing | (31) November 5, 2006 12:32 PM
Posted by: newbie | (30) November 5, 2006 11:41 AM
Posted by: sunway | (29) November 4, 2006 11:03 PM
Posted by: Zenberg | (28) November 4, 2006 09:22 PM
Posted by: Anonymous | (27) November 4, 2006 09:19 PM
Posted by: Cloud | (26) November 4, 2006 08:58 PM
Posted by: 厄尔特特 | (25) November 4, 2006 08:48 PM
Posted by: sunway | (24) November 4, 2006 08:31 PM
Posted by: Cloud | (23) November 4, 2006 08:25 PM
Posted by: Zenberg | (22) November 4, 2006 08:22 PM
Posted by: sunway | (21) November 4, 2006 08:14 PM
Posted by: Cloud | (20) November 4, 2006 08:07 PM
Posted by: sunway | (19) November 4, 2006 07:51 PM
Posted by: Cloud | (18) November 4, 2006 07:15 PM
Posted by: 笔端 | (17) November 4, 2006 11:01 AM
Posted by: Bennie | (16) November 4, 2006 10:28 AM
Posted by: Anonymous | (15) November 4, 2006 08:30 AM
Posted by: 笔端 | (14) November 4, 2006 12:10 AM
Posted by: Anonymous | (13) November 3, 2006 11:45 AM
Posted by: enterprisebeing | (12) November 3, 2006 11:19 AM
Posted by: Anonymous | (11) November 3, 2006 11:11 AM
Posted by: Cloud | (10) November 3, 2006 06:07 AM
Posted by: enterprisebeing | (9) November 2, 2006 09:02 PM
Posted by: Zenberg | (8) November 2, 2006 08:57 PM
Posted by: Cloud | (7) November 2, 2006 06:14 PM
Posted by: Anonymous | (6) November 2, 2006 06:06 PM
Posted by: sunway | (5) November 2, 2006 12:57 PM
Posted by: Cloud | (4) November 2, 2006 12:18 PM
Posted by: Anonymous | (3) November 2, 2006 10:07 AM
Posted by: qyb | (2) November 1, 2006 10:26 PM
Posted by: sunway | (1) November 1, 2006 09:24 PM