# Creating a win32 window in VS '19 with cmake

**URL:** <https://discourse.cmake.org/t/creating-a-win32-window-in-vs-19-with-cmake/11552>\
**Category:** Code\
**Created:** [August 29, 2024, 10:23pm UTC](https://discourse.cmake.org/t/creating-a-win32-window-in-vs-19-with-cmake/11552 "2024-08-29T22:23:33Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![catsonmars](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@catsonmars](https://discourse.cmake.org/u/catsonmars)\
**Post date:** [August 29, 2024, 10:23pm UTC](https://discourse.cmake.org/t/creating-a-win32-window-in-vs-19-with-cmake/11552/1 "2024-08-29T22:23:33Z")

</div>

What I’ve tried. Just creating a win32 window in c++. Was pretty straightforward, the only configuring that needed to be done was project: properties → Linker -\>system → sub system → windows

Now for the next step, integrate a win32 window with cmake. Uh oh, no way to go into properties. So when I try to execute I get a popup window that says “please select a valid startup item”. Which only appears when I have the win32 code in the cpp file. If I use the default cpp file visual studio creates with the make file I get “hello cmake” in the debug window.

I tried one more additional thing. In CmakeLists.txt I added:  
add\_executable(win32\_wit\_cmake WIN32 win32\_wit\_cmake.cpp)

My question is this. How do I make it so visual studio knows how to build a win32 window with cmake.

---

<div class="post-metadata">

**Author:** ![vre](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/v/f04885/32.png) [@vre](https://discourse.cmake.org/u/vre)\
**Post date:** [August 30, 2024, 6:57am UTC](https://discourse.cmake.org/t/creating-a-win32-window-in-vs-19-with-cmake/11552/2 "2024-08-30T06:57:29Z")

</div>

You should add the following line after `add_executable` command to your `CMakeLists.txt` file:

`set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT win32_wit_cmake)`

---

<div class="post-metadata">

**Author:** ![catsonmars](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@catsonmars](https://discourse.cmake.org/u/catsonmars)\
**Post date:** [September 13, 2024, 4:29pm UTC](https://discourse.cmake.org/t/creating-a-win32-window-in-vs-19-with-cmake/11552/3 "2024-09-13T16:29:58Z")

</div>

Hi, thanks for your response. I get the following

CMake Error at C:\Users.… \CMakeLists.txt:10 (add\_executable): No SOURCES given to target: cmake32\_1

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [September 13, 2024, 5:12pm UTC](https://discourse.cmake.org/t/creating-a-win32-window-in-vs-19-with-cmake/11552/4 "2024-09-13T17:12:55Z")

</div>

In your original post the target name was `win32_wit_cmake`, but this error message implies that you (_also?_) have `cmake32_1` target, which you haven’t mentioned before. What other targets are you hiding from us? 🙂

---

<div class="post-metadata">

**Author:** ![catsonmars](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@catsonmars](https://discourse.cmake.org/u/catsonmars)\
**Post date:** [September 13, 2024, 5:23pm UTC](https://discourse.cmake.org/t/creating-a-win32-window-in-vs-19-with-cmake/11552/5 "2024-09-13T17:23:31Z")

</div>

sorry, I changed the name of the file. This should have posted like two weeks ago.

---

<div class="post-metadata">

**Author:** ![catsonmars](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@catsonmars](https://discourse.cmake.org/u/catsonmars)\
**Post date:** [September 13, 2024, 5:34pm UTC](https://discourse.cmake.org/t/creating-a-win32-window-in-vs-19-with-cmake/11552/6 "2024-09-13T17:34:56Z")

</div>

> [@vre](#):
>
> set\_property(DIRECTORY ${CMAKE\_CURRENT\_SOURCE\_DIR} PROPERTY VS\_STARTUP\_PROJECT win32\_wit\_cmake)

I just downloaded visual studio 22 and I started a new project

CMakeProject1

in my CMakeLists.txt I have

```auto
if (CMAKE_VERSION VERSION_GREATER 3.12)
  set_property(TARGET CMakeProject1 PROPERTY CXX_STANDARD 20)
  add_executable(CMakeProject1 WIN32 CMakeProject1.cpp)
  set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT CMakeProject1)

endif()

```

And when I try to build the project I get “Target missing executable”

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [September 13, 2024, 5:38pm UTC](https://discourse.cmake.org/t/creating-a-win32-window-in-vs-19-with-cmake/11552/7 "2024-09-13T17:38:51Z")

</div>

> ```auto
> add_executable(CMakeProject1
> 
> ```

That’s a yet another target name, the third one by now, in addition to `win32_wit_cmake` and `cmake32_1`.  
Perhaps you should share your entire project file.

---

<div class="post-metadata">

**Author:** ![catsonmars](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@catsonmars](https://discourse.cmake.org/u/catsonmars)\
**Post date:** [September 13, 2024, 5:43pm UTC](https://discourse.cmake.org/t/creating-a-win32-window-in-vs-19-with-cmake/11552/8 "2024-09-13T17:43:11Z")

</div>

Hi, I started over from scratch. It says new users can’t upload attachments. If I go back to the original file I can’t even build it properly because I can’t select a startup item.

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [September 13, 2024, 5:48pm UTC](https://discourse.cmake.org/t/creating-a-win32-window-in-vs-19-with-cmake/11552/9 "2024-09-13T17:48:32Z")

</div>

And you’ve never shared a text file over the internet?  
You can use a pastebin service, such as [this one](https://paste.mozilla.org), or create a public GitHub repository for your project.

---

<div class="post-metadata">

**Author:** ![catsonmars](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@catsonmars](https://discourse.cmake.org/u/catsonmars)\
**Post date:** [September 13, 2024, 5:51pm UTC](https://discourse.cmake.org/t/creating-a-win32-window-in-vs-19-with-cmake/11552/10 "2024-09-13T17:51:21Z")

</div>

Good idea. [GitHub - catsonmars/cmake\_win32: First attempt at integrating cmake in visual studio](https://github.com/catsonmars/cmake_win32)

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [September 13, 2024, 6:10pm UTC](https://discourse.cmake.org/t/creating-a-win32-window-in-vs-19-with-cmake/11552/11 "2024-09-13T18:10:48Z")

</div>

If this is not a nested project, then I’d say you are missing the [project()](https://cmake.org/cmake/help/latest/command/project.html) statement. And also the [cmake\_minimum\_required()](https://cmake.org/cmake/help/latest/command/cmake_minimum_required.html).  
Also I don’t understand what you are trying to achieve by creating the same target `CMakeProject1` twice (_as that CMake version check is very likely to pass_).

Anyway, I actually lost track a bit of what your problem was. So I just tried to build your project (_after fixing those problems_), and here’s the application that I got running:

 ![window](https://discourse.cmake.org/uploads/default/original/2X/b/b691290a7e19493f2083a3914bcdfe8efcab37c3.png)

Is that what you were after?

It builds and runs fine both from bare CLI and from Visual Studio.

---

<div class="post-metadata">

**Author:** ![catsonmars](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@catsonmars](https://discourse.cmake.org/u/catsonmars)\
**Post date:** [September 13, 2024, 7:18pm UTC](https://discourse.cmake.org/t/creating-a-win32-window-in-vs-19-with-cmake/11552/12 "2024-09-13T19:18:50Z")

</div>

I think the issue was the parameters in set\_property. It HAS to be this.

```auto

set_property(TARGET cm5 PROPERTY CXX_STANDARD 20)

```

is that what you had?

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [September 13, 2024, 7:24pm UTC](https://discourse.cmake.org/t/creating-a-win32-window-in-vs-19-with-cmake/11552/13 "2024-09-13T19:24:21Z")

</div>

No, I usually set C/CXX standards per project, not as a target property like you did, so I deleted that line from your original project. But actually it shouldn’t matter in this case, as your application builds fine both with and without it.

> `TARGET cm5`

That’s the fourth target by now )

---

<div class="post-metadata">

**Author:** ![catsonmars](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@catsonmars](https://discourse.cmake.org/u/catsonmars)\
**Post date:** [September 13, 2024, 7:28pm UTC](https://discourse.cmake.org/t/creating-a-win32-window-in-vs-19-with-cmake/11552/14 "2024-09-13T19:28:39Z")

</div>

Correct. That is me seeing if I could reproduce an executable file. Original here

> <https://github.com/catsonmars/cmake_win32/blob/main/CMakeLists.txt>
