CMake Toolchain Files and _GNU_SOURCE definition

Usually CMake defines _GNU_SOURCE automatically but not when using a/my CMake Toolchain File. Is this a problem with my CMake Toolchain File, and if yes, how do I fix it? What exactly is the precondition that CMake defines _GNU_SOURCE?

Or should the project not assume that _GNU_SOURCE is defined when building on GNU/Linux, i.e. this should be fixed in the CMakeLists.txt of the project and not in the CMake Toolchain File?

The define _GNU_SOURCE is never added by CMake.

Or are you talking about compiling CMake itself. Then it’s defined in the CMakeLists.txt of cmlibuv.

I’m sorry, I should have posted some background. I am trying to build a project which needs _GNU_SOURCE to be defined but _GNU_SOURCE is not defined in its CMakeLists.txt. It compiles fine for AlmaLinux 8, but not for a custom Linux using a CMake Toolchain file. I was told that this is the fault of the CMake Toolchain file, so I assumed that CMake is defining _GNU_SOURCE on its own under certain circumstances, which is obviously not the case. (And since I could have found this out for myself easily I feel a little bit stupid right now.)

Thanks for pointing me in the right direction. I’ll try to find out what really the problem is and will report.

A CMake toolchain file is defined by the user/project and given to the CMake call via CMAKE_TOOLCHAIN_FILE variable. It’s not provided by CMake.

That file usually contains global compiler settings, depending on the compiler in use and the target system. You can add that define to the compiler flags, or I guess using add_compile_definitions.