How to Load Libraries at Runtime

แชร์
ฝัง
  • เผยแพร่เมื่อ 12 ธ.ค. 2024

ความคิดเห็น • 58

  • @raksipulikka
    @raksipulikka 4 ปีที่แล้ว +28

    I used this stuff in my game engine to enable user to use C as a scripting language. User provides a filename -> file is compiled into a shared library -> shared library is linked with the host program and all that during runtime. Super cool and powerfull!

    • @JacobSorber
      @JacobSorber  4 ปีที่แล้ว +5

      Nice. Glad it was helpful.

  • @OhhBabyATriple
    @OhhBabyATriple 5 ปีที่แล้ว +53

    Your channel is in my opinion top 5 on TH-cam for programming, outstanding every time.
    You deserve way more exposure.

    • @JacobSorber
      @JacobSorber  5 ปีที่แล้ว +5

      Thanks! It's always good to hear that I'm helping. Please help spread the word.

    • @user-sl6gn1ss8p
      @user-sl6gn1ss8p 4 ปีที่แล้ว +2

      I know it's been a year, but in case you get this, what would be the other four? : p

  • @dimasveliz6745
    @dimasveliz6745 3 ปีที่แล้ว +9

    Oh man, 2 years ago you filmed this, still I wanted to thank you! Awesome and concise video!

    • @JacobSorber
      @JacobSorber  3 ปีที่แล้ว +2

      You're welcome. Glad it was helpful.

  • @RonyBatistaInoa
    @RonyBatistaInoa 5 ปีที่แล้ว +12

    Amazed to see high quality engaging videos on more advanced C topics. Great work.

  • @SirusStarTV
    @SirusStarTV 4 ปีที่แล้ว +7

    Zero dislikes is evidence that there's only people that seek that type of information.

    • @CoolKoon
      @CoolKoon 3 ปีที่แล้ว +3

      Yep, no frustrated teenagers procrastinating with their studies or searching for yet another cat video/PewDiePie wannabe :D

    • @netanelkomm5636
      @netanelkomm5636 2 หลายเดือนก่อน

      I watch this for entertainment while I eat tbh😂 Maybe I'll come to this channel when I go through more advanced CS concepts like this further in my degree.

  • @grevierx
    @grevierx 2 ปีที่แล้ว +1

    Thank you so much for all the awesome videos. I'm a grad school CS student and still rely alot on your videos to really learn the detailed stuff. Great production and the fact that you address alot of the topics they don't really teach in school really makes this a special channel! Really appreciate it!

  • @seismicman
    @seismicman 3 ปีที่แล้ว +10

    I needed to add the flag -ldl to the end of the Makefile line: $(CC) $(CFLAGS) -o $@ $^ -ldl
    Otherwise, I get " undefined reference to `dlopen' " and the other dl*** commands.
    Thanks for the videos. An excellent learning resource.

    • @nikiparza731
      @nikiparza731 3 ปีที่แล้ว

      Had this issue when I compiled with gcc on linux. Thanks !

  • @papasmurf9146
    @papasmurf9146 2 ปีที่แล้ว +2

    I always appreciate learning something new. Thank you.
    Just as an aside, I typoed the library name that was fed to dlopen. At least on Linux 5.16.9-100.fc34.x86_64 and gcc 11.2.1, the errno is not set. Calling perror just returned "success".

  • @ashwinijadhav4211
    @ashwinijadhav4211 4 ปีที่แล้ว +2

    So nicely explained.

  • @szymach
    @szymach 2 ปีที่แล้ว +2

    I wish people talked about this use case when teaching function pointers.

  • @hsaidinsan6345
    @hsaidinsan6345 4 ปีที่แล้ว +2

    One word, Perfect 👌

  • @karlforshaw
    @karlforshaw 5 ปีที่แล้ว +3

    Thanks for this Jacob!

  • @madinabah7002
    @madinabah7002 4 ปีที่แล้ว +2

    This was super clear. Thank you!!

  • @NANA-fx1rt
    @NANA-fx1rt 5 ปีที่แล้ว +2

    Good job Jacob!

  • @jonajo261
    @jonajo261 3 ปีที่แล้ว

    great video , clear , rapid, simple.

  • @kushagrakumar9576
    @kushagrakumar9576 5 ปีที่แล้ว +3

    Excellent content 👍

  • @abhisheksa6635
    @abhisheksa6635 ปีที่แล้ว

    People make DSA and all, these are important concepts to do something after coding, where you scratch your head and run in loops.

  • @romagnolirojas10
    @romagnolirojas10 2 ปีที่แล้ว +1

    i keep getting a seg fault calling opfunc .. so frustrating
    dsym returns NULL for me

  • @srijanpaul
    @srijanpaul 4 ปีที่แล้ว +3

    Thank you so much for this! Great explanation and perfectly paced.
    How would one make such a thing cross platform? using ifdef macros to make the right call on different OSs?
    But then how would one resolve the filename being `.so` vs `.dll` conflic

    • @cobalt2489
      @cobalt2489 3 ปีที่แล้ว

      Either let the user specify a path or use ifdef macros when compiling. Otherwise you could check platforms or match paths.
      This may be avoided completely though by distributing shared libs with a system agnostic file name, such as runtime.common. I remember vaguely about some libraries doing this but can't remember anything specific. Needless to say this is usually against conventions and guidelines.

    • @srijanpaul
      @srijanpaul 3 ปีที่แล้ว

      @@cobalt2489 yeah I looked lua's loadlibrary module recently and it does something similar

  • @TheLeontheking
    @TheLeontheking 3 ปีที่แล้ว

    Being on linux, I had to add the direcory of the libraries (which was the same as executable and working-directory) to the LD_LIBRARY_PATH environment-variable, otherwise the dlopen-calls would fail.

  • @jessemiller8387
    @jessemiller8387 3 ปีที่แล้ว +3

    Didn't realize Matthew McConaughey can code

  • @kamalmoustafa318
    @kamalmoustafa318 2 ปีที่แล้ว

    How to load arrays, structs, classes (in case of cpp)? For example if i have a script with onLoad, onExit, onUpdate(float dt), how can i load the whole struct with functions and variables ?

  • @shvideo1
    @shvideo1 3 ปีที่แล้ว +1

    It would be great if your 2 part video, a great ones at that, covered Windows env. as well. Thank you.

    • @JacobSorber
      @JacobSorber  3 ปีที่แล้ว +2

      I know. And, it probably would, if I ever used windows, in my day-to-day work. Maybe a future project will get me back in Windows land and I'll fill in some of these gaps.

    • @shvideo1
      @shvideo1 3 ปีที่แล้ว

      @@JacobSorber Thank you Jacob. It's so hard finding material on the Internet on command line compiling of any language in Windows. There are zillions of "developers" who only dream in the Visual Studio IDE. I am surprised at that. May be the hard-core ones can't be bothered to post their knowledge. Appreciate the positive response.

  • @0xcf830
    @0xcf830 2 ปีที่แล้ว

    I love when things come from window API, there are 3 pasue in the video.

  • @erl2nd
    @erl2nd 2 ปีที่แล้ว

    Very interesting, I noticed you are using VS Code, any suggestions to set up on Windows 11?

  • @ashutom14
    @ashutom14 3 ปีที่แล้ว +1

    what is the editor you are using?

    • @JacobSorber
      @JacobSorber  3 ปีที่แล้ว

      I used to use atom. My recent videos are using vs code.

  • @user-mr3mf8lo7y
    @user-mr3mf8lo7y 2 ปีที่แล้ว

    Love the video. Short and sweat. Need to get your comments.... In this age of gHz CPUs, would it make siginificant difference creating static executable or utilizing with shared libraries in terms of performance (in abstract thinking)? Is there a somewhat (critical) relationship between size of executable (excluding text/comments used, splitted) and (initial) load of that executable? How the size matter? Cheers,.

  • @ryuokami8924
    @ryuokami8924 ปีที่แล้ว

    Great video but I have a question. It works for me but only if it's in the main fonction. If the fonction is named "lol" or "other". It doesn't work.. Why ?

  • @KangJangkrik
    @KangJangkrik 3 ปีที่แล้ว

    Is it possible to load library dynamically with common header?

  • @alicia696
    @alicia696 3 ปีที่แล้ว

    Does anyone know how to find the correct name of the function we are trying to access from dlsym?
    thanks

  • @JayDee-b5u
    @JayDee-b5u 6 หลายเดือนก่อน

    How about reloading libraries during runtime? Seamlessly?

  • @aabdev
    @aabdev 4 ปีที่แล้ว

    Dear Jacob Sorber,
    Suppose you debug a Linux application and put a break-point into a function defined by a dynamically-linked library used by the application.
    Will that break-point stop any other application that makes use of the same library?
    Regards,
    AB

    • @CoolKoon
      @CoolKoon 3 ปีที่แล้ว +1

      No, because when you're debugging, you're debugging your target executable, not something else. And such breakpoints only exist within the scope of that specific debug session anyway.

  • @manojsk4883
    @manojsk4883 3 ปีที่แล้ว

    I encountered this error in Sypder , in the following line:
    ImportError: dlopen: cannot load any more object with static TLS..
    Spyder version is: 4.1.5.
    Help me fix this error .. It is Urgent, sorry to ping you

  • @yifangtan4207
    @yifangtan4207 3 ปีที่แล้ว

    The line to print out the function name using function pointer always gave seg fault in my gcc 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04) !
    printf("Library: %s
    ", libnamefunc());
    Anybody has the trick to resolve this problem?

  • @sukivirus
    @sukivirus 3 ปีที่แล้ว

    Can we create dynamic lib (dll) on windows by using gcc ?

    • @JacobSorber
      @JacobSorber  3 ปีที่แล้ว +1

      Yeah, you should be able to. I've seen it done on cygwin, though I don't know if there are any cygwin dependences.

    • @sukivirus
      @sukivirus 3 ปีที่แล้ว

      @@JacobSorber I managed to make DLL on windows using VStudio. I try to avoid using VStudio as when I program it gives out several warnings about potential memory leaks and some annoying errors when you try to use some of the std functions of C. I wanted to know the difference between writing _stdcall and _declspec? Also is it only useful to use extern C if we are trying to use the library in C++?

  • @alexwang007
    @alexwang007 3 ปีที่แล้ว

    Would this work for embedded C😂 I only know how to work with microprocessors

  • @leonardobasbous7298
    @leonardobasbous7298 3 ปีที่แล้ว +2

    -ldl linux

  • @jakobfredriksson2272
    @jakobfredriksson2272 3 ปีที่แล้ว

    Lol. I'm going crazy. My code (identical with Jacob's) segfaults! That, or whining over not finding the library :) If anyone have a clue:
    OS: Ubuntu 20.04.2 LTS,
    Type: 64 bits,
    GNOME Version: 3.36.8

    • @boxedowl
      @boxedowl 3 ปีที่แล้ว +3

      If it's anything like the issue I had try two things:
      1.) Check your Make file again VERY carefully. I had a segfault with perfect C code and tracked it down to a single small typo in the make file.
      2.) If that all checks out copy the .so file into /usr/lib then if it still complains about finding it move the .so file to /lib and try again.
      Once I did both of these mine ran without the segfault.

    • @nikiparza731
      @nikiparza731 3 ปีที่แล้ว

      Check that you indeed copied the .so files to the /usr/lib folder. I have 16.04 LTS Ubuntu and it worked on mine.

  • @r.faj.5636
    @r.faj.5636 11 หลายเดือนก่อน

    See this is why I don't understand all these bloated web frameworks like react, electron etc. Operating Systems are built on the core idea of being able to use a computer in a dynamic way. C can have hot reloading in your application. You'll have better performance and a less bloated application with all the benefits.

    • @soniablanche5672
      @soniablanche5672 10 หลายเดือนก่อน

      development speed is more important than performance in web development. You are not doing time sensitive operations or looping over millions of data on the web. It's a waste of time to think about such optimization on the web.