Search code examples
linuxcompilationwgetldelf

How to quickly find the object files another object tile needs to be present at link time?


I have compiled the sources of wget, here is the ftp server https://ftp.gnu.org/gnu/wget/ to link my own program to one of the object files that I obtained after I compiled the project. But running nm -u on the desired file (to be specific src/http.o) gives me a whole lot of names that need to be resolved at link-time.

Question #1

Is there a tool to find which other object files are needed to be present for linker to resolve all the symbols? Manually testing every possible combination of object files does not even seem reasonable.

Question #2

When I try to link my program with every possible object file obtained from compiling the project I meet the following error - multiple definition. Does it imply that in general I need to select only a meaningful subset of the object files that I get after compiling some project and then building my executable with them?


Solution

  • Is there a tool to find which other object files are needed to be present for linker to resolve all the symbols?

    No. Constructing such a tool would not be difficult (you want to find connected components in the dependency graph), but the problem is not common.

    Manually testing every possible combination of object files does not even seem reasonable.

    It looks like wget consists of about 100 source files. Using all possible permutations, you would only have to try your link 100! times, which is indeed a bit too many combinations to try.

    As @kaylum commented, the developers didn't intend wget as a reusable library, so there is no guarantee that there is a solution even if you do try every possible combination.

    Also note that linking in wget sources imposes licence restrictions on your final program (you would have to release it under GPLv3).

    When I try to link my program with every possible object file obtained from compiling the project I meet the following error - multiple definition.

    That is expected: both your own program and wget/src/main.c define the main function.

    Does it imply that in general I need to select only a meaningful subset of the object files that I get after compiling some project and then building my executable with them?

    Yes. And in general there is no guarantee that a subset satisfying your requirements even exists.