10

In Windows C++ when you want to link against a DLL you must supply an import library. But in GNU build system when you want to link against .so files which are the equivalent of dll you don't. Why is this? Is there an equivalent of Windows import libraries.

Note: I don't speak about the case where you use GNU C++ in Windows where you have to use import libraries as well. The splitting line is between Windows C++ and Linux C++.

Dragno
  • 3,027
  • 1
  • 27
  • 41
  • 3
    Standard C++ is designed for abstract hardware. It is oblivious of OS and implementation details. – Ron Dec 09 '18 at 10:39
  • 2
    Don't look into Linux as something equivalent to Windows. Look into Linux with fresh eyes! – Basile Starynkevitch Dec 09 '18 at 10:45
  • 5
    You simply link against the library. For example to link against `libssl.so`, you would include `-lssl` in your command string (the `lib` portion is not included in your option (e.g. `-lssl`). If the libraries are not within your standard search path you add `-L/path/to/lib` to add that directory to the search path. (and that library must know where it resides if additional pieces are needed. When building the library the linker `-rpath` option can help there. – David C. Rankin Dec 09 '18 at 11:43
  • 6
    (Note: this question has nothing to do with C++ language) Well, import libraries only contain code that can be automatically generated linkage-time. Actually, that's what gcc-for-windows (mingw) does: it can link against *.dll-files without using *.lib files. A note: never use *.lib files that contain _ordinals:_ you might end up with executables that don't run on a different version of Windows. – Lorinczy Zsigmond Dec 09 '18 at 11:54

2 Answers2

17

The linking model is different in Windows and in Linux. Read Levine's book Linkers and loaders (on Linux, every public symbol of a library is exported, unless you play visibility tricks; on Windows, that is not the case, and exported symbols need to be explicited).

The C++11 standard (read n3337) don't mention dynamic linking. It is an implementation detail.

The future C++20 could have modules.

There is no "import libraries" in Linux.

For more details, be aware that name mangling is different. Read also Program Library Howto, Drepper's How to Write Shared Libraries

On Linux, plugins are loaded (and handled differently than on Windows) by the dynamic loader. See ld-linux(8), dlopen(3), dlsym(3), elf(5)

Inspect, on Linux, ELF files (object files, libraries, executables) with objdump(1) and readelf(1) and nm(1).

See also C++ dlopen mini howto. Read also about the Visibility function attribute. See also this question.

.so files which are the equivalent of dll

A Linux shared object (ELF .so file) is not exactly equivalent to a Windows DLL. Read the references given above.

I also recommend reading Operating Systems: Three Easy Pieces and the old Advanced Linux Programming (both are freely downloadable). Later read syscalls(2) and the pages referenced from there.

Be also aware that Linux is free software, so you can download and study the source code of most of its components.

PS. Linux and Windows are really different. Don't expect to find in Linux the exact equivalent of every Windows feature. Look into Linux with fresh eyes. Take advantage that Linux is made of free software, and consider studying the source code e.g. of the kernel, binutils, GNU libc or musl-libc (both providing some ld-linux.so and libc.so, so a C standard library), GCC or Clang (both providing a C++ standard library above the libc.so).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 1
    I'm in a situation more or less the opposite of the OP's, I understand Linux's system but I'm trying to understand Windows dlls in more detail. By any chance do you have more information on what you mean by exported symbols need to be explicit? That's `__declspec(dllexport)` isn't it? Is it true to say that Windows dlls do not have any exported symbols at all and would be useless to link against on their own, and that's why you need import .lib files? That's what it seems like. Also, it seems like the Program Library HowTo link is dead. EDIT: Found this https://stackoverflow.com/a/27899384 – jrh Mar 23 '21 at 18:03
  • 1
    I never used Windows in my life. – Basile Starynkevitch Mar 23 '21 at 19:19
  • 1
    This answer may contain correct facts but they are completely irrelevant to the question, because it is possible to link against a Windows DLL having only the DLL. Windows version of gcc does it. Microsoft's linker won't process the DLL directly, but can link using only files that have been totally automatically generated from the DLL. The use of import libraries on Windows are not a consequence of differences in linking model, but of shortcuts (I'll be generous and say optimizations because they probably do reduce link time) taken in the Microsoft linker. – Ben Voigt Mar 23 '21 at 22:08
  • @BenVoigt how do you say the answer is irrelevant if the one who asked accepted it as the best answer for his question? I will take the answer in consideration and study the points that I don’t know. – Maf Oct 02 '22 at 19:38
  • @Mal: One of the problems with the acceptance checkmark is that it's given out by someone who doesn't know what is correct (that's why they asked the question after all). It often turns out well, but sometimes there are "accepted answers" which are outright false. Also, some users feel pressured to give the acceptance checkmark to some answer, even if all the options are flawed (especially earlier in the site's history, when the acceptance percentage was calculated and shown publicly, and did generate a lot of comments) – Ben Voigt Oct 03 '22 at 15:28
5

To add to Basile's answer, you may occasionally need import libraries on Linux to simulate delay loading of shared libraries (which is useful e.g. if your app rarely needs this library and you don't want to waste resources on it).

Such simulated import libraries would consist of a bunch of wrappers that call dlopen and dlsym internally and then goto to implementation in shared library. They can be implemented manually, via project-specific script or via generic tool Implib.so.

yugr
  • 19,769
  • 3
  • 51
  • 96