# CMake Toolchain Files and \_GNU\_SOURCE definition

**URL:** <https://discourse.cmake.org/t/cmake-toolchain-files-and-gnu-source-definition/9411>\
**Category:** Code\
**Tags:** os:linux\
**Created:** [November 10, 2023, 8:46pm UTC](https://discourse.cmake.org/t/cmake-toolchain-files-and-gnu-source-definition/9411 "2023-11-10T20:46:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![axelsommerfeldt](https://discourse.cmake.org/user_avatar/discourse.cmake.org/axelsommerfeldt/32/4004_2.png) [@axelsommerfeldt](https://discourse.cmake.org/u/axelsommerfeldt)\
**Post date:** [November 10, 2023, 8:46pm UTC](https://discourse.cmake.org/t/cmake-toolchain-files-and-gnu-source-definition/9411/1 "2023-11-10T20:46:22Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jtxa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jtxa/32/1535_2.png) [@jtxa](https://discourse.cmake.org/u/jtxa)\
**Post date:** [November 13, 2023, 11:54pm UTC](https://discourse.cmake.org/t/cmake-toolchain-files-and-gnu-source-definition/9411/2 "2023-11-13T23:54:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![axelsommerfeldt](https://discourse.cmake.org/user_avatar/discourse.cmake.org/axelsommerfeldt/32/4004_2.png) [@axelsommerfeldt](https://discourse.cmake.org/u/axelsommerfeldt)\
**Post date:** [November 15, 2023, 9:54pm UTC](https://discourse.cmake.org/t/cmake-toolchain-files-and-gnu-source-definition/9411/3 "2023-11-15T21:54:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jtxa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jtxa/32/1535_2.png) [@jtxa](https://discourse.cmake.org/u/jtxa)\
**Post date:** [November 17, 2023, 3:15am UTC](https://discourse.cmake.org/t/cmake-toolchain-files-and-gnu-source-definition/9411/4 "2023-11-17T03:15:12Z")

</div>

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`.
