How to determine which object files are really needed for linking?

I had to modify the open source code for use in project C. Instead of creating a library from the modified code, I would just like to compile and create an executable from my own source in combination with the modified open source. The goal is to have a separate package that can be distributed. I can get this to work fine using the GNU build tools and successfully built my executable.

Now I would like to reduce the amount of code that I create and link. Is there an easy way to determine which of the open source files I really should compile? An open source package has, say, 40.c files. I assume that my code uses (or can be used) only 20 of these files. Currently, I collect them all and drop them all into the linker. There must be a smart (and easy?) Way to determine which ones I really need, right?

I am happy to provide additional information if this is helpful. Thanks in advance.

+5
source share
2 answers

, , , , , nm.

:

nm:

$ nm *.o

a.o:
00000000 T a
         U aa

b.o:
00000000 T b

t.o:
         U a
         U b
00000000 T main

ua.o:
00000000 T ua

ub.o:
00000000 T ub

, awk script

# find-unused.awk
BEGIN {req["main"]="crt"}

/\.o\:$/{
    gsub(/\:/,"");
    modulename=$0;
}

$1=="U"{
    req[$2] = modulename;
}

/[0-9,a-f].* T/{
    def[$3] = modulename;
}

END{
    print "modules referenced:"
    for (i in req)
    {
        if (def[i] != "")
            print "    "def[i];
    }

    print "functions not found"
    for (i in req)
    {
        if (def[i] == "")
            print "    "i;
    }
}

:

$ nm *.o|awk -f find-unused.awk

:

modules referenced:
    t.o
    a.o
    b.o
functions not found
    aa

- ua ub .

+3

, , , / . , . GNU linker -map . , , , .

40 , ?

+2

All Articles