Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of import libraries in Linux

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++.

like image 544
Dragno Avatar asked Dec 09 '18 10:12

Dragno


People also ask

How do I import libraries in Linux?

Install a library manually conf , and in the trusted directories ( /lib and /usr/lib ). The cache is used by the run-time linker, ld.so or ld-linux.so . ldconfig checks the header and filenames of the libraries it encounters when determining which versions should have their links updated.

What is DLL equivalent in Linux?

On Linux, the equivalent of a . dll is a "dynamic shared object", or a . so. You could statically link the required libraries in your executable, but that is really not best practice. See David Heffernan's answer.

Where should I put libraries in Linux?

By default, libraries are located in /usr/local/lib, /usr/local/lib64, /usr/lib and /usr/lib64; system startup libraries are in /lib and /lib64. Programmers can, however, install libraries in custom locations. The library path can be defined in /etc/ld. so.

What are import libraries?

An import library (. lib) file contains information the linker needs to resolve external references to exported DLL functions, so the system can locate the specified DLL and exported DLL functions at run time. You can create an import library for your DLL when you build your DLL.


2 Answers

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).

like image 70
Basile Starynkevitch Avatar answered Oct 20 '22 09:10

Basile Starynkevitch


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.

like image 5
yugr Avatar answered Oct 20 '22 10:10

yugr