Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake - Using POSIX feature test macros?

CMake has a set of handy properties that correspond to setting certain feature test macros in the underlying compiler. For example, with gcc:

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)

will result in the following flags being passed:

-std=c11 -Wpedantic

which will also set the feature test macros

_ISOC11_SOURCE

This works quite well as I can rely on CMake to deal with the differences between compilers. I was looking through the list of feature test macros that CMakes supports, however, and I couldn't locate any related to the _POSIX_C_SOURCE feature test. Is there any better way to do this other than add_definitions(-D_POSIX_C_SOURCE=200809L)?

like image 627
Huckle Avatar asked Jun 01 '26 22:06

Huckle


1 Answers

tl;dr: As of CMake 4.2, there is no feature test nor CMake variable regarding POSIX support.

Longer answer:

I am not a Kitware developer, but I believe the answer is negative. If you search the CMake documentation you will not find pages with that phrase in the title; and there is no CMAKE_ variable with POSIX in its name.

That does not mean that CMake should not have some facility of this kind; CMake is far from complete and perfect w.r.t. what it offers.

Now _POSIX_C_SOURCE tells your C code that some library functions are available beyond what the standard library offers. It seems to be the facility should therefore be one of the following:

  1. A find_package() for POSIX, which, if found, will have an INTERFACE_COMPILE_DEFINITIONS property which include _POSIX_C_SOURCE, and possible additional libraries to include, which contain POSIX functions.
  2. CMake may choose to think of the POSIX functions as somehow part of the C standard library, in which case it would add a CMAKE_ variable like you suggested, e.g. CMAKE_C_POSIX_AVAILABLE or whatever, that also causes _POSIX_C_SOURCE to be defined when compiling. This could tie in with being generally more forthcoming regarding the standard library (bug 26672; I opened this last year

(others might have other ideas.)

I've filed CMake bug 27634 about this matter.

like image 154
einpoklum Avatar answered Jun 05 '26 00:06

einpoklum