Context of the Problem
We are creating a DPDK package using CMake that exports targets with a namespace prefix (e.g., dpdk::libeal, dpdk::libmempool_ring). Some DPDK libraries (for example, mempool_ring and mempool_stack) require linking with the --whole-archive flag (or -Wl,--whole-archive), otherwise symbols from these libraries are not included in the final executable.
The Problem
We are trying to use the CMake generator expression $<LINK_LIBRARY:WHOLE_ARCHIVE,...>, but it doesn’t work properly for exported targets:
-
When using local target names (before export):
cmake
target_link_libraries(some_target INTERFACE "$<LINK_LIBRARY:WHOLE_ARCHIVE,libmempool_ring>" )After exporting the package, when targets receive the
dpdk::prefix, this expression starts interpretinglibmempool_ringas a library name (not a target) and tries to link with-llibmempool_ring. -
When using
$<TARGET_NAME:...>:cmake
target_link_libraries(some_target INTERFACE "$<LINK_LIBRARY:WHOLE_ARCHIVE,$<TARGET_NAME:libmempool_ring>>" )In this case, CMake correctly resolves the library path (the correct path to
librte_mempool_ring.a), but the--whole-archiveflags are not added. The$<TARGET_NAME:...>expression appears to return the path to the library file, not a reference to the target, which causes the loss of information about the need forWHOLE_ARCHIVElinking.
Expected Behavior
When linking, something like this should be generated:
text
-Wl,--whole-archive /path/to/librte_mempool_ring.a -Wl,--no-whole-archive
Actual Behavior
-
In the first case, an incorrect linker flag is generated:
-llibmempool_ring -
In the second case, only the library path is generated without the
--whole-archiveflags
Questions
-
How to properly use
$<LINK_LIBRARY:WHOLE_ARCHIVE,...>for targets that will be exported with a namespace prefix? -
Is there a workaround for this issue in current CMake versions (4.2.1)?
-
Is this a known issue/limitation in CMake?
CMake Version and Environment
-
CMake 4.2.1 (minimum version 3.30 specified in the project)
-
Compiler: GCC/G++
-
Linker: GNU ld or gold
-
OS: Linux