• Re: [OT] Linking against a static library

    From pozz@3:633/10 to All on Wed Sep 3 13:02:12 2025
    Il 02/09/2025 01:16, Michael S ha scritto:
    On Tue, 2 Sep 2025 02:02:20 +0300
    Michael S <already5chosen@yahoo.com> wrote:

    On Thu, 28 Aug 2025 18:12:52 +0200
    pozz <pozzugno@gmail.com> wrote:

    I don't think this is a pure C programming question, but it's
    related.

    I'm building a static library mylib.a with all the object files
    except main.o, and the final executable linking together main.o and
    mylib.a.

    I want to remove from the final exe everything present in mylib.a
    that is not used in main.o.

    Suppose only mod1.o is present in mylib.a. foo1() and bar1() are
    defined in mod1.c. main() is the only function in main.c and only
    foo1() is called from main().

    gcc -O2 -ffunction-sections -fdata-sections -c -o mod1.o mod1.c
    ar rcs mylib.a mod1.o
    gcc -O2 -ffunction-sections -fdata-sections -c -o main.o main.c
    gcc -Wl,--gc-sections,--print-gc-sections -o main[.exe] main.o
    mylib.a objdump -d main[.exe] | grep bar1

    MinGW in Windows build a main.exe that contains bar1(), while gcc in
    WSL doesn't.

    Why?

    I investigated it a little more and found two things:
    1. The problem caused by gcc compiler rather than by gnu ld
    linker. If you link MSVC-generated object files with gnu ld then
    "garbage collection" works as expected.
    2. The problem is related to gcc implementation of SEH.
    Compile to asm, edit asm removing SEH directives then assemble to .o
    and it works as expected.

    Now in order to continue from here I'd have to know much more
    about SEH than I actually do. So, I stopped.





    I still don't know much about SEH, but now, due to Stack Overflow, I
    know how to get rid of it.
    Just add -fno-asynchronous-unwind-tables to your compilation options
    and suddenly 'garbage collection' works.
    Of course, it helps only if you don't care about SEH. But, considering
    that what you are doing are mere unit tests, I don't see why would you possibly care about it.

    Thank you Michael, it works!


    === Synchronet 3.21a-Linux NewsLink 1.2
    --- SBBSecho 3.29-Linux
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/10)