共享库:Windows vs Linux方法 [英] Shared Libraries: Windows vs Linux method

查看:92
本文介绍了共享库:Windows vs Linux方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Windows共享库(DLL)与Linux共享库(SO),我有一个简短的问题.

I have a quick question about Windows shared libraries (DLLs) vs Linux shared libraries (SOs).

为什么当您创建Windows DLL时,它要求客户端程序也要链接到静态库(.lib文件),而在Linux中创建的应用程序不需要与此静态库进行任何链接.

Why is it that when you create a Windows DLL it requires the client program to also link against a static library (.lib file), but applications created in Linux does not require any linking against such static library.

它与代码重定位等有关吗?谢谢.

Does it have anything to do with code relocation and the like? Thanks.

推荐答案

实际上没有代码重定位,这是一个完全不同的问题.这是关于架构上的差异:

Not actually with code relocation, that's a totally different issue. It is about a difference in architecture:

  • 在Windows中,DLL就像可执行文件(EXE). EXE和DLL之间的主要区别在于EXE具有入口点(main/WinMain函数),因此它可用于启动进程,而DLL仅可加载到预先存在的进程中.但请参阅(1)

  • In Windows, a DLL is just like a executables (EXE). The main difference between an EXE and a DLL is that the EXE has an entry point (main/WinMain function) and so it can be used to start a process, while DLLs can only be loaded into a pre-existing process. But see (1)

在Linux中,.so的工作类似于静态库(.a).主要区别在于.so文件可以与正在运行的程序链接,而.a文件仅在编译程序时可以链接.

In Linux, a .so work similar to a static library (.a). The main difference is that the .so file can be linked with a running program, while the .a file can only be linked when compiling the program.

这种方法的结果是,在Linux中,可以使用相同的文件来生成和运行程序.但是在Windows中,您需要适当的库(LIB)来链接程序.实际上,对应于DLL的lib通常只具有函数名称(用于满足链接器的作用)和用于进行重定位的存根(stub).但请参阅(2)

A consequence of this approach is that in linux the same file can be used to build and run the program. But in Windows you need a proper library (LIB) to link the program. Actually the lib that corresponds to a DLL usually have nothing more than the names of the functions, to satisfy the linker, and the stubs to do the relocation. But see (2)

(1)嗯,DLL也具有入口点,但是它不像某些初始化/完成挂钩那样被用作主要功能.

(1) Well, DLLs have entry point too, but it is not used as the main function, just as some sort of initialization/finalization hook.

(2)一些链接器非常聪明,可以在某些简单情况下使用DLL本身链接到DLL,而无需其他LIB文件.我认为至少MinGW链接器可以做到这一点.

(2) Some linkers are smart enough to be able, in some simple cases, to link to a DLL by using the DLL itself, without the need of an additional LIB file. I think that at least the MinGW linker can do that.

这篇关于共享库:Windows vs Linux方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆