问题
at times if we don't list libs in certain order inside a makefile, it fails.
The reason being - definition should come before its use.
How to determine the correct order?
回答1:
Actually, when linking libraries, the use should come before the definition. Any unresolved symbols need to be known before a library file providing their definitions is processed.
What comes to the order, I'm afraid you have to do this manually. If libA depends on libB (i.e. libA uses symbols from libB), then you have to link in this order: -lA -lB
.
This is mostly a matter of documentation. A well-documented library clearly states what other libraries it depends on, so you can figure out the correct linking order.
If you don't want to read documentation or there is no documentation available, trial and error is always an option :)
回答2:
I found this to be annoying too. I remember searching for a tool to make the sorting for me, but didn't find anything to help.
Finally I decided to use the brute force approach: If you will list all your libs twice, e.g. -lA -lB -lA -lB
, it will guarantee that every library is listed before (and after) every other library.
So given a $(LIST)
of libraries, you can use $(addprefix -l,$(LIST) $(LIST))
.
Not very elegant, and slows the linking stage somewhat, but works.
来源:https://stackoverflow.com/questions/5631316/lib-dependencies-and-their-order