Home Gcc
Post
Cancel

Gcc

GCC

Shared Libraries

Naming convention

Real Name

all libraries do not follow the convention

  • lib.so.x.y.z (libjpeg.so.1.2.3)
  • lb-x.yy.so (libc-2.31.so)

name -> name of the Library x -> major version number y -> minor version number z -> patch version number

Shared Object Name (soname)

  • Label for a major version of a shared object
    • soname: libjpeg.so.1 => libjpeg.so.1.2.3
  • helps to resolve shared library dependencies

Linker Name

  • gcc will look for this during
  • libname.so

To create a shared library

source code -> object file -> shared library

  • gcc -fpic -c source.c
    • creates an object file source.o with position-independent code (PIC)
  • gcc -fpic source1.o source2.o -shared -Wl,-soname,libjpeg.so.1 -o libjpeg.so.1.2.3
    • creates a shared library from the object file
  • utilize the created shared library during compiling and linking
    • gcc main.c -L. -ljpeg -o main
      • -L. tells the linker to look in the current directory for libraries
      • -ljpeg tells the linker to link against libjpeg.so (the linker will resolve this to libjpeg.so.1.2.3 based on the soname)

LB_LiBRARY_PATH environment variable

  • specifies a list of directories where the linker should look for shared libraries at runtime
  • if the shared library is not found in the standard locations, the linker will search the directories

Shared object libraries (.so files) on Linux/Unix systems are primarily located in standard paths like /lib, /usr/lib, and /usr/local/lib. The dynamic linker (ld.so) searches these directories, along with paths defined in /etc/ld.so.conf and LD_LIBRARY_PATH`, to resolve library dependencies at runtime

Static Libraries

References:

  • https://www.youtube.com/watch?v=UdMRcJwvWIY
This post is licensed under CC BY 4.0 by the author.