【问题标题】:Configure CMake to follow symlink配置 CMake 以遵循符号链接
【发布时间】:2023-04-01 16:27:01
【问题描述】:

在 Linux 中,考虑有 test.so 文件。它在运行时需要其他 .so(例如 alpha.so、beta.so、charlie.so)文件。它基本上是一个共享库。当我在终端中运行以下命令时:

$ ldd test.so

显示以下输出:

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

我想修改用于构建 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

而不是将其链接到该库的特定版本(alpha.1.4.sobeta1.4.socharlie.1.4.so)。

如何修改我的 CMake 配置以使 test.so 文件遵循符号链接而不是特定版本?我基本上想让它版本独立。

【问题讨论】:

  • 感谢您的回答 Tysvarev。我想将 test.so 链接到 alpha.so、beta.so、charlie.so 文件。在安装的目录中包含 alpha.so -> alpha.so.1.4 (symlink) alpha.so.1.4 我想将 test.so 链接到指向 alpha.so (symlink) 而不是 alpha.so.1.4 以使其与版本无关,就像这个 test.so -> alpha.so -> alpha.so.1.4 但现在它像这个 test.so -> alpha.so.1.4

标签: linux makefile cmake symlink ldd


【解决方案1】:

@Tsyvarev 的解决方案适用于 SO_NAME 中不包含版本的动态库。

但在我的情况下,.so 文件的 SO_NAME 中包含版本号。尽管不建议在 SO_NAME 中使用版本号,但某些库会这样做。我在 linux 中使用 patchelf 命令修改了.so 文件。

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

这将更改库的 SO_NAME。

【讨论】:

    【解决方案2】:

    要禁用库链接中的 soname 部分,请设置 NO_SONAME 属性:

    set_target_properties(<library-target> PROPERTIES NO_SONAME OFF)
    

    您可以对项目中创建的每个库目标使用此命令,或者通过列出应应用它的所有目标来使用它一次:

    # 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)
    

    他们说有CMAKE_SHARED_LIBRARY_SONAME_C_FLAG 变量,它会影响所有目标。但似乎这个变量只在工具链脚本中起作用,而不是在它们之后。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-06
      • 2012-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多