本系列开发语言为C++。如果你是以python为开发语言,还可以参考我的另外一个系列的博文
要使用wxWidgets进行开发之前,我们必须先安装和配置wxWidgets。
window下的安装和配置
vscode开发配置
wxWidgets开发可以使用vscode,也可以使用Visual studio,CLion等,首先我们来看一下vscode下如何配置。
1、先下载MinGW64并安装,下载地址。https://www.mingw-w64.org/downloads/ 。
如果这样联网安装出现错误,也可以直接通过下面的链接下载对应的版本。
解压到C:\mingw-w64\mingw64目录
修改环境变量,在path中添加目录即可。
2、下载vscode并安装。
3、接着打开vscode,安装插件。包括c/c++ Extension Pack。
4、在指定目录下新建一个项目目录,例如新建~/git/MyApp目录,到github上下载一个wxWidgets项目模板(地址:https://github.com/huckor/wxwidgets-vscode),解压后把所有文件复制到项目目录下。复制后的项目目录结构如下:
5、到wxWidgets官网下载windows版的wxWidgets源代码,复制到项目目录的dep/win目录下,如下图
6、然后进入$projectpath/dep/win/build/msw目录,运行以下命令
mingw32-make.exe -f makefile.gcc SHARED=0 UNICODE=1 BUILD=release
如果一切顺利的话,会在$projectpath/dep/win/lib/目录下多出一个gcc_lib目录,如下图,编译过程还是蛮耗时的,大家耐心等待一会。
7、修改.vdcode目录下的c_pp_properties.json、launch.json和tasks.json文件,设置mingw64的路径和wxWidgets的版本,例如我下载的wxWidgets是3.2版本,对应的修改即可。
c_cpp_properties.json内容参考:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/src/**",
"${workspaceFolder}/dep/win/lib/gcc_lib/mswu",
"${workspaceFolder}/dep/win/include"
],
"defines": [
"_WINDOWS",
"_UNICODE",
"__WXMSW__",
"NDEBUG",
"NOPCH"
],
"compilerPath": "c:\\mingw-w64\\mingw64\\bin\\g++.exe",
"cStandard": "c11",
"cppStandard": "gnu++14",
"intelliSenseMode": "gcc-x86"
},
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/src/**",
"${workspaceFolder}/dep/mac/mac_lib/lib/wx/include/osx_cocoa-unicode-static-3.2",
"${workspaceFolder}/dep/mac/include"
],
"defines": [
"WX_PRECOMP",
"__WXOSX_COCOA__",
"_FILE_OFFSET_BITS=64",
"__ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES=1"
],
"compilerPath": "/usr/bin/g++",
"cStandard": "c11",
"cppStandard": "gnu++17",
"intelliSenseMode": "clang-x86"
},
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/src/**",
"${workspaceFolder}/dep/linux/linux_lib/lib/wx/include/gtk3-unicode-static-3.2",
"${workspaceFolder}/dep/linux/include"
],
"defines": [
"WX_PRECOMP",
"__WXGTK__",
"_FILE_OFFSET_BITS=64"
],
"compilerPath": "/usr/bin/g++",
"cStandard": "c11",
"cppStandard": "gnu++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
launch.json文件内容参考:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "cppdbg",
"request": "launch",
"linux": {
"program": "${workspaceRoot}/out/StrategyApp",
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
},
"osx": {
"program": "${workspaceRoot}/out/StrategyApp",
"MIMode": "lldb"
},
"windows": {
"program": "${workspaceRoot}\\out\\StrategyApp.exe",
"MIMode": "gdb",
"miDebuggerPath": "C:\\mingw-w64\\mingw64\\bin\\gdb.exe"
},
"stopAtEntry": false,
"cwd": "${workspaceFolder}/out",
"program": "${workspaceFolder}/out/StrategyApp",
"environment": [],
"externalConsole": false,
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "Compile & Link"
}
]
}
tasks.json文件内容参考:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "Compile & Link",
"linux":{
"command": "g++",
"args": [
"-o",
"${workspaceFolder}/out/StrategyApp",
"${workspaceFolder}/out/**.o",
"-L${workspaceFolder}/dep/linux/linux_lib/lib",
"-Bstatic",
"-pthread",
"-lwx_gtk3u_core-3.2",
"-lwx_baseu-3.2",
"-lwxtiff-3.2",
"-lwxjpeg-3.2",
"-lgtk-3",
"-lgdk-3",
"-lpangocairo-1.0",
"-lpango-1.0",
"-lharfbuzz",
"-latk-1.0",
"-lcairo-gobject",
"-lcairo",
"-lgdk_pixbuf-2.0",
"-lgio-2.0",
"-lgobject-2.0",
"-lgthread-2.0",
"-lglib-2.0",
"-lX11",
"-lSM",
"-lpng",
"-lz",
"-ldl",
"-lm",
"-lXtst",
"-lpangoft2-1.0",
"-lfontconfig",
"-lfreetype"
]
},
"osx":{
"command": "g++",
"args": [
"-I${workspaceFolder}/dep/mac/mac_lib/lib/wx/include/osx_cocoa-unicode-static-3.2",
"-I${workspaceFolder}/dep/mac/include",
"-isysroot",
"/Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk",
"-mmacosx-version-min=13",
"${workspaceFolder}/out/**.o",
"-o",
"${workspaceFolder}/out/StrategyApp",
"-L${workspaceFolder}/dep/mac/mac_lib/lib",
"-Bstatic",
"-framework",
"IOKit",
"-framework",
"Carbon",
"-framework",
"Cocoa",
"-framework",
"AudioToolbox",
"-framework",
"System",
"-framework",
"OpenGL",
"-framework",
"WebKit",
"-framework",
"Security",
"-framework",
"QuartzCore",
"-lwx_osx_cocoau_core-3.2",
"-lwx_baseu-3.2",
"-lwxtiff-3.2",
"-lwxjpeg-3.2",
"-lwxpng-3.2",
"-lwxregexu-3.2",
"-lz",
"-lpthread",
"-liconv",
"-llzma"
],
},
"windows": {
"command": "g++",
"args": [
"-L${workspaceFolder}\\dep\\win\\lib\\gcc_lib",
"-Lc:\\mingw-w64\\mingw64\\x86_64-w64-mingw32\\lib",
"-mwindows",
"-static",
"${workspaceFolder}\\out\\**.o",
"-o",
"${workspaceFolder}\\out\\StrategyApp.exe",
"-lwxmsw32u_core",
"-lwxbase32u",
"-lwxpng",
"-lcomdlg32",
"-lgdi32",
"-lcomctl32",
"-lole32",
"-loleaut32",
"-ldmoguids",
"-luuid",
"-lwinspool",
"-lz",
"-lwxregexu",
"-lwxzlib",
"-luxtheme",
"-loleacc",
"-lshlwapi",
"-lversion"
],
},
"group": {
"kind": "build",
"isDefault": true
},
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"dependsOn": [
"MoveObjects"
]
},
{
"type": "shell",
"label": "Compile",
"linux":{
"command": "g++",
"args": [
"-c",
"$(find",
"${workspaceFolder}/src/",
"-type",
"f",
"-iregex",
"'.*\\.cpp')",
"-g",
"-D__WXGTK__",
"-D_FILE_OFFSET_BITS=64",
"-DWX_PRECOMP",
"-fno-strict-aliasing",
"-pthread",
"-I${workspaceFolder}/dep/linux/linux_lib/lib/wx/include/gtk3-unicode-static-3.2",
"-I${workspaceFolder}/dep/linux/include",
"-I/usr/include/gtk-3.0",
"-I/usr/include/at-spi2-atk/2.0",
"-I/usr/include/at-spi-2.0",
"-I/usr/include/dbus-1.0",
"-I/usr/lib/x86_64-linux-gnu/dbus-1.0/include",
"-I/usr/include/gio-unix-2.0",
"-I/usr/include/cairo",
"-I/usr/include/pango-1.0",
"-I/usr/include/fribidi",
"-I/usr/include/harfbuzz",
"-I/usr/include/atk-1.0",
"-I/usr/include/pixman-1",
"-I/usr/include/uuid",
"-I/usr/include/freetype2",
"-I/usr/include/libpng16",
"-I/usr/include/gdk-pixbuf-2.0",
"-I/usr/include/libmount",
"-I/usr/include/blkid",
"-I/usr/include/glib-2.0",
"-I/usr/lib/x86_64-linux-gnu/glib-2.0/include",
"-I/usr/include/gtk-3.0/unix-print",
"-Wall"
]
},
"osx":{
"command": "g++",
"args": [
"-I${workspaceFolder}/dep/mac/mac_lib/lib/wx/include/osx_cocoa-unicode-static-3.2",
"-I${workspaceFolder}/dep/mac/include",
"-isysroot",
"/Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk",
"-mmacosx-version-min=13",
"-c",
"$(find",
"${workspaceFolder}/src/",
"-type",
"f",
"-iregex",
"'.*\\.cpp')",
"-g",
"-D__WXOSX_COCOA__",
"-D_FILE_OFFSET_BITS=64",
"-D__ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES=1",
"-DWX_PRECOMP",
"-fno-strict-aliasing",
"-fno-common",
"-Wall"
]
},
"windows": {
"command": "g++",
"args": [
"-I${workspaceFolder}\\dep\\win\\lib\\gcc_lib\\mswu",
"-I${workspaceFolder}\\dep\\win\\include",
"-c",
"$(",
"dir",
"-Path",
"${workspaceFolder}\\src",
"-Filter",
"*.cpp",
"-Recurse",
"|",
"%{$_.FullName}",
")",
"-g",
"-Wall",
"-D_WINDOWS",
"-D_UNICODE",
"-D__WXMSW__",
"-DNDEBUG",
"-DNOPCH"
],
},
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
]
},
{
"type": "shell",
"label": "MoveObjects",
"linux":{
"command": "mv",
"args": [
"${workspaceFolder}/*.o",
"${workspaceFolder}/out/"
]
},
"osx":{
"command": "mv",
"args": [
"${workspaceFolder}/*.o",
"${workspaceFolder}/out/"
]
},
"windows": {
"command": "Move-Item",
"args": [
"-Path",
"${workspaceFolder}\\*.o",
"-Destination",
"${workspaceFolder}\\out",
"-Force"
],
},
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [],
"dependsOn": [
"Compile"
]
}
]
}
选中项目中的main.cpp文件,然后点击右上角的debug按钮即可启动项目了。
运行后的效果如下
Visual Studio开发配置
1、下载window版的源代码,解压。
2、进入目录${wxWidgets}\build\msw,打开wx_vc17.sln文件,选择不同的目标平台,点生成解决方案。
编译好之后,就可以把Visual Studio项目关闭了。
3、再打开Visual Studio,新建一个空的C++项目。
新建一个main.cpp文件
把下面的示例代码复制到main.cpp中
//////////////////////////////////////////////////////////////////////////
//主程序
// wxWidgets "Hello World" Program
// For compilers that support precompilation, includes "wx/wx.h".
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
#define ID_MENU_HELLO 1 // Menu Item ID
//////////////////////////////////////////////////////////////////////////////////////////////////
///// DECLARE THE APPLICATION'S MAIN FRAME WINDOW
//////////////////////////////////////////////////////////////////////////////////////////////////
class Frame : public wxFrame
{
public:
Frame(const wxString& title, const wxPoint& pos, const wxSize& size);
private:
void OnHello(wxCommandEvent& event);
void OnExit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
};
//////////////////////////////////////////////////////////////////////////////////////////////////
///// DECLARE THE APPLICATION CLASS
//////////////////////////////////////////////////////////////////////////////////////////////////
class HelloWorld : public wxApp
{
public:
// Define the initialization method
// Its called on application start up
virtual bool OnInit();
};
//////////////////////////////////////////////////////////////////////////////////////////////
///// IMPLEMENT THE OnInit() WHICH INITIALIZES THE APPLICATION
/////////////////////////////////////////////////////////////////////////////////////////////
bool HelloWorld::OnInit()
{
// Create main application window
Frame* frame = new Frame("Hello World", wxPoint(50, 50), wxSize(300, 300));
// Display the frame window
frame->Show(true);
// Start the event loop
return true;
}
////////////////////////////////////////////////////////////////////////////////////////
///// START RUNNING THE APPLICATION
////////////////////////////////////////////////////////////////////////////////////////
wxIMPLEMENT_APP(HelloWorld);
////////////////////////////////////////////////////////////////////////////////////////
///// IMPLEMENT THE FRAME (APP MAIN WINDOW) CONSTRUCTOR
///////////////////////////////////////////////////////////////////////////////////////
Frame::Frame(const wxString& title, const wxPoint& pos, const wxSize& size)
:wxFrame(NULL, wxID_ANY, title, pos, size)
{
wxMenu* menuFile = new wxMenu;
menuFile->Append(ID_MENU_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!");
// Register events for this frame
Bind(wxEVT_MENU, &Frame::OnHello, this, ID_MENU_HELLO);
Bind(wxEVT_MENU, &Frame::OnAbout, this, wxID_ABOUT);
Bind(wxEVT_MENU, &Frame::OnExit, this, wxID_EXIT);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
////// DEFINE EVENT HANDLERS FOR THE APPLICATION FRAME
///////////////////////////////////////////////////////////////////////////////////////////////////////////
// Event Handler for clicking the About Menu item
// Navigate to Help > About in the GUI app
void Frame::OnAbout(wxCommandEvent& event)
{
wxMessageBox("This is a wxWidgets Hello World example", "About Hello World", wxOK | wxICON_INFORMATION);
}
// Event Handler for clicking the Hello Menu item
// Naviaget to File > Hello in the GUI app
void Frame::OnHello(wxCommandEvent& event)
{
wxLogMessage("Hello world from wxWidgets!");
}
// Event handler on clicking the Exit Menu item
// Navigate to File > Exit in the GUI app
void Frame::OnExit(wxCommandEvent& event)
{
Close(true);
}
在项目上点击鼠标右键,然后选择属性,打开项目属性对话框。
按下图设置附加包含目录
添加wxwidgets的include目录
点应用之后,我们的main.cpp就已经没有错误提示,但是这时候运行项目,就会出现找不到wxbase32ud.lib的错误。
再次打开项目属性,设置wxWidgets lib路径。
这时候运行项目,又出现如下的错误
这是因为我们的项目是console项目,改成window项目即可。
再次启动项目,一个可爱的窗口就显示出来了。
这样,Visual Studio下的开发配置就好了。
CLion开发配置
由于CLion是收费的,我没有用过,所以这里略过
Mac下的安装和配置
从App Store下载Xcode安装,或者从https://developer.apple.com 下载Command line tools并安装。
下载vscode并安装。
接着打开vscode,安装插件。包括c/c++ Extension Pack。
在指定目录下新建一个项目目录,例如新建~/git/MyApp目录,到github上下载一个wxWidgets项目模板(地址:https://github.com/huckor/wxwidgets-vscode),解压后把所有文件复制到项目目录下。复制后的项目目录结构如下:
到wxWidgets的官网下载mac版的源代码,加压后复制到dep/mac目录。
查看mac上的SDK版本,例如我的mac有如下版本
find / -name "*.sdk" -print 2>/dev/null
/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk
/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk
/Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk
然后在该目录下新建一个mac_lib目录,进入mac_lib目录,运行如下命令,这里的SDK版本就是上面我们查看的版本。
../configure --disable-shared --enable-unicode --prefix="$(pwd)" --with-macosx-sdk=/Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk --with-macosx-version-min=13
如果没有出现错误,在运行make命令
make
修改.vdcode目录下的c_pp_properties.json、launch.json和tasks.json文件,把sdk和wxWidgets改成对应的版本,例如我下载的wxWidgets是3.2版本,sdk是13版本,对应的修改即可。
选中项目中的main.cpp文件,然后点击右上角的debug按钮即可启动项目了。
运行后的效果如下
linux下的安装和配置
因为我这里没有Linux GUI环境,平时开发也不用Linux,所以这部分就不讲了,大家可以自行参考项目doc目录中的linux.pdf文件进行配置。
评论区