Creating a win32 window in VS '19 with cmake

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.

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)

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

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? :slight_smile:

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

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

CMakeProject1

in my CMakeLists.txt I have

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”

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.

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.

And you’ve never shared a text file over the internet?
You can use a pastebin service, such as this one, or create a public GitHub repository for your project.

Good idea. GitHub - catsonmars/cmake_win32: First attempt at integrating cmake in visual studio

If this is not a nested project, then I’d say you are missing the project() statement. And also the cmake_minimum_required().
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:

Is that what you were after?

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

I think the issue was the parameters in set_property. It HAS to be this.


set_property(TARGET cm5 PROPERTY CXX_STANDARD 20)

is that what you had?

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 )

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