CMake创建MFC桌面应用
安装
- (非必须)VC_redist.x86和VC_redist.x64:https://learn.microsoft.com/en-us/cpp/windows/latest-supported-vc-redist?view=msvc-170
- (必须)使用Visual Studio生成工具安装"C++桌面开发 "(所有默认选中的,不要删):https://visualstudio.microsoft.com/zh-hans/visual-cpp-build-tools/
- 或者安装 Visual Studio Community 时,选"C++桌面开发"
- 选了"C++桌面开发"后,记得在右边选上 MFC
- (非必须)使用 CLion 开发(或者使用其他编辑器,自己在CMake指定生成器)
- 配置默认ToolChain为 Visual Studio,CLion会自动从已安装的Visual Studio中读取
项目代码
- CMakeLists.txt (关键)
cmake
cmake_minimum_required(VERSION 3.0)
project(mfc_demo)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8")
# 1: static MFC library, 2: shared MFC library
set(CMAKE_MFC_FLAG 2)
#find_package(MFC)
#if(NOT MFC_FOUND)
# message(FATAL_ERROR "MFC is not found, please check MFC installation")
#endif()
add_executable(mfc_demo mfc.cpp)
#target_sources(mfc_demo PRIVATE
# mfc.cpp
# )
#set_target_properties(mfc_demo PROPERTIES LINK_FLAGS "/SUBSYSTEM:WINDOWS")
target_compile_definitions(mfc_demo PRIVATE
-DWIN32
-D_DEBUG
-D_WINDOWS
-D_UNICODE
-DUNICODE
-D_AFXDLL
)
target_link_options(mfc_demo PRIVATE
/ENTRY:wWinMainCRTStartup
)
- mfc.h
cpp
#ifndef MFC_DEMO_MFC_H
#define MFC_DEMO_MFC_H
#include <afxwin.h>
class MyApp:public CWinApp {
public:
virtual BOOL InitInstance();
};
class MyFrame:public CFrameWnd
{
public:
MyFrame();
};
#endif //MFC_DEMO_MFC_H
- mfc.cpp
cpp
#include "mfc.h"
MyApp app;
BOOL MyApp::InitInstance()
{
MyFrame* frame = new MyFrame;
frame->ShowWindow(SW_SHOWNORMAL);
frame->UpdateWindow();
m_pMainWnd = frame;
return TRUE;
}
MyFrame::MyFrame() {
Create(NULL, TEXT("mfc"));
}