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!
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.
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!
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.
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".
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
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.
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.
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 ?
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.
@@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.
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,.
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 ?
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
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.
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
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?
@@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++?
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
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.
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.
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.
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!
Nice. Glad it was helpful.
Your channel is in my opinion top 5 on TH-cam for programming, outstanding every time.
You deserve way more exposure.
Thanks! It's always good to hear that I'm helping. Please help spread the word.
I know it's been a year, but in case you get this, what would be the other four? : p
Oh man, 2 years ago you filmed this, still I wanted to thank you! Awesome and concise video!
You're welcome. Glad it was helpful.
Amazed to see high quality engaging videos on more advanced C topics. Great work.
Zero dislikes is evidence that there's only people that seek that type of information.
Yep, no frustrated teenagers procrastinating with their studies or searching for yet another cat video/PewDiePie wannabe :D
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.
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!
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.
Had this issue when I compiled with gcc on linux. Thanks !
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".
So nicely explained.
I wish people talked about this use case when teaching function pointers.
One word, Perfect 👌
Thanks for this Jacob!
This was super clear. Thank you!!
Good job Jacob!
Thanks.
great video , clear , rapid, simple.
Excellent content 👍
People make DSA and all, these are important concepts to do something after coding, where you scratch your head and run in loops.
i keep getting a seg fault calling opfunc .. so frustrating
dsym returns NULL for me
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
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.
@@cobalt2489 yeah I looked lua's loadlibrary module recently and it does something similar
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.
Didn't realize Matthew McConaughey can code
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 ?
It would be great if your 2 part video, a great ones at that, covered Windows env. as well. Thank you.
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.
@@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.
I love when things come from window API, there are 3 pasue in the video.
Very interesting, I noticed you are using VS Code, any suggestions to set up on Windows 11?
what is the editor you are using?
I used to use atom. My recent videos are using vs code.
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,.
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 ?
Is it possible to load library dynamically with common header?
Does anyone know how to find the correct name of the function we are trying to access from dlsym?
thanks
How about reloading libraries during runtime? Seamlessly?
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
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.
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
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?
Can we create dynamic lib (dll) on windows by using gcc ?
Yeah, you should be able to. I've seen it done on cygwin, though I don't know if there are any cygwin dependences.
@@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++?
Would this work for embedded C😂 I only know how to work with microprocessors
-ldl linux
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
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.
Check that you indeed copied the .so files to the /usr/lib folder. I have 16.04 LTS Ubuntu and it worked on mine.
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.
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.