cmakelinkershared-librariesdlopen

Please explain shared libraries vs. module libraries


I've been learning CMake recently, from which I've become introduced to the concept of a distinction between shared libraries and module libraries.

Some recent, relevant learning material (not the totality of it):
https://cmake.org/cmake/help/v3.0/prop_tgt/TYPE.html
https://cmake.org/cmake/help/latest/command/add_library.html
https://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html

I only became aware of this distinction after my introduction to CMake.

Prior to this, I was only aware of the distinction between shared libraries and static libraries. I thought that all .so files were shared libraries, but it now appears that a .so file may be either a shared library or a module library(?) And I was under the impression that the user had the choice of linking with a .so file or loading it (with dlopen() or similar functions). But now, per my in-progress learning, I'm forming the impression that the user needs to know whether his .so is a shared library or a module library, and in the case of the former, must only link with it, and in the case of the latter must only load it.

A point of confusion, for me, is that it seems one can either link to a .so file or load it, in the sense that a gcc/g++ invocation that links to a .so file will return 0 (assuming symbols are properly resolved, etc.), while running dlopen() on that same .so file will also be successful.

My question is: can anyone please explain the difference and origin of this distinction between shared libraries and module libraries?


For simplicity, I've kept the question around *nix systems (i.e. .so files and not .dll files), but if the answer relates to the difference between Windows, *nix (and other systems), please let me know if the question can be phrased better accordingly.


This question is related to this one, but I think this question is distinct because I'm trying to get at the rationale/history behind the distinction between shared libraries and static libraries, as opposed to just the fact that CMake facilitates the distinction (or makes the distinction more apparent to those, like myself, who had the impression that all .so files were shared libraries, and that it was a user's arbitrary choice whether to link to it or load it).


Solution

  • On Linux I don't think there is much of a difference, as you said you can still link to a MODULE library and you can also dlopen a SHARED library if you like to.

    However, on Windows there is a difference. Shared libraries on Windows are usually composed of two files: The .dll file loaded when running a program that uses the shared library, and a .lib file used by the linker when creating the executable. Building a program that links to a shared library does not require the .dll at all, and similarly you can run such a program without the .lib file being present.

    Because MODULES are meant to be loaded manually and not linked, a MODULE library will not create the .lib file, so on Windows you can't actually link to a MODULE library.