我参与了一些关于Linux中库的争论,我想确认一些事情。
这是我的理解(请纠正我,如果我错了,我会在后面编辑我的帖子),有两种方式使用库构建应用程序:
静态库(。a files):在链接时,将整个库的副本放入最终的应用程序中,以便库中的函数始终对调用应用程序可用 共享对象(。在链接时,对象只是通过相应的头文件(.h)来验证它的API。这个库直到运行时才真正被使用,这时才需要它。
静态库的明显优势是它们允许整个应用程序是自包含的,而动态库的好处是“。因此,“文件可以被替换(即:如果由于安全漏洞需要更新它),而不需要重新编译基础应用程序。
我听说有些人在共享对象和动态链接库(DLL)之间做了区分,尽管它们都是“。所以“文件。在Linux或任何其他POSIX兼容的操作系统(如MINIX、UNIX、QNX等)上进行C/ c++开发时,共享对象和dll之间有什么区别吗?有人告诉我,一个关键的区别(到目前为止)是共享对象只在运行时使用,而DLL必须首先使用应用程序内的dlopen()调用打开。
最后,我还听到一些开发人员提到了“共享档案”,据我所知,它本身也是静态库,但从未被应用程序直接使用。相反,其他静态库将链接到“共享存档”,从共享存档中提取一些(但不是全部)函数/资源到正在构建的静态库中。
提前感谢大家的协助。
更新
在向我提供这些术语的上下文中,这实际上是一个必须学习Linux的Windows开发人员团队使用的错误术语。我试图纠正他们,但(不正确的)语言规范仍然存在。
Shared Object: A library that is automatically linked into a program when the program starts, and exists as a standalone file. The library is included in the linking list at compile time (ie: LDOPTS+=-lmylib for a library file named mylib.so). The library must be present at compile time, and when the application starts. Static Library: A library that is merged into the actual program itself at build time for a single (larger) application containing the application code and the library code that is automatically linked into a program when the program is built, and the final binary containing both the main program and the library itself exists as a single standalone binary file. The library is included in the linking list at compile time (ie: LDOPTS+=-lmylib for a library file named mylib.a). The library must be present at compile time. DLL: Essentially the same as a shared object, but rather than being included in the linking list at compile time, the library is loaded via dlopen()/dlsym() commands so that the library does not need to be present at build time for the program to compile. Also, the library does not need to be present (necessarily) at application startup or compile time, as it is only needed at the moment the dlopen/dlsym calls are made. Shared Archive: Essentially the same as a static library, but is compiled with the "export-shared" and "-fPIC" flags. The library is included in the linking list at compile time (ie: LDOPTS+=-lmylibS for a library file named mylibS.a). The distinction between the two is that this additional flag is required if a shared object or DLL wants to statically link the shared archive into its own code AND be able to make the functions in the shared object available to other programs, rather than just using them internal to the DLL. This is useful in the case when someone provides you with a static library, and you wish to repackage it as an SO. The library must be present at compile time.
额外的更新
“DLL”和“共享库”之间的区别只是我当时工作的公司(Windows开发人员被迫转向Linux开发,这个术语被保留下来)的一种(懒惰的,不准确的)口语化说法,遵循上面提到的描述。
此外,在“共享档案”的情况下,库名后面的“S”文字只是该公司使用的一种惯例,而不是在整个行业中使用。