# How to include "WIN32" in an "add\_executable()" command only on Windows?

**URL:** https://discourse.cmake.org/t/how-to-include-win32-in-an-add-executable-command-only-on-windows/13852
**Category:** Code
**Created:** [March 29, 2025, 8:17pm UTC](https://discourse.cmake.org/t/how-to-include-win32-in-an-add-executable-command-only-on-windows/13852 "2025-03-29T20:17:17Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![fishnet37222](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fishnet37222/32/5457_2.png) [@fishnet37222](https://discourse.cmake.org/u/fishnet37222)
#### Post date: [March 29, 2025, 8:17pm UTC](https://discourse.cmake.org/t/how-to-include-win32-in-an-add-executable-command-only-on-windows/13852/1 "2025-03-29T20:17:17Z")

</div>

I currently have the following block of code in my CMakeLists.txt file. What I’d like to be able to do is have just one “add\_executable()” command and still have the “WIN32” keyword included only on Windows.

```auto
if(${WIN32})
	add_executable(minesweeper WIN32 minesweeper.rc ${SOURCE_FILES})
elseif(${LINUX})
	add_executable(minesweeper ${SOURCE_FILES})
endif()

```

I tried using a generator expression, but got an error about CMake being unable to find a source file named “WIN32”. Is there any way I can replace the two “add\_executable()” commands with just 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 29, 2025, 9:04pm UTC](https://discourse.cmake.org/t/how-to-include-win32-in-an-add-executable-command-only-on-windows/13852/2 "2025-03-29T21:04:07Z")

</div>

There’s a few ways you could tackle this. Firstly, note that you can put WIN32 in the `add_executable()` call and it will be ignored by platforms other than Windows, so no need to make adding that conditional. That leaves the `minesweeper.rc` file. You could use a generator expression like so:

```cmake
add_executable(minesweeper WIN32
    $<$<BOOL:${WIN32}>:minesweeper.rc>
    ${SOURCE_FILES}
)

```

The `$<BOOL:${WIN32}>` is needed because WIN32 might not be defined, and the `$<expression:...>` form requires the `expression` to evaluate to 1 or 0. If WIN32 isn’t defined, `${WIN32}` would evaluate to an empty string, which fails that requirement. Wrapping it in `$<BOOL:...>` ensures the result is 1 or 0.

However, it’s probably simpler to avoid the generator expression altogether. Here’s how I would implement it:

```cmake
add_executable(minesweeper WIN32 ${SOURCE_FILES})
if(WIN32)
    target_sources(minesweeper PRIVATE minesweeper.rc)
endif()

```

As a final note, you haven’t shown how you’re populating the `SOURCE_FILES` variable. A common pattern used in tutorials is to populate such a variable using `file(GLOB)`. Don’t do that, the docs make clear that’s not a suitable pattern. Explicitly list out each file, and you can do that directly in the call to `add_executable()`. You don’t gain anything by putting such a list in a `SOURCE_FILES` variable.

---

<div class="post-metadata">

### Author: ![fishnet37222](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fishnet37222/32/5457_2.png) [@fishnet37222](https://discourse.cmake.org/u/fishnet37222)
#### Post date: [March 29, 2025, 9:12pm UTC](https://discourse.cmake.org/t/how-to-include-win32-in-an-add-executable-command-only-on-windows/13852/3 "2025-03-29T21:12:13Z")

</div>

I was populating the `SOURCE_FILES` variable by listing out each file. I put it in a variable so I could include the same set of files in each `add_executable()` command. The information you provided enabled me to get rid of that variable, though.
