編譯wxWidgets庫
- 如果隻是使用wxWidgets DLL庫可以省略編譯這一步,直接下載下傳編譯好的庫
- http://wxwidgets.org/downloads/
- 點選"Download Windows Binarires"
- 需要下載下傳Header Files, Development Files供開發使用
- 下載下傳Release DLLs給釋出程式使用
- 下載下傳源碼包,需要要git,否則第三方庫源碼為空,比如expat, zlib等。不嫌麻煩也可以一個一個在wxWidgets的github網站上下載下傳好再解壓到對應的源碼目錄,例如https://github.com/wxWidgets/libexpat:
- git clone --recurse-submodules -j4 https://github.com/wxWidgets/wxWidgets.git
- git checkout -b 3.1.5 v3.1.5 #切換到3.1.5釋出版本
- 在build/msw裡輕按兩下wx_vc16.sln(對應的是VS2019)
- 編譯配置:DLL Debug: 調試版本的DLL庫,DLL Release:釋出版本的DLL庫,Debug:調試版本的靜态庫,Release: 釋出版本的靜态庫
- 生成解決方案,編譯結果在lib\vc_x64_dll 或 lib\vc_x64_lib
- 修改目錄名為vc14x_x64_dll 或 vc14x_x64_lib,并複制到C:\Data\wxWidgets\lib\
- 複制build\msw\wx_setup.props 到C:\Data\wxWidgets\build\msw\下
- 複制wxWidgets根目錄下的wxwidgets.props到C:\Data\wxWidgets\下
使用wxWidget庫
- 建立系統環境變量WXWIN,值為C:\Data\wxWidgets
- 因為wxWidgets會優先選擇DLL庫而不是靜态庫,如果要選擇靜态庫,請修改C:\Data\wxWidgets\lib\vc14x_x64_dll為别的名字。因為這個選擇是在VS2019啟動時加載wxwidgets.props裡做的,後面再修改這個目錄名需要重新開機VS2019。
- 在VS2019中建立一個C++空項目(Empty Project)。
- 因為隻有x64的wxWidgets庫,需要把工程的配置切換到x64,否則出現加載wxwidgets.props錯誤。也可以直接删除x86配置。
- 打開Property Manager: View -- Other Windows -- Property Manager
- 在項目名上右擊,選"Add Existing Property Sheet ...",選擇C:\Data\wxWidgets\wxwidgets.props
- 修改項目的屬性
- Link -- System -- SubSystem
- 把“Console (/SUBSYSTEM:CONSOLE)”換成 "Windows (/SUBSYSTEM:WINDOWS)"
- Link -- System -- SubSystem
- 開始寫wxWidgets代碼,可以先使用wxWdigets網站上的hello world例程測試一下編譯環境。
- 如果使用的是DLL庫,可以把C:\Data\wxWidgets\lib\vc14x_x64_dll添加到系統Path變量。這樣就可以找到這些DLL庫而不用拷貝到.exe目錄。
- hello world程式靜态連結wxWidgets庫後大小為4MB左右,還算比較小巧。
錯誤處理
- E1256 __w64 can only be specified on int, long, and pointer types
Error LNK2001 unresolved external symbol main
沒有修改項目屬性為Windows,還是Console類型
hello_world.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
class MyApp : public wxApp
{
public:
virtual bool OnInit();
};
class MyFrame : public wxFrame
{
public:
MyFrame();
private:
void OnHello(wxCommandEvent& event);
void OnExit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
};
enum
{
ID_Hello = 1
};
wxIMPLEMENT_APP(MyApp);
bool MyApp::OnInit()
{
MyFrame *frame = new MyFrame();
frame->Show(true);
return true;
}
MyFrame::MyFrame()
: wxFrame(NULL, 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!");
}