Windows 開発における必須知識:CMake の WIN32_EXECUTABLE プロパティで GUI アプリを制覇
CMake の "Properties: Targets" における "WIN32_EXECUTABLE" プロパティは、Windows プラットフォームで GUI アプリケーションを作成する際に重要となります。このプロパティを設定することで、CMake に対してターゲットが GUI アプリケーションであることを指示し、適切なリンカオプションを設定することができます。
詳細
"WIN32_EXECUTABLE" プロパティは、ターゲットが GUI アプリケーションであるかどうかを指定するブール値です。デフォルト値は FALSE
であり、ターゲットはコンソールアプリケーションとして扱われます。
このプロパティを TRUE
に設定すると、CMake は以下の処理を行います。
WinMain()
エントリーポイントを使用するように設定します。- Windows 用のリンカオプションを設定します。
設定方法
"WIN32_EXECUTABLE" プロパティは、add_executable
コマンドを使用して設定することができます。以下の例では、myApp
という名前のターゲットを作成し、"WIN32_EXECUTABLE" プロパティを TRUE
に設定しています。
add_executable(myApp main.cpp other.cpp)
set_target_properties(myApp PROPERTIES WIN32_EXECUTABLE TRUE)
利点
"WIN32_EXECUTABLE" プロパティを設定することで、以下の利点が得られます。
- コード的可読性が向上します。
- ターゲットが GUI アプリケーションであることを明確に示すことができます。
- GUI アプリケーションに必要なリンカオプションが自動的に設定されます。
注意事項
"WIN32_EXECUTABLE" プロパティを設定する際には、以下の点に注意する必要があります。
- コンソールアプリケーションを作成する場合は、このプロパティを
FALSE
に設定する必要があります。 - GUI アプリケーションを作成する場合は、このプロパティを
TRUE
に設定する必要があります。 - このプロパティは Windows プラットフォームでのみ使用できます。
cmake_minimum_required(VERSION 3.10)
project(MyApp)
set(CMAKE_INCLUDE_DIRS "include")
add_executable(MyApp main.cpp other.cpp)
set_target_properties(MyApp PROPERTIES WIN32_EXECUTABLE TRUE)
target_link_libraries(MyApp ${CMAKE_THREAD_LIBS})
This code will create an executable named MyApp
from the source files main.cpp
and other.cpp
. The WIN32_EXECUTABLE
property will be set to TRUE
, which will tell CMake that this is a GUI application. The target_link_libraries()
command will link the MyApp
executable to the pthread
library, which is required for GUI applications on Windows.
Here is an example of the main.cpp
file:
#include <Windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
// Create the main window
HWND hWnd = CreateWindow(
"MyWindowClass",
"My Application",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
640,
480,
NULL,
NULL,
hInstance,
NULL);
if (!hWnd) {
return 1;
}
// Show the main window
ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);
// Run the message loop
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
This code will create a simple window with the title "My Application". The window will be 640 pixels wide and 480 pixels tall. The message loop will process messages from the window, such as mouse clicks and keyboard presses.
To build and run the application, you can use the following commands:
cmake .
cmake --build .
MyApp
Using the GUI_APP Property
CMake provides the
GUI_APP
property, specifically designed for identifying GUI applications. Setting this property toTRUE
instructs CMake to configure the target as a GUI application and apply the necessary settings.add_executable(MyApp main.cpp other.cpp) set_target_properties(MyApp PROPERTIES GUI_APP TRUE)
This approach is functionally equivalent to using
WIN32_EXECUTABLE
and explicitly setting theWIN32_EXECUTABLE
property.Utilizing the LINK_SUBSYSTEM Property
The
LINK_SUBSYSTEM
property allows you to specify the subsystem type for the linked executable. For GUI applications on Windows, set this property toWINDOWS
orCONSOLE
(withWIN32_EXECUTABLE
set toFALSE
).add_executable(MyApp main.cpp other.cpp) set_target_properties(MyApp PROPERTIES LINK_SUBSYSTEM WINDOWS)
This method explicitly defines the subsystem type, ensuring the executable is linked as a GUI application.
Leveraging the TARGET_TYPE Property
CMake's
TARGET_TYPE
property enables you to define the type of target being created. For GUI applications, set this property toGUI
instead of the defaultEXECUTABLE
.add_executable(MyApp main.cpp other.cpp GUI)
This approach explicitly declares the target as a GUI application, guiding CMake in the appropriate linking process.
Combining Properties
You can combine the
GUI_APP
andLINK_SUBSYSTEM
properties to achieve a more comprehensive definition of a GUI application.add_executable(MyApp main.cpp other.cpp) set_target_properties(MyApp PROPERTIES GUI_APP TRUE LINK_SUBSYSTEM WINDOWS)
This approach provides explicit control over both the GUI application identification and the subsystem type.