如何告诉 CMake 在链接命令中不传递重复的库

huangapple go评论81阅读模式
英文:

How to tell CMake to pass no duplicate libraries in the link command

问题

I have a circular dependency between the two static libraries libA and libB:

add_library(LibA STATIC a.c)
add_library(LibB STATIC b.c)

# Circular dependency: LibA <-> LibB
target_link_libraries(LibA PRIVATE LibB)
target_link_libraries(LibB PRIVATE LibA)

add_executable(Example main.c)
target_link_libraries(Example PRIVATE LibA LibB)

When generating the build configuration CMake tries to resolve these dependencies by passing LibA and LibB twice each to the linker:

ilinkarm.exe --silent CMakeFiles\Example.dir\main.c.o  LibA.lib  LibB.lib  LibA.lib  LibB.lib -o Example.exe

However, the IAR linker I use already does multiple passes through the list of libraries, and it would be sufficient if LibA and LibB are only listed once. Linking with the duplications does still work, but I get the following warnings:

[build] Warning[Li065]: duplicate file: "LibA.lib"
[build] Warning[Li065]: duplicate file: "LibB.lib"

Is there a possibility to tell CMake that every library in the linker command has to be unique even with circular dependencies?

I tried setting CMAKE_C_LINK_EXECUTABLE, but I couldn't find an alternative to <LINK_LIBRARIES>, which already seems to contain the duplicates. That's why this line results in the same warning:

set(CMAKE_C_LINK_EXECUTABLE "&lt;CMAKE_LINKER&gt; &lt;OBJECTS&gt; &lt;LINK_FLAGS&gt; &lt;LINK_LIBRARIES&gt; -o &lt;TARGET&gt;")

CMake version: 3.26.3
Ninja version: 1.10.2
IAR version: 9.32.2

英文:

I have a circular dependency between the two static libraries libA and libB:

add_library(LibA STATIC a.c)
add_library(LibB STATIC b.c)

# Circular dependency: LibA &lt;-&gt; LibB
target_link_libraries(LibA PRIVATE LibB)
target_link_libraries(LibB PRIVATE LibA)

add_executable(Example main.c)
target_link_libraries(Example PRIVATE LibA LibB)

When generating the build configuration CMake tries to resolve these dependencies by passing LibA and LibB twice each to the linker:

ilinkarm.exe --silent CMakeFiles\Example.dir\main.c.o  LibA.lib  LibB.lib  LibA.lib  LibB.lib -o Example.exe

However the IAR linker I use already does multiple passes through the list of libraries and it would be sufficient if LibA and LibB are only listed once. Linking with the duplications does still work, but I get the following warnings:

[build] Warning[Li065]: duplicate file: &quot;LibA.lib&quot;
[build] Warning[Li065]: duplicate file: &quot;LibB.lib&quot;

Is there a possibility to tell CMake that every library in the linker command has to be unique even with circular dependencies?

I tried setting CMAKE_C_LINK_EXECUTABLE but I couldn't find an alternative to &lt;LINK_LIBRARIES&gt; which already seems to contain the duplicates. That's why this line results in the same warning:

set(CMAKE_C_LINK_EXECUTABLE &quot;&lt;CMAKE_LINKER&gt; &lt;OBJECTS&gt; &lt;LINK_FLAGS&gt; &lt;LINK_LIBRARIES&gt; -o &lt;TARGET&gt;&quot;)

CMake version: 3.26.3<br>
Ninja version: 1.10.2<br>
IAR version: 9.32.2

答案1

得分: 2

LINK_INTERFACE_MULTIPLICITY 属性通常用于控制此行为。然而,CMake 不会考虑此属性值低于 2,所以在 CMake 3.26 及更早版本中无法实现。请查看以下代码:

https://github.com/Kitware/CMake/blob/d9641980d2a223e0a6fe42ff23499e55f49fd6d5/Source/cmComputeLinkDepends.cxx#L1314-L1328

这是一个链接到 CMake 问题跟踪器的链接:https://gitlab.kitware.com/cmake/cmake/-/issues

英文:

Normally this is controlled by the LINK_INTERFACE_MULTIPLICITY property. However, CMake does not respect values of this property below 2, so this cannot be done as of CMake 3.26. See the code here:

https://github.com/Kitware/CMake/blob/d9641980d2a223e0a6fe42ff23499e55f49fd6d5/Source/cmComputeLinkDepends.cxx#L1314-L1328

size_t cmComputeLinkDepends::ComputeComponentCount(NodeList const&amp; nl)
{
  size_t count = 2;
  for (size_t ni : nl) {
    if (cmGeneratorTarget const* target = this-&gt;EntryList[ni].Target) {
      if (cmLinkInterface const* iface =
            target-&gt;GetLinkInterface(this-&gt;Config, this-&gt;Target)) {
        if (iface-&gt;Multiplicity &gt; count) {
          count = iface-&gt;Multiplicity;
        }
      }
    }
  }
  return count;
}

Here is a link to the CMake issue tracker: https://gitlab.kitware.com/cmake/cmake/-/issues

huangapple
  • 本文由 发表于 2023年5月17日 21:52:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272894.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定