DEV Community

Albert Jokelin
Albert Jokelin

Posted on

Removing redundant libraries from Makefiles

It can be awfully frustrating when your binary is massive, you can’t strip your executable otherwise you may lose vital debugging information and the only way to figure out what libraries are and aren’t needed is by brute-forcing your way through.

There is an alternative way i discovered while working on my C++ program.

Theory: Assume we have two sets S1 and S2 defined as follows,

S1: Set of all libraries included in the makefile. 
S2: Set of all libraries used by the compiler to build the binary. 
Enter fullscreen mode Exit fullscreen mode

It is obvious that S1 is the complete domain (i.e. S1 = U) and n(S1) >= n(S2). Now we define a third set, S3 as follows,

S3: Set of all libraries to be removed from the makefile. 
Enter fullscreen mode Exit fullscreen mode

The way we obtain S3 is by using the following equation:

S3 = S1 – S2
   = (S1 ∩ S2)’ [The complement of the intersection of S1 and S2]
Enter fullscreen mode Exit fullscreen mode

Add this line to the end of your COMPILER variable in the makefile -Xlinker -Map=output.map. This flag creates a map of all the functions called while linking the binaries.

Now, to make your life easier you can either use the sed command to parse through output.map or you can search for individual libraries and remove the ones not called from your Makefile.

Top comments (0)