简体   繁体   English

配置CMake以遵循符号链接

[英]Configure CMake to follow symlink

In Linux, Consider there is test.so file. 在Linux中,请考虑有test.so文件。 It requires other .so (for example alpha.so, beta.so, charlie.so)file during runtime. 在运行时,它需要其他.so(例如alpha.so,beta.so,charlie.so)文件。 Its basically a shared library. 它基本上是一个共享库。 When I run the following command in the terminal: 当我在终端中运行以下命令时:

$ ldd test.so

following output was displayed: 显示以下输出:

alpha.1.4.so ==> usr/lib/x86-linux-gnu/alpha.1.4.so
beta.1.4.so ==> usr/lib/x86-linux-gnu/beta.1.4.so
charlie.1.4.so ==> usr/lib/x86-linux-gnu/charlie.1.4.so

I want to modify the cmake which is used for building the test.so to point to symlink, like this 我想修改用于构建test.so的cmake,以指向符号链接,像这样

alpha.so ==> usr/lib/x86-linux-gnu/alpha.1.4.so
beta.so ==> usr/lib/x86-linux-gnu/beta.1.4.so
charlie.so ==> usr/lib/x86-linux-gnu/charlie.1.4.so

rather than linking it to the specific version of that library ( alpha.1.4.so , beta1.4.so , charlie.1.4.so ). 而不是将其链接到该库的特定版本( alpha.1.4.sobeta1.4.socharlie.1.4.so )。

How can I modify my CMake configuration to make the test.so file to follow symlink rather than the specific version? 如何修改CMake配置以使test.so文件遵循符号链接而不是特定版本? I basically want to make it version independent. 我基本上想使其版本独立。

For disable soname part in the library linking, set NO_SONAME property: 要在库链接中禁用soname部分,请设置NO_SONAME属性:

set_target_properties(<library-target> PROPERTIES NO_SONAME OFF)

You may use this command for each library target, created in your project, or use it once by listing all targets to which it should be applied: 您可以对在您的项目中创建的每个库目标使用此命令,也可以通过列出应对其应用的所有目标来使用一次此命令:

# Collect all library targets somehow
set(LIBRARY_TARGETS <library-target1> <library-target2> ...)
# And set the property for all of them
set_target_properties(${LIBRARY_TARGETS} PROPERTIES NO_SONAME OFF)

They said there is CMAKE_SHARED_LIBRARY_SONAME_C_FLAG variable, which affects on all targets. 他们说有CMAKE_SHARED_LIBRARY_SONAME_C_FLAG变量,该变量会影响所有目标。 But it seems that this variable works only in toolchain scripts, not after them. 但是似乎该变量仅在工具链脚本中起作用,而在它们之后不起作用。

The solution by @Tsyvarev will fit for dynamic library whose SO_NAME do not contains version in it. @Tsyvarev的解决方案适用于SO_NAME中不包含版本的动态库。

But in my case the SO_NAME of the .so file contains the version number in it. 但是在我的情况下, .so文件的SO_NAME中包含版本号。 Though it is not recommended to have a version number in SO_NAME some library does it. 尽管不建议在SO_NAME中使用版本号,但还是可以使用某些库。 I have modified the .so file using patchelf command in linux. 我已经在Linux中使用patchelf命令修改了.so文件。

patchelf --set-soname dynamic_lib.so dynamic_lib.so.version_number

This will change the SO_NAME of the library. 这将更改库的SO_NAME。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM