Skip to content

fix: CodeCoverage.cmake unconditionally links gcov, breaks macOS/Apple Clang #6349

@sublimator

Description

@sublimator

Problem

cmake/CodeCoverage.cmake in add_code_coverage_to_target unconditionally links gcov for both C and CXX targets:

target_link_libraries(
    ${name}
    ${scope}
    $<$<LINK_LANGUAGE:CXX>:${COVERAGE_CXX_LINKER_FLAGS}
    gcov>
    $<$<LINK_LANGUAGE:C>:${COVERAGE_C_LINKER_FLAGS}
    gcov>)

On macOS with Apple Clang, libgcov doesn't exist — Clang handles coverage instrumentation via --coverage without needing a separate library. This causes link failures when trying to build with -Dcoverage=ON on macOS.

Fix

Only link gcov when using GCC:

target_link_libraries(
    ${name}
    ${scope}
    $<$<LINK_LANGUAGE:CXX>:${COVERAGE_CXX_LINKER_FLAGS}>
    $<$<LINK_LANGUAGE:C>:${COVERAGE_C_LINKER_FLAGS}>)

# GCC requires explicit libgcov linkage; Clang/Apple Clang handle it via --coverage
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
    target_link_libraries(${name} ${scope} gcov)
endif ()

Also note the original generator expressions are malformed — gcov> is inside the $<$<LINK_LANGUAGE:...>:...> expression, meaning it's only linked conditionally on language, but the closing > placement makes it part of the expression value rather than a separate library argument.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions