Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 687
Didier Trosset Avatar asked Oct 01 '10 14:10

Didier Trosset


People also ask

How do I know what version of so library I have?

Or from the command line, you can first search for the name of the associated package using dpkg -S /usr/lib/libnuma. so. 1 , which probably returns libnuma1 as the package name. Then run apt-cache showpkg libnuma1 to find the package version.

Which GCC option is used to link the library?

The -l option tells gcc to link in the specified library.

How are shared libraries linked?

Shared libraries (also called dynamic libraries) are linked into the program in two stages. First, during compile time, the linker verifies that all the symbols (again, functions, variables and the like) required by the program, are either linked into the program, or in one of its shared libraries.


1 Answers

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 
like image 106
Dmitry Yudakov Avatar answered Sep 20 '22 11:09

Dmitry Yudakov