[Day 16] - Static and Dynamic Libraries (ar, objdump, ld, ldd) - Crash Course in C Programming

preview_player
Показать описание

►Lesson Description: In this lesson I show you how to create both static and shared libraries on linux. Static and shared libraries are both ways to allow for code reuse, and another way to break large projects into smaller more manageable pieces. Using libraries can also have advantages in our build systems, by allowing for separate compilation for instance.

00:00 introduction
00:50 ldd tool usage
2:28 Physical structure of project
3:12 Header guards
5:08 The include directory (-I)
6:30 Building individual .o object files as static library
10:10 Creating two .o files.
11:50 Compiling with two .o files
12:18 The advantage of separate compilation of our .o files
13:55 The archive (ar) tool.
17:00 The advantage and use case of shared object (.so) files
18:10 Building a shared library with position independent code
20:30 Understanding include and linking errors (undefined reference)
22:48 Understanding format of how to link libraries with -l (lowercase L)
25:40 LD_LIBRARY_PATH to utilize our shared libraries
27:40 Summary
31:26 dlopen preview
32:11 Conclusion

►Please like and subscribe to help the channel!
Рекомендации по теме
Комментарии
Автор

As a beginner to c, this is my sum up.Hope to help someone like me:)
1.complie multiple c files under the src directory: gcc ./src/main.c ./src/mymath.c ./src/mymath2.c -I./include -o prog -> prog
2.complie all c files under the src directory: gcc ./src/*.c -I./include -o prog -> prog
3.creat static library: gcc -c ./src/main.c -I./include -> main.o
4.using the archive tool: ar rcs mymath.a mymath.o mymath2.o -> mymath.a
5.embed the static library into the final executable: gcc main.o mymath.o mymath2.o -o prog -> prog
6.creat dynamic library(shared library) : gcc -fPIC -shared ./src/mymath.c -I./include -o libmymath.so -> libmymath.so
7.linked with the dynamic library : gcc ./src/main.c -I./include -o hello -lmymath -lmymath2 -L./ -> hello
8.run the hello program: LD_LIBRARY_PATH="./" ./hello
9.the difference between static library and dynamic library: the code of the static library gets embeded into the final executable at compile time, but the code of the dynamic library does not get embeded into the executable.instead, the linking happens at runtime, this requires the dynamic library to be present on the system at runtime.

timesink
Автор

The fact that you experience the errors and solve them make the lesson worthwhile so we are able to watch out for the same errors in the lessons are really appreciated

richardkombo
Автор

Great lecture, you gradually move from beginner to advanced level concepts. Great teaching skills sir.

dd-iuiy
Автор

This is exactly what I was looking for. Whole day I have been working on the c++ series and luckily found this. Thanks.

Southpaw
Автор

Wow! Thanks Mike (my namesake) This is really nice, I enjoyed the lesson, I like the way you simplify and break down the compilation process, thumb up to you!!!

MichaelOzoro
Автор

Such a fantastic tutorial on fundamentals.
I'm learning openCV using c++ and need to brush up on my basics (because of all the header & lib includes to get started). This video is just perfect.
Hope you do windows as well.
Cheers,

b

behrampatel
Автор

Thank you Mike, its a great demo hope you can pace down a lil bit i found it hard to keep up.😊

antenehtaye
Автор

Hey, man! Your contents is helping me a lot. Hugs from Brazil!

felipealemanha
Автор

this topic was just amazing. Now I know what its meant when they say that "GCC" does some magic into compiling process. Looking forward to learn more about this topic

rzwnhmd
Автор

Awesome piece. Can one get the "tree" source file you used during the lecture?

ugochukwuokpoko
Автор

Hallo Mike, Thumb up for the videos and this topic.
I will wash it later.

blaisofotso
Автор

Can you please talk about dlopen u mentioned at the end of the lesson? Thank you so much bro it was very helpful.

Random-smgi
Автор

Thanks, but this lesson was the hardest for me.

aghiadalzein
Автор

Do you have any plans covering generic programming in C?

reverseila
Автор

Hi, I'd like to ask a question if you dont mind. When you wrote mymath.c you included mymath.h header to it, but when I tried It actually perfectly work without including mymath.h to mymath.c file. I guess we don't need to add that but since you added I wonder if there is a special reason for that.

MuhammetUçan-yc