How to specify the library version to use at link time?

随声附和 提交于 2019-11-26 07:35:08

问题


Following question How do applications resolve to different versions of shared libraries at run time?, I wondered how to specify on the link command line which version of the library to use?

Let\'s say I have

libmy.so.1.0
libmy.so.1    -> libmy.so.1.0
libmy.so.2.0
libmy.so.2    -> libmy.so.2.0
libmy.so      -> libmy.so.2

The usual way to specify the library to link with the executable does not show the version to use. Furthermore, it is very likely that one wants to link with the most recent version. Thus the usual line works fine in most cases.

gcc app.o -lmy -o app

What is the command line to link app that should use version 1 of the library?


回答1:


The linker is able to accept filenames too

gcc  app.o -l:libmy.so.1 -o app

From man ld:

-l namespec
--library=namespec
Add the archive or object file specified by namespec to the list of files to link. This option may be used any number of times. If namespec is of the form :filename, ld will search the library path for a file called filename, otherwise it will search the library path for a file called libnamespec.a.

I noticed that older versions do not support it, so check man ld -l or --library option on your system.

You could also link to the file mentioning its full name

gcc  app.o /mylibpath/libmy.so.1 -o app


来源:https://stackoverflow.com/questions/3840218/how-to-specify-the-library-version-to-use-at-link-time

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