There are 2 options:
Embed the version information into the name of the library, e.g. libmy.so.1.2, which should be soft-linked into libmy.so in your installation to be able to resolve it at run-time. Modify your build tools to accommodate this. This is a common approach, but not really convenient for build labels.
You can try to embed the version info into your library via some static string. On Unix you do not have resources with version information (as on Windows), so this is the only way to do it. Possible approach here can be:
a. Create a C/C+ source file (version.cxx) with something like this:
#include <version.h>
static char version_stamp[]="\n@(#) " PRODUCT " " VERSION " " BUILD_NUMBER;
(change format according your needs)
b. Create version.h where you define values of PRODUCT/VERSION/BUILD_NUMBER macros. You can generate/modify this header as a part of your build process or modify it manually. E.g.:
#define PRODUCT "MyProduct"
#define VERSION "1.2.1"
#define BUILD_NUMBER "241"
c. Make sure that your version C/C++ file (version.cxx) is linked into every library/executable in your product. Modify build tools as needed.
Then after you link your library you can use this command to get its version:
strings libmy.so | grep '@(#)'
If you have SCCS installed on your machine (some Unixes do) you can run:
what libmy.so
(that's why I used @(#) prefix above)
BTW same approach can be used on Windows: just include the header mentioned above into .rc file with version resources defined (and link that .rc into every binary in your product). As result you have one place where you define version and it shared on all platforms and all binaries.
Another benefit in (2) is that you may use the version macros from the header above in your run-time code when necessary (e.g. in log files).