Skip to content

Commit 72844ac

Browse files
committed
cmake: align check_symbol_exists with build flags
check_symbol_exists() runs an independent try_compile with no knowledge of target properties. With GCC 15 defaulting to C23, strndup was found during the check but then hidden at build time by __STRICT_ANSI__ (set by -std=c99 / C_STANDARD 99), causing an implicit-declaration error. Fix this by setting CMAKE_REQUIRED_FLAGS and CMAKE_REQUIRED_DEFINITIONS to match the flags the iio target actually compiles with, saving and restoring their previous values around the check block. Introduce IIO_C_STANDARD and IIO_POSIX_C_SOURCE as the single source of truth so that set_target_properties, target_compile_definitions and the check block all stay in sync from one place. Signed-off-by: Dan Nechita <dan.nechita@analog.com>
1 parent 930f30c commit 72844ac

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

CMakeLists.txt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,11 @@ set_target_properties(iio PROPERTIES
120120
SOVERSION ${PROJECT_VERSION_MAJOR}
121121
PUBLIC_HEADER "${IIO_PUBLIC_HEADERS}"
122122
)
123+
set(IIO_C_STANDARD 99)
124+
set(IIO_POSIX_C_SOURCE 200809L)
123125
set_target_properties(iio ${IIO_COMPAT_LIB} PROPERTIES
124126
FRAMEWORK ${OSX_FRAMEWORK}
125-
C_STANDARD 99
127+
C_STANDARD ${IIO_C_STANDARD}
126128
C_STANDARD_REQUIRED ON
127129
C_EXTENSIONS OFF
128130
)
@@ -146,7 +148,7 @@ target_compile_definitions(iio PRIVATE LIBIIO_EXPORTS)
146148

147149
target_compile_definitions(iio PUBLIC
148150
# Request Posix 2008.09
149-
_POSIX_C_SOURCE=200809L __XSI_VISIBLE=500
151+
_POSIX_C_SOURCE=${IIO_POSIX_C_SOURCE} __XSI_VISIBLE=500
150152

151153
# Per http://www.mingw.org/wiki/Use_more_recent_defined_functions
152154
$<$<PLATFORM_ID:MinGW>:_WIN32_WINNT=0x600 WINVER=0x600>
@@ -259,12 +261,25 @@ endif()
259261
target_link_libraries(iio PRIVATE iio_common_config)
260262

261263
include(CheckSymbolExists)
264+
# Run the checks with the same flags used during the actual build, so that
265+
# the probe compilation matches what the compiler will see (e.g. -std=c99
266+
# sets __STRICT_ANSI__ in GCC, which can hide POSIX extensions in MinGW
267+
# headers). IIO_C_STANDARD and IIO_POSIX_C_SOURCE are the single source of
268+
# truth; changing them above automatically updates the checks below.
269+
set(_saved_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
270+
set(_saved_CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS}")
271+
list(APPEND CMAKE_REQUIRED_FLAGS "-std=c${IIO_C_STANDARD}")
272+
list(APPEND CMAKE_REQUIRED_DEFINITIONS "-D_POSIX_C_SOURCE=${IIO_POSIX_C_SOURCE}" "-D__XSI_VISIBLE=500")
262273
check_symbol_exists(strdup "string.h" HAS_STRDUP)
263274
check_symbol_exists(strndup "string.h" HAS_STRNDUP)
264275
check_symbol_exists(strerror_r "string.h" HAS_STRERROR_R)
265276
check_symbol_exists(strtok_r "string.h" HAS_STRTOK_R)
266277
check_symbol_exists(newlocale "locale.h" HAS_NEWLOCALE)
267278
check_symbol_exists(clock_gettime "time.h" HAS_CLOCK_GETTIME)
279+
set(CMAKE_REQUIRED_FLAGS "${_saved_CMAKE_REQUIRED_FLAGS}")
280+
set(CMAKE_REQUIRED_DEFINITIONS "${_saved_CMAKE_REQUIRED_DEFINITIONS}")
281+
unset(_saved_CMAKE_REQUIRED_FLAGS)
282+
unset(_saved_CMAKE_REQUIRED_DEFINITIONS)
268283

269284
# For EAI_* codes
270285
include(CheckIncludeFile)

0 commit comments

Comments
 (0)