What object files did linker actually use? - Unix

This is a discussion on What object files did linker actually use? - Unix ; When I link my object files like this: g++ -o program obj1.o obj2.o obj3.o ... is there a way to find out (semi-)automatically what object files were actually needed and which ones I could have left out? Could the linker ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: What object files did linker actually use?

  1. What object files did linker actually use?

    When I link my object files like this:

    g++ -o program obj1.o obj2.o obj3.o ...

    is there a way to find out (semi-)automatically what object files were
    actually needed and which ones I could have left out? Could the linker
    map help, somehow?

    I have a largish project with 100 object files and getting this
    information would help me (re-)organize my source and object files
    into different libraries.

    Thanks!
    Markus


  2. Re: What object files did linker actually use?

    On Aug 22, 11:12 pm, Markus Dehmann wrote:
    > When I link my object files like this:
    >
    > g++ -o program obj1.o obj2.o obj3.o ...
    >
    > is there a way to find out (semi-)automatically what object files were
    > actually needed and which ones I could have left out? Could the linker
    > map help, somehow?
    >
    > I have a largish project with 100 object files and getting this
    > information would help me (re-)organize my source and object files
    > into different libraries.


    Here are two options that may be of use to you:

    If you pass "-Wl,--cref" to gcc, you'll get a cross-reference listing:
    for each symbol in the program, you will see in where it was defined
    and where it was referenced. From this you could look for symbols
    that are defined but never referenced by any other file.

    Another possibility is to combine your object files into a static
    library (archive) using ar(1), and take advantage of the behavior that
    when linking an archive, ld(1) only pulls in those archive members
    (object files) that are actually referenced. If you use the "-Wl,--
    trace" option to gcc, you'll see which members those are.

  3. Re: What object files did linker actually use?

    >When I link my object files like this:
    >
    >g++ -o program obj1.o obj2.o obj3.o ...
    >
    >is there a way to find out (semi-)automatically what object files were
    >actually needed and which ones I could have left out? Could the linker
    >map help, somehow?


    One way to do this is to put all of them (except maybe the main program)
    into a static library, link, and then see what gets included (you may
    link the system libraries dynamic if you wish). You might need "ranlib".

    Using gcc tools, at least on FreeBSD, objdump -t ${executable name}
    will list a lot of stuff, including things identified as "df *ABS*"
    with a symbol name ending in .c, .S, or .s, which are source file
    names. This might be done faster with:

    objdump -t ${executable name} | grep " df .ABS."

    >I have a largish project with 100 object files and getting this
    >information would help me (re-)organize my source and object files
    >into different libraries.


+ Reply to Thread