Once, in a post long, long ago, I reminded myself how to compile xkey, a lovely tool for snooping on open X servers, but not necessarily a standard part of every distribution. ;) It was easy enough last time:
But for various reasons, I needed to compile it statically. Being a novice programmer, I simply tagged a
onto the end of the command. However, that only resulted in various undefined reference errors to such functions as ‘XQueryTree’, ‘XSelectInput’, ‘XOpenDisplay’, and ‘XLookupString’…which means that the X11 libs/includes were not being referenced properly. Tossing a
in didn’t help things any. After googling around for a while, I was reminded that gcc can be very picky with respect to the order of its arguments. By placing the source filename before the lib arguments, I got a little bit closer. I was now only getting one error - undefined reference to `pthread_equal’.
Granted, I had no clue what that meant, but once again google to the rescue and all I needed to do was add a -lpthread (duh) to the libs. So the final command to make a static compilation of xkey.c is this.
gcc -g -v xkey.c -L/usr/X11R6/lib -lX11 -lXtst -lpthread -Wall -static -o xkey
Now I have a handy binary I can easily use on a system without the proper headers even if it is 30 times as large!