Skip to content

Commit d5558e3

Browse files
authored
Fix compiler errors from check third-party dependency on Windows (#188)
# Issue The check unit testing framework (used as a third-party dependency) emits compiler diagnostics that break the build on Windows platforms, even though it's configured as an external dependency with `SYSTEM_INCLUDES`. Specific error: ``` D:\a\libsbp\libsbp\c\third_party\check\lib\timer_delete.c:48:5: error: implicit declaration of function 'alarm' [-Wimplicit-function-declaration] ``` # Root Cause The check library has platform compatibility issues on Windows: 1. Missing POSIX functions: Windows lacks POSIX functions like alarm(), setitimer(), and proper timer support 2. Incomplete fallback detection: While check includes compatibility shims (e.g., alarm.c), the file timer_delete.c calls alarm() directly without proper header inclusion or declaration checking 3. SYSTEM_INCLUDES limitation: The SYSTEM_INCLUDES flag in CMake only suppresses warnings from headers consumed via target_include_directories(). However, check adds its compatibility source files (including timer_delete.c) directly to the target via target_sources(), so these files are compiled as part of the target itself and inherit all parent compile flags including -Werror Note: The check project has been unmaintained for over 4 years (last release: 0.15.2 in 2020), making an upstream fix unlikely. # Solution Modified cmake/common/FindCheck.cmake to temporarily adjust CMAKE_C_FLAGS during the check library's configuration and compilation: 1. Save original flags before including the dependency 2. Apply platform-specific suppressions: - MinGW/GCC: Remove -Werror and add -Wno-error=implicit-function-declaration - MSVC: Remove /WX and add /wd4013 (disables C4013: implicit function declaration) 3. Restore original flags immediately after the dependency is added This approach: - ✅ Doesn't modify third-party code - ✅ Only affects check library compilation - ✅ Preserves strict warnings for project code - ✅ Supports both MinGW and MSVC toolchains
1 parent 1ab487a commit d5558e3

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

FindCheck.cmake

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,29 @@
1313
include("GenericFindDependency")
1414
option(CHECK_ENABLE_TESTS "" OFF)
1515
option(CHECK_INSTALL "" OFF)
16+
17+
# Store current compile options to restore later
18+
set(_SAVED_CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
19+
20+
# Temporarily modify CMAKE_C_FLAGS to disable implicit-function-declaration errors
21+
# for the check library compilation (needed for Windows compatibility)
22+
if(WIN32)
23+
if(MSVC)
24+
# MSVC: Remove warnings-as-errors flag and disable specific warning C4013 (implicit function declaration)
25+
string(REPLACE "/WX" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
26+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4013")
27+
else()
28+
# MinGW/GCC: Remove -Werror and disable implicit-function-declaration errors
29+
string(REPLACE "-Werror" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
30+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-error=implicit-function-declaration")
31+
endif()
32+
endif()
33+
1634
GenericFindDependency(
1735
TARGET check
1836
SYSTEM_INCLUDES
1937
)
38+
39+
# Restore original compile flags
40+
set(CMAKE_C_FLAGS "${_SAVED_CMAKE_C_FLAGS}")
41+
unset(_SAVED_CMAKE_C_FLAGS)

0 commit comments

Comments
 (0)