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

24,447

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
Share:
24,447
Didier Trosset
Author by

Didier Trosset

Didier Trosset has been a professional programmer for 25 years. He started his career with a couple of video games titles published on Mac and PlayStation. After an attempt to create his own start-up, he chooses a geographical relocation closer to his origins, near Geneva in the Alps. From this point in time, he has been working in Switzerland. He first wrote computer programs for a cable and satellite pay TV provider, controlling the data streams that were traveling back and forth from the Earth to televisions. He then joined a small company named Acqiris, writing Linux device drivers for High-Speed Digitizer PCI cards. Acqiris was bought out in late 2006 by Agilent Technologies, where he has continued spreading Linux support to other instruments. The Electronics Measurement Group of Agilent Technologies then became Keysight Technologies. In August 2017, the PCIe High-Speed Digitizer business spun off from Keysight, giving birth for the second time to Acqiris. Specialties: C++ programming, Python programming, Debian GNU/Linux, device drivers

Updated on October 02, 2020

Comments

  • Didier Trosset
    Didier Trosset over 3 years

    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?

  • Anonigan
    Anonigan almost 13 years
    Be careful: what linker uses, and what is resolved dynamicaly on runtime (what ldd app will show) might be not the same thing.