I had to modify some open source code to use in a C project. Instead of building a library from the modified code, I’d like to just compile and build an executable from my own source combined with the modified open source code. The goal is to have a stand-alone package that can be distributed. I can get this to work just fine using the GNU build tools and have successfully built my executable.
Now I’d like to pare down the amount of code I am building and linking. Is there an easy way to determine which of the open source files I actually need to compile? There are, say, 40 .c files in the open source package. I’m guessing my code only uses (or causes to be used) 20-ish of those files. Currently I’m compiling all of them and throwing everything at the linker. There has to be a smart (and easy?) way to determine which ones I actually need, right?
I’m happy to provide further details if it’s helpful. Thanks in advance.
When faced with this I’ve either simply taken the final link command stripped out all of the objects and then added back in until it works, or processed the output of the nm command.
Worked example:
Looking at the output of nm:
So I create the following awk script
and then call it like this;
it tells me:
Which is right – because the ua & ub functions in the above example aren’t used.