# CMAKE\_SYSTEM\_NAME versus predefined variables

**URL:** <https://discourse.cmake.org/t/cmake-system-name-versus-predefined-variables/10311>\
**Category:** Code\
**Tags:** os:linux, os:windows, os:macos\
**Created:** [March 7, 2024, 5:41am UTC](https://discourse.cmake.org/t/cmake-system-name-versus-predefined-variables/10311 "2024-03-07T05:41:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saviz](https://discourse.cmake.org/user_avatar/discourse.cmake.org/saviz/32/6194_2.png) [@Saviz](https://discourse.cmake.org/u/Saviz)\
**Post date:** [March 7, 2024, 5:41am UTC](https://discourse.cmake.org/t/cmake-system-name-versus-predefined-variables/10311/1 "2024-03-07T05:41:22Z")

</div>

When working with CMake, it’s common to execute specific commands depending on the **platform** and **operating system** detected. During my research across various internet sources, I’ve encountered two commonly used snippets:

  

**Method 1:**

```auto
if(CMAKE_SYSTEM_NAME MATCHES "Windows")

	# WINDOWS
    
elseif(CMAKE_SYSTEM_NAME MATCHES "Darwin")
   
	# APPLE
   
elseif(CMAKE_SYSTEM_NAME MATCHES "Linux")

    # LINUX

endif()

```

  

**Method 2:**

```auto
if(WIN32)
  
  # WINDOWS

elseif(APPLE)
  
  # APPLE

elseif(LINUX)
  
  # LINUX

endif()

```

  

What is the difference between these two methods, and why is the first one mostly preferred over the second one?

---

<div class="post-metadata">

**Author:** ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)\
**Post date:** [March 7, 2024, 7:38am UTC](https://discourse.cmake.org/t/cmake-system-name-versus-predefined-variables/10311/2 "2024-03-07T07:38:43Z")

</div>

There are at least a couple of important differences:

- The `LINUX` variable is only defined with CMake 3.25 and later, whereas testing if `CMAKE_SYSTEM_NAME` matches `Linux` will work for all CMake versions.
- With CMake 3.14 and later, `CMAKE_SYSTEM_NAME` can be set to `iOS`, `tvOS`, or `watchOS`. CMake 3.28 also added support for the value `visionOS`. All of these will have the `APPLE` CMake variable set to true.
