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.
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
@@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.
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!
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 ;)
@@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!
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!
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.
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?
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
@@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.
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?
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.
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.
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.
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.
Really great video. It shows a step by step example to let you know how to create your own one. Thanks!
made it very easy to understand. a hidden gem indeed.
I love your way of teaching, theory plus practical demonstrations are great
tracing every error that appears to you and finding out its cause for the first person to deal with these programs
Very great thing 👌
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
Thank you.
@@embeddedarmdev
Do you have a udemy course?
I'd definitely recommend it!
@@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.
@@embeddedarmdev youtube tutorials is a wonder of modern age. Thanks to authors like you.
Big thanks man. You're a gem. Great video!
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.
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!
Thank you. I'm glad you fund it useful.
So happy I found this vid thank you!
I'm glad you found it too. Thanks.
GREAT video. I'm trully amazed
Thank you so much for this video, it helped a lot.
Finally! Thnaks a lot man!
Great video, the best explanation i found ! thanks a lot !!
Thank you.
Super useful. Thx a lot!!
Very informative. Thanks boss.
This tutorial was very helpful thank you
Thanks! This is a really informative tutorial :D
Thank you!
Very informative! great work!
Thanks a lot for this video. I've found a surprising lack of content on C/C++ libraries
Thanks, very well explained. It would be also nice, if compilation with Makefile would be explained as well
That's a good idea for a topic. I will add it to my list.
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 ;)
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🚀
Very helpful, thank you.
Great tutorial.
Thank you!
thank you so much bro
10/10 video
Great video! Kudos.
Thanks!
@@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!
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!
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.
What a bunch of random info. It's good to know but I dont see how it relates to the video.
Am getting an error after adding -L lib undefined reference to 'subNumbers'
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?
Thanks , I solved it
how do you solve it? I'm getting the same error
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
@@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.
Impressive thanks!!!
Thank you! I appreciate it.
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?
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.
Thanks
Thanks for your video, Is it possible to create static library using shared library?
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.
@@embeddedarmdev Thanks for your quick reply.
It really helps🥹🥹🥹🥹🥹
Hey Sir ! what is -lmath ?
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.
Thanks embeddeddarmdev. I tested it out and worked fine.
Very useful. Thank you ❤