英文:
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 "<CMAKE_LINKER> <OBJECTS> <LINK_FLAGS> <LINK_LIBRARIES> -o <TARGET>")
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 <-> 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 "<CMAKE_LINKER> <OBJECTS> <LINK_FLAGS> <LINK_LIBRARIES> -o <TARGET>")
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 及更早版本中无法实现。请查看以下代码:
这是一个链接到 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:
size_t cmComputeLinkDepends::ComputeComponentCount(NodeList const& nl)
{
size_t count = 2;
for (size_t ni : nl) {
if (cmGeneratorTarget const* target = this->EntryList[ni].Target) {
if (cmLinkInterface const* iface =
target->GetLinkInterface(this->Config, this->Target)) {
if (iface->Multiplicity > count) {
count = iface->Multiplicity;
}
}
}
}
return count;
}
Here is a link to the CMake issue tracker: https://gitlab.kitware.com/cmake/cmake/-/issues
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论