Write Better Code! | How to Create Shared Libraries in C/C++

Поділитися
Вставка
  • Опубліковано 4 гру 2024
  • In this video we talk about how to program our own custom libraries in C. We go deep on the process of implementing and creating shared libraries. Specifically, you will learn what shared libraries are, the benefits of creating your code as a shared library, and how program your own custom library.
    Subscribe for more videos on low level computer topics!
    Join us on Discord! / discord

КОМЕНТАРІ • 67

  • @bbug705076
    @bbug705076 2 роки тому +27

    Thanks for the content. I did benefit from this channel

  • @delqyrus2619
    @delqyrus2619 Рік тому +3

    Thanks! I knew how to write libraries, but i never completely understood what is happening under the hood. Now i understand at least more!

  • @hetaeramancer
    @hetaeramancer 3 роки тому +32

    i hope you do more C videos, particularly ones involving pointers on structures, unions, or arrays

  • @renichbon
    @renichbon 2 роки тому +6

    Pretty good content, man. Keep it up.

  • @catsby9051
    @catsby9051 7 місяців тому +10

    Statically linking libraries is, more often than not, the right way to go. In theory, shared libraries are nice but in practice, not a lot of sharing can actually take place. To ensure better portability, most developers will distribute the necessary shared libraries along with their application because they cannot be sure that the system that the app is installed on will have the necessary versions of the shared libraries. This fact nullifies the arguments around disk and RAM usage. In the case of containers, the entire file system is shipped with the application. Including all shared libraries.

  • @darren15
    @darren15 2 роки тому +5

    Awesome video. I had wondered for years how shared libraries worked. Thanks alot.

  • @TheCoder557
    @TheCoder557 Рік тому +6

    Shared libraries are very useful, especially when implementing Python in C!

  • @Stevesteacher
    @Stevesteacher 2 роки тому

    Thank you, I've been struggling with this for a while!

  • @the_yugandharr
    @the_yugandharr Місяць тому

    why did i discover this old gold video so late!

  • @AllanAlmeidaOficial
    @AllanAlmeidaOficial 4 місяці тому

    Thank you so much for this video, very well explained and putting into practice really made the difference! 🎉

  • @Speykious
    @Speykious 3 роки тому +7

    Fun fact ─ related to shared libraries, but not quite C: Rust, as nice of a language as it is, doesn't link its standard library dynamically. Meaning that each time you do any kind of executable, it takes at least 3 MB of space from the get go because Rust's standard library has been appended. There is still a -prefer-dynamic compiler flag, but it is statically linked by default. I'm not informed enough to know why it's like that. Maybe in the future it will become mature enough to prefer dynamic by default, because that would definitely optimize a lot of space, given how much programs are being written in Rust these days.

    • @honkhonk8009
      @honkhonk8009 2 роки тому

      I think its because with most programs to just get stuff up and running, you dont really care much about optimization. that stuff comes last yk.
      Cleaning up your source and optimizations come after u have a working product.

    • @Speykious
      @Speykious 2 роки тому +5

      @@honkhonk8009 Programming optimizations are separate from static/dynamic linking. Lots of people have been telling me however that the actual reason this is the case is because the Rust ABI (Application Binary Interface) is not stable, and that having a stable ABI would severely restrain the ability of the language to be changed, which goes against the goals of the core team.
      I did ask myself if it would be possible for Rust to ever become stable enough for the ABI to be as stable, though I know nothing about ABIs unfortunately

  • @mage0966
    @mage0966 2 роки тому +2

    Great video, i found it very interesting, but there are too many concepts for just one video, i hope that you could make another video or course for explain this concepts more deppeer.

  • @cd-stephen
    @cd-stephen Рік тому

    i learn so much here - thank you for your channel

  • @charliebanales5287
    @charliebanales5287 2 роки тому +2

    Your videos are awesome, thank you so much!

  • @leocode128
    @leocode128 2 місяці тому

    Hi , it will be really great if you make a video on complete compile process for c or cpp , like what does the -i flag does , whats pkg-config what is linking, how to create a linker script for embedded programming etc . Its very hard to find resources explaining all these and i really want to know the ins and outs

  • @1873Winchester
    @1873Winchester Рік тому +3

    It feels a bit clumsy but I suppose that is to explain it better what is happening. But in a production environment I assume it's done so that the end user who will run the program, doesn't need to include the library path every time? Perhaps the library is installed in the default library path by an installer?

    • @autistadolinux5336
      @autistadolinux5336 Рік тому

      There's a way to change that path for each executable, as far as i know, and desktop entries (icons) usually also include a LD_LIBRARY_PATH at execution.
      But for most of the time its completely useless to have an shared library, unless you want to preserve the same functionality across the whole system (libc's are a good example of this) or you want to make some sort of plugin system (GIMP and Xfce panel plugins are a good example of this).

  • @jonathanmoore5619
    @jonathanmoore5619 3 роки тому +3

    Just found you. Great expo.

  • @JohannY2
    @JohannY2 2 роки тому +1

    I would suggest that you zoom in more, for those of us that watch on phones.

  • @CHEL0VEK100
    @CHEL0VEK100 8 місяців тому

    It was a good video, thanks! Do you have a video about static libraries? ❤

  • @colydeane
    @colydeane 4 роки тому +6

    Nice explanation.

  • @MohammadHabash-b7r
    @MohammadHabash-b7r Рік тому

    Thank you for this useful video!

  • @a.v7998
    @a.v7998 7 місяців тому

    Awesome video man!!

  • @alexandrohdez3982
    @alexandrohdez3982 Рік тому

    you are the best 👏👏👏...

  • @eng.shh80
    @eng.shh80 2 роки тому +2

    Great , thank you!

  • @AndrewMelnychuk0seen
    @AndrewMelnychuk0seen 2 роки тому

    This was so good! Thanks!

  • @PurpleMenace297
    @PurpleMenace297 9 місяців тому

    I have two questions.
    1. At 11:45 -L is specified during the gcc command to "let the linker know where to find the shared library". But regardless you still need to set the envvar LD_LIBRARY_PATH. is this because the linker and loader are not the same?
    2. During the compilation of the shared library you specified fpic to "make it address independent" what are the consequences of not specifying this? And similarly why exactly do we need to specify this?

    • @LeaoMartelo
      @LeaoMartelo 6 місяців тому

      hello i know this is 2 months ago, but i think i can reply to the second question in a simplified way
      not making it address independent will make it look for the same addresses in ram every time, that might not work depending on your program, using that flag makes it set the address table at link time for the program

  • @bbug705076
    @bbug705076 2 роки тому +4

    !

  • @acommunistdwarf
    @acommunistdwarf Рік тому

    Can you selectively load them in the program or does the selection of the library that provides the functions have to be done at compile time?

  • @brecoldyls
    @brecoldyls 2 роки тому +2

    Great video! When linking, is it always necessary to set the LD_LIBRARY_PATH environment variable? I was able to link and run a test program without doing so, and I'm wondering what the general conditions are for needing to vs not needing to set it

    • @_denzy_6310
      @_denzy_6310 2 роки тому +1

      Linker will set a static location where the so is located. If it changes, you get so not found. Check with 'ldd' command

  • @peterjansen4826
    @peterjansen4826 Рік тому

    You probably don't read this given that it is a 2 years old video. The -lmmath part at 11m38s is confusing. The name of the library is mmath. If you use an option (-l) then usually you would have a space after it to give the string but you typed it without a space and it works.

    • @acommunistdwarf
      @acommunistdwarf Рік тому

      It's how GCC works, that's why the library has to be named starting with lib.

  • @ajayjadhav900
    @ajayjadhav900 2 роки тому +1

    How to include sub-directories in Visual Studio?
    I have to include many header files, which are in different sub-directories. Is there a way in Visual Studio (I am using 2013 edition) to set one include path that Visual Studio will search also the sub-directories for header files?
    My Project is in C/C++.
    Add the "base folder" to the project (project properties -> Configuration Properties -> C/C++ -> Additional Include Directories, "additional include directories")
    I have tried the above option but it is not possible for me to add each and every directory followed by a semicolon.
    I have total 60 + different C C++ sub-directories

    • @erdican7749
      @erdican7749 2 роки тому

      Actualy it includes everyhing at that path to your include path so you could include like this:
      #include "folder/header.h"

  • @kevinshao9148
    @kevinshao9148 2 роки тому

    Thanks for the great video! 2 questions would like to confirm:
    1) so from your video, this .so library will be linked at run time, not pre-compiled in the executable main, right?
    2) and you don't need to include this shared object library? but you have to put declaration?
    Thanks a lot!

    • @thediaclub4781
      @thediaclub4781 Рік тому +1

      2) The declarations are necessary but often put into header files. Like printf probably is declared somewhere in stdio.h

    • @kevinshao9148
      @kevinshao9148 Рік тому

      @@thediaclub4781 Thanks a lot for kind reply!!!

  • @alfajrdream
    @alfajrdream 2 роки тому

    Very nice !!

  • @alexobzor
    @alexobzor 2 роки тому

    If you hadn't declared these functions in main.c, would it succeeded? I think that it wasn't necessary, but I don't know exactly.

    • @virtualouise
      @virtualouise Рік тому

      Generally they would be in some header file that you have to declare in your main.c

  • @meenamurthy1503
    @meenamurthy1503 2 роки тому

    Thank you!

  • @empajerek4193
    @empajerek4193 3 роки тому +2

    Great!!!

  • @xarax7950
    @xarax7950 6 місяців тому

    thanks !

  • @believeinpractical3930
    @believeinpractical3930 3 роки тому +1

    Awesome

  • @epocfeal
    @epocfeal 2 роки тому

    thank you!

  • @uzumakiuchiha7678
    @uzumakiuchiha7678 Рік тому

    DWM?

  • @kelseystickney8663
    @kelseystickney8663 Рік тому

    You misspelled “maintenance”. Didn’t know if you realized that 3 years after the fact. Haha

  • @nettoaoquadrado
    @nettoaoquadrado 2 роки тому

    Fico muito grato por ter acesso a um conteúdo de qualidade de maneira totalmente gratuita. Obrigado de coração

  • @kodosunofficial_3
    @kodosunofficial_3 2 місяці тому

    build a shared library for android is so painful

  • @poutineausyropderable7108
    @poutineausyropderable7108 3 місяці тому

    I see you are not a ricer.
    Base I3.

  • @henryg2350
    @henryg2350 Рік тому

    Sob, I can’t watch damn videos with loud ass keyboards … they cringe me so hard idk why… but can you show give a link to the info you explained might be??

  • @sj82516
    @sj82516 Рік тому

    Thanks for the good content! Just wondering why you name your shared object as `libmy_math.so` but the linker are finding `libmmath.so` ? How the naming match to each other ?

    • @xeniaxu4452
      @xeniaxu4452 9 місяців тому

      In my macbook, after I renamed the libmy_math into libmmath, the linker can't work, it still looked for libmy_math obj but not libmmath

  • @mytechnotalent
    @mytechnotalent 3 роки тому +2

    Great vid! How would we do this for the Pico? I was able to get the lib created with arm-none-eabi-gcc -o libtest.so -fpic -shared -s test.c ? What would have to be done in the CMakeLists.txt file?

    • @LowLevelTV
      @LowLevelTV  3 роки тому +2

      The problem is that the Pico only takes one file as input and doesn't do runtime linking. You could link your library into your program at compile time by producing archive files (libtest.a instead of libtest.so). Then your CMakeLists file would pull them in to the UF2 file.