C++ : how to link against libA.so and not libA-X.Y

2019-05-25 02:02发布

问题:

I have a library A, that I develop. When I deploy it on a machine, the corresponding libA.so and libA-X.Y.Z.so are put in /usr/lib (X.Y.Z being the version number).

Now I develop a library B, which uses A. When I link B, I use the flag -lA. Then "ldd libB.so" gives me :

(...)
libA-X.Y.Z.so => /usr/lib/libA-X.Y.Z.so
(...)

My problem is that when I release a new version of A (X.Y.ZZ), I also have to release a new version of B. Otherwise, someone installing the latest A won't be able to install B which will be looking for the version X.Y.Z which doesn't exist.

How do I solve this problem ? How can I tell B to look for libA.so and not libA-X.Y.Z.so ? Or is it wrong to do so ? even unsafe ?

Update 1 : library A (that I inherited from someone else) uses autotools.

Update 2 : when I build library A, I can see : "-Wl,-soname -Wl,libA-0.6.1.so". If I understand properly that means that we are forcing the soname to be libA-0.6.1.so. Is that right ? Now my problem is that I have no clue how to modify this behaviour in a project which uses autotools. I googled for a while but can't find any useful information. Should I modify configure.in or a Makefile.am ?

回答1:

When you create libA.so, pass the -soname option to the linker (if you linking through gcc, use -Wl,-soname). Then, when B gets linked, the linker refers to A through its soname, not through its filename. On the target system, make sure you have a link from the soname to the real file. See

http://www.linux.org/docs/ldp/howto/Program-Library-HOWTO/shared-libraries.html



回答2:

This also works in Windows as "DLL hell" :).

If B needs a specific version of A and you would link to libA not libA-X.Y.Z then only substituting libA with newer version might cause B not to load or crash.

But of course you can do a symlink from libA-X.Y.Z to libA-X1.Y1.Z1. If no APIs changed and only implementations than you should be safe.



回答3:

Answering to my second update : In the Makefile.am of libA, I modified _la_LDFLAGS from -release to -avoid-version. This created a shared library without version number and I then recompiled libB which successfully linked against this unversioned shared library.