How software libraries work?

Поделиться
HTML-код
  • Опубликовано: 29 июл 2024
  • In computer science, a library is a collection of non-volatile resources used by computer programs, often for software development.
    Programming Libraries are nothing new - they have been around for roughly 70 years, and they’re used by almost all programs written today. Even the simplest helloworld program written in C++ is using multiple libraries. Some of them most likely - you never heard of.
    So what are software libraries, and how do they work? Obviously you already know they are pieces of code written for you by some other developer, so you don’t have to do everything from scratch. And to be fair: most programmers won’t need to bother themselves with details how this works. Well at least until it doesn’t. Then my friend, you open the hatch to figure out what is going on, and boy oh boy. Things get a little overwhelming. So today we’ll look into these building blocks that make up our environment and let us do our job.
    #programming #tech #softwaredevelopment
    refspecs.linuxbase.org/elf/elf...
    en.wikipedia.org/wiki/Executa...
    First, we have code-generation libraries. These are special tools that read configuration stored in the code, or externally to create more code that will be later read to the compiler. Example in java could be: project lombok, mapstruct library, or tools like xtext, For C# you’ll find powerful T4, and for C++ - well, there are libraries that work on preprocessor level like GTest, and some could argue that the whole template system is nothing else but a glorified code generation tool.
    Static libraries come second. They are also called compile-time libraries and for a good reason: they’re permanently sewn into the machine code of our program’s executable during the compilation. Machine code compiled from your source will be merged with machine code of libraries during the linking process into a single binary file.
    Dynamic libraries are a little different: Linker reads them when building your binary to check if everything is correct but it doesn’t actually add libraries to your code. They belong to the operating system, and often multiple programs use the same libraries (that’s why they are also called shared objects). Whenever a user runs your program - the operating system will check if perhaps the library is already loaded, because another process is using it. If that’s the case - it will share the library's address with your program. Otherwise it will just load it before running your code.
    Now let’s talk briefly about remote libraries. These allow programmers to use RPCs or remotely called procedures. While code of such a library can actually run on the same machine, it has substantial overhead compared to say dynamic libraries. All this gets justified if you can benefit from distributed architecture. Simplest example is your trusty database: your program most likely has a database client, and this client is calling the database server remotely to fetch some data. There are whole frameworks that support writing such libraries, for example gRPC.
    Today I want to dive a tad deeper into shared libraries. This is what my recent work in Cisco is currently about: I’m putting one of our services written in C++ into a Docker container, and correctly managing shared libraries is really a huge part of the task.
    As I have mentioned before: even the smallest program like hello world uses libraries, and shared ones too. So let’s try it. I’ll spin up a docker container and we’ll take a look at these shared libraries.
    For my docker image I’m using ubuntu with builtin clang-12, my current go-to compiler for C++. I’ll add two packages here: paxutils and ncurses-library. Paxutils brings some useful tools to analyse binaries, and ncurses is just an exemplary dependency I’ll use in my code to color text output.
    As promised I’ll compile a simple hello world program: nothing fancy, just plain string sent to standard output. I can compile this from the command line and I’ll use a standard tool to list dependencies: LDD.
    As you can see: there are few libraries that are used here: kernel vdso (virtual dynamic shared object), standard C++ library, libm (c math library), gcc (low-level runtime library), libc (c-standard library) and lastly: interesting ld-linux…..so.
    This last row is a clue to what we’re after: dynamic linker library.
    - Wait. What is that dynamic linker?
    - This is the system component that is responsible for finding dynamic libraries that the program wants to use. Interesting fact is - that each program decides which linker is going to handle dependencies.
    - Wait so there are multiple dynamic linkers?
    Well, fortunately not really, no. In practice we use one linker in the operating system. But here’s the catch: binaries in linux follow the ELF standard, and it says that binaries contain sections, and in one of these sections every binary defines a program interpreter.
  • НаукаНаука

Комментарии • 20

  • @aloufin
    @aloufin 3 года назад +14

    shot of whisky everytime smok says 'libe-laries' ! awesome video, keep it up =)

    • @SmokCode
      @SmokCode  3 года назад +3

      Lawl! I'm actually working on my weird accent with a dedicated tutor, stay tuned and see how it evolves!

  • @Mr_SSK
    @Mr_SSK 2 года назад +2

    The background is damn cooool..
    Anyways, the explanation was really helpful!

  • @pepe6666
    @pepe6666 5 месяцев назад

    i am loving these breakdowns. just explanation of how the architecture works from a meaningful context - its so useful. also i love the way you say libraries. thanks for the insight to see that this needed to be explained in this manner.

  • @breakingmhet8078
    @breakingmhet8078 3 года назад +1

    super helpful, thank you!

  • @someaccount-mp4tk
    @someaccount-mp4tk 15 дней назад

    Great content

  • @MrAman47
    @MrAman47 Год назад

    After listening to you say "liblary" 40 times in this video, I can now say that I also say "liblary". Great video btw!

  • @wanfaradiba2650
    @wanfaradiba2650 2 года назад

    what software did use by application computerized library system?

  • @eclipsetutorialvideo
    @eclipsetutorialvideo 3 года назад +1

    Could you please explain why you needed to go inside the dynamic libraries to make your cpp program work in your container?
    What error did you meet?
    By the way to use the same image do : docker pull clangbuiltlinux/ubuntu
    Thanks

    • @SmokCode
      @SmokCode  3 года назад +2

      Sure. When you're docker container - you're using certain distribution of OS, with only few libraries that are available. More complex programs use a lot of libraries, some of them are available as dynamic. In this case I was debugging why postgresql client lib didn't work with my program. I found out that postgres depends on kerberos, which was installed in my system, but in an incompatible version. So now I could either switch the kerberos version or change the version of postgres to match what I had in the system. Other way is providing a directory for dynamic linker to show where your custom versions are, but that's another can of worms.

    • @eclipsetutorialvideo
      @eclipsetutorialvideo 3 года назад

      @@SmokCode Thanks for having provided the context :) I never thought that learning how libraries are used could be so different. Could we say that loading .net libraries are dynamic then?

  • @mshingote
    @mshingote 3 года назад +1

    Which book you were referring in this video?

    • @SmokCode
      @SmokCode  3 года назад +1

      Welcome on the channel!
      These are good sources:
      refspecs.linuxfoundation.org/elf/elf.pdf (ELF spec)
      amzn.to/3mZnsNN (Linkers and Loaders by Levine)

    • @mshingote
      @mshingote 3 года назад

      @@SmokCode Is there any similar available for windows platform?

    • @SmokCode
      @SmokCode  3 года назад +1

      You can use the official reference for PE format (exe, dll and other)
      docs.microsoft.com/en-us/windows/win32/debug/pe-format
      I don't know which book is a good one for windows.

  • @kukiezi
    @kukiezi 3 года назад +1

    Wow, I have never heared about most of those things while learning on university or in my first two jobs. If I may ask, did You learn about those things beacause it was needed for specific job or do You just read about those topics in your free time :D

    • @SmokCode
      @SmokCode  3 года назад

      I needed this so my docker container works correctly on few old machines (debian jessie).

    • @kukiezi
      @kukiezi 3 года назад

      @@SmokCode Thank You. Great video and explanation. Tho one really needs to read more about that to get into the topic deeply.

  • @ROSALIEIK
    @ROSALIEIK 8 месяцев назад

    its new to me lol.

  • @sandipansarkar9211
    @sandipansarkar9211 3 года назад

    too advanced