install shared imported library with necessary lin

2019-05-19 00:59发布

问题:

This question asks how to install a shared library with cmake which has been imported rather than being built by the current project:

Can I install shared imported library?

To repeat the issue:

add_library(libfoobar SHARED IMPORTED)
# this install command is illegal
install(TARGET libfoobar LIBRARY DESTINATION "${RPMBUILDROOT}${LIBDIR}")

This was raised as a [https://gitlab.kitware.com/cmake/cmake/issues/14311|issue] with cmake that has been closed, effectively with a resolution of will not fix. The grounds are, quite reasonably, that cmake does not know enough about an imported target to reliably install it.

One point the answer to that question misses is that install(TARGET) will automagically create links from libfoo.so to libfoo.so.major and libfoo.so.minor version on GNU/Linux and other unix-like platforms where this is required.

Is there a way to hijack cmake into treating a custom target as if it was built by the project or otherwise persuade it to create those links? Something like:

add_library(libfoobar SHARED IMPORTED)

#? add_custom_target(X libfoobar) 
install(TARGET X LIBRARY DESTINATION "${RPMBUILDROOT}${LIBDIR}")

What is a canonical way to do this?

回答1:

When a library is built by CMake, it is CMake who assigns soversion numbers to it (according to project's settings).

When a library isn't built by CMake, CMake doesn't know soversion, so it cannot create symlinks for you.

If you bother about that CMake actually installs symlink instead of file, resolve symlinks before installing, like in that question.


Well, you may ask CMake to guess soversion of the library (e.g. by resolving symlinks and checking their names). But why you ever need symlinks?

The main purpose of soversion symlink is to resolve compatibility issues with the future library's updates. But updates are possible only when the library is installed by the project who creates it.

If your project installs library produced by other project, it is unlikely that you want to support updates for the local library's installation. So there is no needs for you to support soversions.



标签: cmake