Linux repository type

There are two types of libraries that can be created under Linux:

  1. Static library (.a): directly linked into the executable by the application during linking

  2. Dynamic linked libraries (so): dynamic libraries are also used in two ways: a) linked to dynamic libraries at runtime, but declared at compile time, that is, such dynamic libraries must be visible to the compiler at compile time, but the compiler does not compile such libraries into executable files; B) Libraries that are dynamically loaded and unloaded during runtime are loaded using the dynamic loading method. This library format is not fundamentally different from dynamic linking, except that the user program decides when to link, rather than the system linker automatically links

Naming conventions

Libraries need to start with lib, but do not need to include a start or extension when specifying link command line arguments, for example:

gcc src-file.c -lm -lpthreadCopy the code

In this example, libmath.a and libpThread. a are linked

Static library. (a)

Static libraries are generated as follows:

  • Compile the object file. For example, cc -wall -c ctest1.c ctest2.c, the command generates ctest1.o and ctest2.o(-wall indicates warning output during compilation).

  • Create the library file. For example, ar-cvq libctest.a ctest1.o ctest2.o. This command yields a libctest.a file

  • You can use ar-T to view the. O files in the. A file. So, ar is essentially a package command, similar to tar

  • Build the symbol table. Ranlib libctest.a is used to create a symbol table for.a. Some AR commands actually integrate ranlib functionality

The.a file is the same concept as the.lib file on Windows.

The dynamic library (. So)

The method to generate the dynamic library is as follows:

gcc -Wall -fPIC -c *.cCopy the code

The purpose of this option is for the compiler to generate position-independent code. This is because dynamic libraries are linked at runtime and the offsets of variables and functions are not known in advance, requiring address redirection based on offset after linking.

Gcc-shared-wl,-soname,libctest.so.1 -o libctest.so.1.0 *.oCopy the code

The -shared option allows dynamic libraries to be dynamically linked at runtime; -wl,options sets the parameters to be passed to ld(linker). In the above example, lD-soname ibctest.so.1 will be executed when the linker is linking

The command above will eventually output a dynamic library libctest.so.1.0, and out of habit, create two soft chains:

Mv libctest.so.1.0 /opt/lib ln -sf /opt/lib/libctest.so.1.0 /opt/lib/libctest.so.1 ln -sf /opt/lib/libctest.so.1.0 /opt/lib/libctest.so.1.0 /opt/lib/libctest.soCopy the code

Libctest. so is used to use -lctest at compile time to let the compiler find the dynamic library, while libctest.so.1 is used to link at run time

gcc -Wall -I/path/to/include-files -L/path/to/libraries prog.c -lctest -o progCopy the code

Check the rely on

Use the LDD command to view your program’s dependency on dynamic libraries. Such as:

ldd prog

libctest.so.1 => /opt/lib/libctest.so.1 (0x00002aaaaaaac000)
libc.so.6 => /lib64/tls/libc.so.6 (0x0000003aa4e00000)
/lib64/ld-linux-x86-64.so.2 (0x0000003aa4c00000)Copy the code

Obj file

The format and composition of obJ files can be a major indicator of system variability, such as PE on Windows, ELF on Linux and some Unix, Mach-O on MacOS, xCOFF on AIX.

To view the symbol table information of obJ files, you can use methods such as nm Objdump readelf.

Find dynamic libraries at run time

At run time, the system needs to know where to look for dynamic libraries, which is configured through /etc/ld.so.conf. Ldconfig is used to configure the runtime dynamic library lookup path, which actually updates /etc/ld.so.cache. Other environment variables can also affect the lookup: (Linux/Solaris: LD_LIBRARY_PATH, SGI: LD_LIBRARYN32_PATH, AIX: LIBPATH, Mac OS X: DYLD_LIBRARY_PATH, HP-UX: SHLIB_PATH)

Dynamically loading and unloading libraries

The need for an application to be designed as a plug-in architecture requires a mechanism for dynamically loading and unloading libraries. Unlike dynamic linking, dynamic loading means that a dynamic library can be unaware of its existence at compile time, but is tried to be loaded in by a user program at run time.

DLFCN. H functions such as dlopen, DLsym and dlclose are used to achieve this function.

In addition, executables that use the DLFCN mechanism require the -rdynamic option, which instructs the connector to add all symbols (not just external symbols already used by the program, but not static symbols, such as static-decorated functions) to the dynamic symbol table (that is, the.dynsym table).

GNU Libtool

Libtool is just a script. Compiling and linking with libtool produces a file like.la, which is a text file pointing to a.a file and declaring some version information.