作者:jafon.tian
转载请注明出处:https://blog.youkuaiyun.com/JT_Notes
本文主要记录如何从源码编译,安装以及使用wxWidgets。
0 - 编译环境
- 操作系统 : windows 11
- 编译环境
- cmake : 3.31.2
- msys : msys2-x86_64-20220603(MSYS_NT-10.0-26100)
1 - 下载源码
可以通过clone或下载zip包的形式
- clone
repo地址 : https://github.com/wxWidgets/wxWidgets.git - zip
下载页 : https://wxwidgets.org/downloads/
本文使用zip包模式,下载地址为 https://github.com/wxWidgets/wxWidgets/releases/download/v3.2.8/wxWidgets-3.2.8.zip
注意,wxWidgets使用了多了子仓,如果使用clone方式,注意需要更新子仓代码。
2 - 编译和安装
-
2.1 创建构建目录
目录全路径应避免有空格存在。
本文使用如下目录 C:\D\src\wxWidgets -
2.2 解压代码并创建编译目录
C:\D\SRC\WXWIDGETS
├─build # 编译目录
├─wxWidgets-3.2.8.zip # 代码压缩包
└─wxWidgets-3.2.8 # 解压出的代码
- 2.3 进入编译目录
cd .\build\
- 2.4 配置并生成编译文件
# -DCMAKE_BUILD_TYPE=Release : release version
# -DCMAKE_INSTALL_PREFIX="C:/D/src/wxWidgets/install" : 当运行install时的目标目录
# -DwxBUILD_SHARED=OFF : 编译成静态库
cmake -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="C:/D/src/wxWidgets/install" -DwxBUILD_SHARED=OFF ..\wxWidgets-3.2.8
配置相关信息
-- Which libraries should wxWidgets use?
wxUSE_STL: OFF (use C++ STL classes)
wxUSE_REGEX: builtin (enable support for wxRegEx class)
wxUSE_ZLIB: builtin (use zlib for LZW compression)
wxUSE_EXPAT: builtin (use expat for XML parsing)
wxUSE_LIBJPEG: builtin (use libjpeg (JPEG file format))
wxUSE_LIBPNG: builtin (use libpng (PNG image format))
wxUSE_LIBTIFF: builtin (use libtiff (TIFF file format))
wxUSE_NANOSVG: builtin (use NanoSVG for rasterizing SVG)
wxUSE_LIBLZMA: OFF (use liblzma for LZMA compression)
-- Configured wxWidgets 3.2.8 for Windows
Min OS Version required at runtime: Windows XP / Windows Server 2003 (x64 Edition)
Which GUI toolkit should wxWidgets use? msw
Should wxWidgets be compiled into single library? OFF
Should wxWidgets be linked as a shared library? OFF
Should wxWidgets support Unicode? ON
What wxWidgets compatibility level should be used? 3.0
- 2.5 编译
cmake --build .
.
.
.
[1022/1022] Linking CXX static library lib\gcc_x64_lib\libwxmsw32u_qa.a
- 2.6 安装
cmake --build . --target install
.
.
.
-- Installing: C:/D/src/wxWidgets/install/lib/cmake/wxWidgets/wxWidgetsConfig.cmake
-- Installing: C:/D/src/wxWidgets/install/lib/cmake/wxWidgets/wxWidgetsConfigVersion.cmake
3 - 测试程序
- 3.1 创建目录
目录全路径应避免有空格存在。
本文使用如下目录
C:\D\SRC\WXTEST
├─build # 编译目录
├─CMakeLists.txt # cmake文件
└─main.cpp # 测试代码
- 3.2 main.cpp
使用由wxWidgets提供的测试程序(https://docs.wxwidgets.org/latest/overview_helloworld.html)
// Start of wxWidgets "Hello World" Program
#include <wx/wx.h>
class MyApp : public wxApp
{
public:
bool OnInit() override;
};
wxIMPLEMENT_APP(MyApp);
class MyFrame : public wxFrame
{
public:
MyFrame();
private:
void OnHello(wxCommandEvent& event);
void OnExit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
};
enum
{
ID_Hello = 1
};
bool MyApp::OnInit()
{
MyFrame *frame = new MyFrame();
frame->Show(true);
return true;
}
MyFrame::MyFrame()
: wxFrame(nullptr, wxID_ANY, "Hello World")
{
wxMenu *menuFile = new wxMenu;
menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
"Help string shown in status bar for this menu item");
menuFile->AppendSeparator();
menuFile->Append(wxID_EXIT);
wxMenu *menuHelp = new wxMenu;
menuHelp->Append(wxID_ABOUT);
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append(menuFile, "&File");
menuBar->Append(menuHelp, "&Help");
SetMenuBar( menuBar );
CreateStatusBar();
SetStatusText("Welcome to wxWidgets!");
Bind(wxEVT_MENU, &MyFrame::OnHello, this, ID_Hello);
Bind(wxEVT_MENU, &MyFrame::OnAbout, this, wxID_ABOUT);
Bind(wxEVT_MENU, &MyFrame::OnExit, this, wxID_EXIT);
}
void MyFrame::OnExit(wxCommandEvent& event)
{
Close(true);
}
void MyFrame::OnAbout(wxCommandEvent& event)
{
wxMessageBox("This is a wxWidgets Hello World example",
"About Hello World", wxOK | wxICON_INFORMATION);
}
void MyFrame::OnHello(wxCommandEvent& event)
{
wxLogMessage("Hello world from wxWidgets!");
}
- 3.3 CMakeLists.txt
cmake_minimum_required(VERSION 3.20) # Or a suitable version for your project
project(MyWxWidgetsApp CXX)
# 设置wxWidgets_DIR,从而cmake可以找到wxWidgetsConfig.cmake
set(wxWidgets_DIR "${CMAKE_CURRENT_LIST_DIR}/../wxWidgets/install/lib/cmake/wxWidgets")
# Find wxWidgets. Specify the components you need (e.g., core, base, gl, net).
# REQUIRED ensures that CMake will fail if wxWidgets is not found.
# CONFIG必须设置,否则cmake不会使用wxWidgetsConfig.cmake
find_package(wxWidgets REQUIRED gl core base OPTIONAL_COMPONENTS net CONFIG)
# Include the wxWidgets configuration file found by find_package.
# This sets up include directories and other necessary build settings.
if(wxWidgets_USE_FILE) # not defined in CONFIG mode
include(${wxWidgets_USE_FILE})
endif()
# Add your executable target.
add_executable(MyWxWidgetsApp main.cpp)
# Link your executable against the wxWidgets libraries.
# PRIVATE ensures that only this target links against them directly.
# 使用静态库 -static-libgcc -static-libstdc++
# 使用视窗 -mwindows, 否则会另外出现一个命令行窗口
target_link_libraries(MyWxWidgetsApp PRIVATE ${wxWidgets_LIBRARIES} -static-libgcc -static-libstdc++ -mwindows)
- 3.4 编译
cd .\build\
cmake -G "Ninja" ..\
cmake --build .
C:/D/src/wxtest/build/MyWxWidgetsApp.exe
- 3.5 运行
双击MyWxWidgetsApp.exe
430

被折叠的 条评论
为什么被折叠?



