Creating and Linking Static Libraries on Linux with gcc

Поделиться
HTML-код
  • Опубликовано: 28 янв 2025

Комментарии • 64

  • @embeddedarmdev
    @embeddedarmdev  6 месяцев назад +4

    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 года назад +14

    Really great video. It shows a step by step example to let you know how to create your own one. Thanks!

  • @foxOneRides
    @foxOneRides Год назад +5

    made it very easy to understand. a hidden gem indeed.

  • @gattomiaomiao9634
    @gattomiaomiao9634 Год назад +2

    I love your way of teaching, theory plus practical demonstrations are great

  • @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 👌

  • @leninchakravarthy1426
    @leninchakravarthy1426 3 года назад +3

    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  3 года назад

      Thank you.

    • @stefanklaus6441
      @stefanklaus6441 2 года назад

      @@embeddedarmdev
      Do you have a udemy course?
      I'd definitely recommend it!

    • @embeddedarmdev
      @embeddedarmdev  2 года назад +6

      @@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.

  • @aFausify
    @aFausify 3 года назад +2

    Big thanks man. You're a gem. Great video!

  • @mateomerino5792
    @mateomerino5792 3 года назад +1

    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.

  • @WaveSound123
    @WaveSound123 3 года назад +1

    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!

  • @3ensy
    @3ensy 2 года назад +1

    So happy I found this vid thank you!

  • @multivalfran
    @multivalfran 2 года назад +1

    GREAT video. I'm trully amazed

  • @voidlt
    @voidlt 3 года назад +2

    Thank you so much for this video, it helped a lot.

  • @greydogyoutube
    @greydogyoutube 11 месяцев назад +1

    Finally! Thnaks a lot man!

  • @ahcenebelhadi955
    @ahcenebelhadi955 3 года назад +1

    Great video, the best explanation i found ! thanks a lot !!

  • @TheGokhansimsek35
    @TheGokhansimsek35 4 года назад +4

    Super useful. Thx a lot!!

  • @Random-sm5gi
    @Random-sm5gi 10 месяцев назад +1

    Very informative. Thanks boss.

  • @splattab
    @splattab 2 года назад +1

    This tutorial was very helpful thank you

  • @nikudayo7927
    @nikudayo7927 3 года назад +2

    Thanks! This is a really informative tutorial :D

  • @xelad3362
    @xelad3362 3 года назад +1

    Very informative! great work!

  • @akseven4u
    @akseven4u 3 года назад +1

    Thanks a lot for this video. I've found a surprising lack of content on C/C++ libraries

  • @MrCrazyDuck47
    @MrCrazyDuck47 2 года назад +1

    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.

  • @masterbeater8844
    @masterbeater8844 3 года назад +1

    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 ;)

  • @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🚀

  • @sulayenhanglimboo5110
    @sulayenhanglimboo5110 2 года назад +1

    Very helpful, thank you.

  • @nighttime9539
    @nighttime9539 Год назад +1

    Great tutorial.

  • @adampassaretti9149
    @adampassaretti9149 2 года назад +2

    thank you so much bro

  • @VaibhavSharma-zj4gk
    @VaibhavSharma-zj4gk 3 месяца назад

    10/10 video

  • @nilanjanmukhopadhyay8369
    @nilanjanmukhopadhyay8369 3 года назад +1

    Great video! Kudos.

    • @embeddedarmdev
      @embeddedarmdev  3 года назад

      Thanks!

    • @nilanjanmukhopadhyay8369
      @nilanjanmukhopadhyay8369 3 года назад

      @@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  3 года назад +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!

  • @zvxcvxcz
    @zvxcvxcz 2 года назад +3

    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.

  • @Nn-Tech
    @Nn-Tech 2 года назад +1

    Am getting an error after adding -L lib undefined reference to 'subNumbers'

    • @embeddedarmdev
      @embeddedarmdev  2 года назад +1

      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.

  • @madscientist338
    @madscientist338 2 года назад +1

    Impressive thanks!!!

  • @tomjoad6993
    @tomjoad6993 3 года назад

    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  3 года назад

      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.

  • @ratfuk9340
    @ratfuk9340 Год назад +1

    Thanks

  • @lihuazhang4637
    @lihuazhang4637 3 года назад +1

    Thanks for your video, Is it possible to create static library using shared library?

    • @embeddedarmdev
      @embeddedarmdev  3 года назад

      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 3 года назад

      @@embeddedarmdev Thanks for your quick reply.

  • @youchialiu2599
    @youchialiu2599 Год назад

    It really helps🥹🥹🥹🥹🥹

  • @Qwerty_945
    @Qwerty_945 6 месяцев назад

    Hey Sir ! what is -lmath ?

    • @embeddedarmdev
      @embeddedarmdev  6 месяцев назад

      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.

  • @cybernit3
    @cybernit3 7 месяцев назад +2

    Thanks embeddeddarmdev. I tested it out and worked fine.

  • @John223
    @John223 Год назад +1

    Very useful. Thank you ❤