The order of the
-l flags matters when you are linking with static libraries. If you link using
-lfoo -lbar -lbaz, any symbols required by
libbar.a will only be searched for in
libbaz.a. Even if
libfoo.a contains the symbols you're after, the linker won't find them.
What is happening is that for each library, the linker unpacks the archive and adds the object files that contain symbols referenced by whatever came before. If a particular object file in the archive isn't needed, it is ignored.
The fix is to make sure every library is listed before any it depends on in the linker flags. If there are any dependency loops (which there shouldn't be), it may be necessary to list a library twice.