lib dependencies and their order

℡╲_俬逩灬. 提交于 2019-12-24 04:55:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!