Creating and Linking Static Libraries on Linux with gcc

  Рет қаралды 30,701

embeddedarmdev

embeddedarmdev

Күн бұрын

Пікірлер: 63
@embeddedarmdev
@embeddedarmdev 2 ай бұрын
Here is something I would like to point out that I did not really mention in the video. In this video, I only created a static library, I did not create a corresponding shared library (.so) for this library. As a result of this, the only possibility for linking the code from libmath is statically. The default behavior for the linker is to link dynamically and will therefore look for the shared library first, libmath.so in this example. Since the shared library did not exist, it then looked for the static version (libmath.a) and since it found it, it compiled the libmath code into the binary statically. Now, if I had created both a static (libmath.a) and shared library (libmath.so), then the default would have been to compile it dynamically. It would have looked for and found the libmath.so and then that code would have been compiled in dynamically. In this case, when both static/dynamic libraries exist, in order to force it to go static, you have to include the 'static' keyword. This will look only for the libmath.a and will only be successful if it exists. Incidentally, you can use the -Wl option to pass the -Bstatic and -Bdynamic options to the linker to toggle back and forth between static and dynamic linking. So, if you have the following gcc command: gcc test.c -o test -Wl,-Bstatic -lfoo -Wl,-Bdynamic -lbar All code from libfoo would be compiled statically and all code from libbar would be dynamically linked. There would need to be a libfoo.a and a libbar.so for this to be successful. Also note that any libc code would be compiled based on the final -WL, -Boption that is on the command line. If we do this gcc test.c -o test -Wl,-Bstatic -lfoo Then both libfoo and libc would be compiled in statically, so make sure to add the final -Wl,-Bdynamic option to switch it back to the default.
@binwei2829
@binwei2829 3 жыл бұрын
Really great video. It shows a step by step example to let you know how to create your own one. Thanks!
@adampassaretti9149
@adampassaretti9149 2 жыл бұрын
thank you so much bro
@foxOneRides
@foxOneRides Жыл бұрын
made it very easy to understand. a hidden gem indeed.
@Random-sm5gi
@Random-sm5gi 6 ай бұрын
Very informative. Thanks boss.
@zvxcvxcz
@zvxcvxcz Жыл бұрын
Edit: Sorry, -Wl, was explained incorrectly before with an incorrect example as well. Just an aside, there are some libraries that are not easy or are not possible or practical to compile statically. For example, you can't really include -lGL statically. To tell gcc/g++ to do some libs statically but not others, you can '-Wl, -Bstatic' and '-Wl, -Bdynamic' before the respective libraries. Like this: g++ yourcode.cpp -I someinclude -I someotherinclude -L someliblocation -L someotherliblocation -Wl,-Bstatic -lsomestaticlib -Wl, -Bdynamic -lsomelibnotstatic Some libC functions are problematic, e.g. dlopen(), if I recall is will static compile, but it still won't work if the glibc version doesn't match. Network code is also frequently difficult to get a static compilation for, e.g. libcurl. I was trying to compile htslib myself statically and having problems, but it turns out it's fine as long as you don't need to load/write files over the network (just leave out libcurl). I think they actually do build the static .a file by default (without libcurl), but they say nothing about it in the build documentation. A further aside (I'm no longer on topic so this is just for information for people, it isn't meant as a critique), the old rand() functions are pretty bad. If random numbers are even remotely important to one's application, then they should at a minimum use something like Mersenne Twister, but I would actually prefer PCG. There should be C libraries available for both of these and in newer versions of C++ Mersenne Twister is available in the standard library (but I believe normal rand still works the old way for compatibility purposes, need to call up the RNG the new way). Nice clear video though. On the side I see you have some other videos on the topic, longer as well, perhaps you get into some of these weeds there, I just haven't watched yet.
@nighttime9539
@nighttime9539 Жыл бұрын
What a bunch of random info. It's good to know but I dont see how it relates to the video.
@John223
@John223 Жыл бұрын
Very useful. Thank you ❤
@gattomiaomiao9634
@gattomiaomiao9634 Жыл бұрын
I love your way of teaching, theory plus practical demonstrations are great
@cybernit3
@cybernit3 3 ай бұрын
Thanks embeddeddarmdev. I tested it out and worked fine.
@TheGokhansimsek35
@TheGokhansimsek35 3 жыл бұрын
Super useful. Thx a lot!!
@ratfuk9340
@ratfuk9340 Жыл бұрын
Thanks
@voidlt
@voidlt 3 жыл бұрын
Thank you so much for this video, it helped a lot.
@greydogyoutube
@greydogyoutube 7 ай бұрын
Finally! Thnaks a lot man!
@mateomerino5792
@mateomerino5792 3 жыл бұрын
thank you so much, this video help me a lot. i can advance on a homework of my career! greetings from Argentina, im really grateful.
@multivalfran
@multivalfran 2 жыл бұрын
GREAT video. I'm trully amazed
@masterbeater8844
@masterbeater8844 3 жыл бұрын
this was great man, keep it up although it wouldn't hurt if you go a bit deeper on some of the terms you used....like what non-position-inependent means and why its needed and....... Beginners like me freeze when we hear big terminologies ;)
@nighttime9539
@nighttime9539 Жыл бұрын
Great tutorial.
@embeddedarmdev
@embeddedarmdev Жыл бұрын
Thank you!
@aFausify
@aFausify 3 жыл бұрын
Big thanks man. You're a gem. Great video!
@akshaykumar7152
@akshaykumar7152 3 жыл бұрын
Thanks a lot for this video. I've found a surprising lack of content on C/C++ libraries
@MrCrazyDuck47
@MrCrazyDuck47 2 жыл бұрын
Thanks, very well explained. It would be also nice, if compilation with Makefile would be explained as well
@embeddedarmdev
@embeddedarmdev 2 жыл бұрын
That's a good idea for a topic. I will add it to my list.
@splattab
@splattab 2 жыл бұрын
This tutorial was very helpful thank you
@leninchakravarthy1426
@leninchakravarthy1426 2 жыл бұрын
I really appreciate your work. I learned lot more in your tutorial than I searched online for 1 week. I suggest you make a udemy course on complete practical detailed GCC course
@embeddedarmdev
@embeddedarmdev 2 жыл бұрын
Thank you.
@stefanklaus6441
@stefanklaus6441 2 жыл бұрын
@@embeddedarmdev Do you have a udemy course? I'd definitely recommend it!
@embeddedarmdev
@embeddedarmdev 2 жыл бұрын
@@stefanklaus6441 I do not have a Udemy course. I had considered developing one, but I don't think I have enough content at this time to make a quality course. I also like the idea of people having free access to tutorials.
@nighttime9539
@nighttime9539 Жыл бұрын
​@@embeddedarmdev youtube tutorials is a wonder of modern age. Thanks to authors like you.
@nikudayo7927
@nikudayo7927 2 жыл бұрын
Thanks! This is a really informative tutorial :D
@embeddedarmdev
@embeddedarmdev 2 жыл бұрын
Thank you!
@sulayenhanglimboo5110
@sulayenhanglimboo5110 2 жыл бұрын
Very helpful, thank you.
@youchialiu2599
@youchialiu2599 11 ай бұрын
It really helps🥹🥹🥹🥹🥹
@xelad3362
@xelad3362 3 жыл бұрын
Very informative! great work!
@lihuazhang4637
@lihuazhang4637 2 жыл бұрын
Thanks for your video, Is it possible to create static library using shared library?
@embeddedarmdev
@embeddedarmdev 2 жыл бұрын
I'm not aware of any tools that do this. Often the creators of a library will distribute or make available both a shared library and the static one. Many of the libraries that we link into have both versions on the system. If you only have the shared library, I don't know of any way to create a static library from it.
@lihuazhang4637
@lihuazhang4637 2 жыл бұрын
@@embeddedarmdev Thanks for your quick reply.
@WaveSound123
@WaveSound123 3 жыл бұрын
Thank you so much for your effort, it was very informative and concise. I have been looking for a video to explain manual linking for a while now, thank you again. Kind regards!
@embeddedarmdev
@embeddedarmdev 2 жыл бұрын
Thank you. I'm glad you fund it useful.
@3ensy
@3ensy Жыл бұрын
So happy I found this vid thank you!
@embeddedarmdev
@embeddedarmdev Жыл бұрын
I'm glad you found it too. Thanks.
@ahcenebelhadi955
@ahcenebelhadi955 2 жыл бұрын
Great video, the best explanation i found ! thanks a lot !!
@embeddedarmdev
@embeddedarmdev 2 жыл бұрын
Thank you.
@kesc23
@kesc23 Жыл бұрын
Amazing content! with this i could compile only the "encode" part of webp lib with gcc (as emcc) / ar (as emar). thus, obtaining a smaller .wasm file🚀
@yousefelbaz2699
@yousefelbaz2699 Жыл бұрын
tracing every error that appears to you and finding out its cause for the first person to deal with these programs Very great thing 👌
@Qwerty_945
@Qwerty_945 2 ай бұрын
Hey Sir ! what is -lmath ?
@embeddedarmdev
@embeddedarmdev 2 ай бұрын
The -l option (lowercase L) tells gcc that we want to link to a specific library. The convention is to use the name of the library, without the 'lib' prefix. So, if we have a library called libmath, then we drop the 'lib' part of it to get just 'math'. Then we append that to the -l to get: -lmath In this video, libmath is the static library that I created in the first half of the video using the 'ar' command.
@madscientist338
@madscientist338 2 жыл бұрын
Impressive thanks!!!
@embeddedarmdev
@embeddedarmdev 2 жыл бұрын
Thank you! I appreciate it.
@Nn-Tech
@Nn-Tech 2 жыл бұрын
Am getting an error after adding -L lib undefined reference to 'subNumbers'
@embeddedarmdev
@embeddedarmdev 2 жыл бұрын
Could you paste the exact gcc command you are using to compile? It could be a few things. Are you also getting an undefined reference to addNumbers or only for subNumbers?
@Nn-Tech
@Nn-Tech 2 жыл бұрын
Thanks , I solved it
@henriquebernardes8913
@henriquebernardes8913 2 жыл бұрын
how do you solve it? I'm getting the same error
@embeddedarmdev
@embeddedarmdev 2 жыл бұрын
I can try to help you solve it. Just need to know a bit more. Can you paste the exact gcc command you are using? And also let me know if you see the error for both addNumbers and subNumbers
@henriquebernardes8913
@henriquebernardes8913 2 жыл бұрын
@@embeddedarmdev actually my Header is show me an error flag. my gcc command: gcc main.c -lmath -o main -I include -L lib. and yes, both functions are getting wrong.
@nilanjanmukhopadhyay8369
@nilanjanmukhopadhyay8369 2 жыл бұрын
Great video! Kudos.
@embeddedarmdev
@embeddedarmdev 2 жыл бұрын
Thanks!
@nilanjanmukhopadhyay8369
@nilanjanmukhopadhyay8369 2 жыл бұрын
@@embeddedarmdev Are you still around? I thought you stopped making videos after a few. If that was the case I think you should start again. The way you explain things is really effective!
@embeddedarmdev
@embeddedarmdev 2 жыл бұрын
I'm still around, I just haven't made any videos for awhile. I do them in my spare time, and recently I haven't had much of that. I do have a few more videos on my list and hope to get to them soon. Thanks for your interest!
@tomjoad6993
@tomjoad6993 2 жыл бұрын
What if you're trying to compile on a distro and it returns an error because a shared library is in a non-standard dir but you didn't create the lib so you don't know where it is. What's the easiest way to find the lib location?
@embeddedarmdev
@embeddedarmdev 2 жыл бұрын
On the target system, you can call ldd on the executable itself to see the name of the library it is looking for. For example: ldd program_name It will also tell you what that library name gets resolved to on your target system. Try this on some random executable like mkdir to see how it works. The only way I know of to find where a library is located is to search through directories. You could use the tool 'tree' to help. This doesn't come standard (at least on Ubuntu) so you'll have to install it. cd into the root directory (on your target system). Use the f option to list the full path to each file. You could then pipe it to grep and search for your library name. Maybe something like: tree -f | grep "library_name*.so" This might help you locate it.
Creating and Linking Shared Libraries on Linux with gcc
29:12
embeddedarmdev
Рет қаралды 21 М.
哈哈大家为了进去也是想尽办法!#火影忍者 #佐助 #家庭
00:33
Как мы играем в игры 😂
00:20
МЯТНАЯ ФАНТА
Рет қаралды 3,2 МЛН
How To Get Married:   #short
00:22
Jin and Hattie
Рет қаралды 23 МЛН
Static and Dynamic Linking on Linux with gcc
27:09
embeddedarmdev
Рет қаралды 46 М.
What's so good about dynamic linking anyway?
11:55
Chris Kanich
Рет қаралды 9 М.
Introduction to Compiling for Linux with gcc
30:40
embeddedarmdev
Рет қаралды 24 М.
Static vs. Shared Libraries
11:00
Dave Xiang
Рет қаралды 65 М.
Makefiles: 95% of what you need to know
1:01:53
Gabriel Parmer
Рет қаралды 124 М.
Why Are Open Source Alternatives So Bad?
13:06
Eric Murphy
Рет қаралды 643 М.
Using Libraries in C++ (Static Linking)
18:43
The Cherno
Рет қаралды 440 М.
Header files and libraries (Kevin Lynch)
9:32
Northwestern Robotics
Рет қаралды 95 М.
10 ways Linux is just better!
11:32
Linus Tech Tips
Рет қаралды 4 МЛН