10 LDFLAGS I Love
Hello and welcome to what will become the most sarcastic post on my blog. This is going to be a series of “buzzfeed” style programming articles and after this post I very happily pass the baton to Filippo Valsorda to continue. And I urge you to write your own as well.
@jessfraz "We asked Jess for her top 10 ldflags; you won't believe what happened next"
— adg (@enneff) July 17, 2016
So here they are:
-staticI would be an embarassment to myself if I didn’t start with the flag that tells the linker to not link against shared libraries. This is the best flag. STATIC BINARIES FTW.
--export-dynamicThis flag tells the linker to add all the symbols to the dynamic symbol table. This is especially important if you want to do “The Macgyver of Dlopening” and
dlopenyourself.--whole-archiveThis is another flag that comes in handy when you want to
dlopenyourself. See most linkers will only take into account the things it knows it needs. But with this flag, you tell it “YOLO, I want it all” so that later you candlopenyourself with that symbol that was never actually used until runtime. FUN!--no-whole-archiveThis flag un-sets the
--whole-archiveflag which is nice for when you only want the whole archive of one library but not all the others you are linking to.--print-mapThis flag is just dope. It prints a link map to stdout. This gives you information about object files, common symbols, and the values assigned to symbols.
--strip-allThis flag strips all the symbol information from the artifact produced. If say you are a few KB/MB off from your binary fitting on a floppy disk, this flag is your friend.
--strip-debugThis flag is very similar to
--strip-allexcept it only strips the debug symbol information. This all really depends on how much you need to shave off to fit that binary on a floppy disk.--traceThis flag is great for debugging. It prints the names of the input files as
ldprocesses them.-nostdlibThis flag forces the linker to only search the libraries you specify with
--library-pathor-L. This is nice when someone completely messes with your library path and the world is burning and you just want to link to those things you put in some random directory somewhere.--unresolved-symbols=ignore-allThis flag is helpful when telling the linker you DGAF about unresolved symbols and to stop yelling at you.