閑談
近來(lái)提起寫界面的興趣,雖然會(huì)winapi睹酌,不過學(xué)習(xí)新東西(wxWidgets算老物了),東搞西搞剩檀,終于把wxWidgets的開發(fā)環(huán)境搭出來(lái)憋沿,想想還是寫下來(lái)。其實(shí)libcef也不錯(cuò)沪猴,不過終究集成個(gè)瀏覽器卤妒,感覺龐大、臃腫字币。
環(huán)境
OS | IDE | wxWidgets |
---|---|---|
Win7 | VS2015 | 3.1.1 |
wxWidgets庫(kù)文件
先下載 wxWidgets 3.1.1 ,至于你看到的大概是這樣:
VS2015不是有debug共缕、Release兩種編譯版本嗎洗出?暫時(shí)先這樣對(duì)應(yīng)。dev是debug版本的連接庫(kù)图谷,ReleaseDLL是Release的連接庫(kù)(運(yùn)行庫(kù))翩活。dev,ReleaseDLL大概是這樣理解便贵。ReleasePDB是調(diào)試用的菠镇,不用理。我們需要的是dev承璃、ReleaseDLL利耍。后面跟個(gè)x64是64位的,沒有的是x86盔粹。
當(dāng)然隘梨,還有頭文件。之后解壓舷嗡,重新部署文件轴猎,就是下面這個(gè)樣子:
VS2015 (vc140)
wxGui (解決方案)
+ wxGui(項(xiàng)目)
- wxWidgets (3.1.1) https://github.com/wxWidgets/wxWidgets/releases/tag/v3.1.1
- dll
+ vc140_dll (wxMSW-3.1.1_vc140_ReleaseDLL.7z)
+ vc140_x64_dll (wxMSW-3.1.1_vc140_x64_ReleaseDLL.7z)
- include (wxWidgets-3.1.1-headers.7z)
- msvc
- wx
- lib
+ vc140_dll (wxMSW-3.1.1_vc140_Dev.7z)
+ vc140_x64_dll (wxMSW-3.1.1_vc140_x64_Dev.7z)
VS2015配置
首先,新建個(gè)解決方案进萄。之后捻脖,復(fù)制個(gè)wxWidgets的HelloWorld到我們的項(xiàng)目。就是下面這段內(nèi)容中鼠。
// 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!");
}
好簡(jiǎn)單可婶,是不是?跟著下來(lái)配置下VS援雇。
這里配置頭文件目錄扰肌、庫(kù)文件目錄,我把它放在相對(duì)路徑熊杨,方便(笑)J镄瘛盗舰!注意庫(kù)文件目錄64位跟32位是不同的,不要搞錯(cuò)桂躏。vc140_dll是x86钻趋,vc140_x64_dll不用說了吧。
運(yùn)行時(shí)環(huán)境變量
上面我們弄好頭剂习、庫(kù)目錄蛮位,還有個(gè)運(yùn)行時(shí)環(huán)境變量,程序運(yùn)行時(shí)鳞绕,需要用到wxWidgets庫(kù)的dll失仁,不配置好,運(yùn)行時(shí)會(huì)彈出缺少dll的對(duì)話框们何。
在圖片中環(huán)境里面加上
PATH= dll路徑
PATH=$(ProjectDir)..\wxWidgets\lib\vc140_x64_dll
將dll的路徑添加到PATH環(huán)境變量中萄焦,不用加%PATH%,因?yàn)槲野l(fā)現(xiàn)一樣可以冤竹,估計(jì)是下面那個(gè)合并環(huán)境變量的原因吧拂封。vc140_x64_dll是debug版本的,而release的就是ReleaseDLL的文件目錄鹦蠕。
setup.h platform.h報(bào)錯(cuò)
一切準(zhǔn)備都做好冒签,不過會(huì)報(bào)錯(cuò)。大概就是setup.h platform.h什么的钟病。platform.h(include\wx\platform.h)中有段話:
Include wx/setup.h for the Unix platform defines generated by configure and
the library compilation options
Note that it must be included before defining hardware symbols below as they
could be already defined by configure but it must be included after defining
the compiler macros above as msvc/wx/setup.h relies on them under Windows.
之后跟著這句
#include "wx/setup.h"
其實(shí)很簡(jiǎn)單萧恕,就是換成
#include msvc/wx/setup.h
不過,我改成下面這樣肠阱,適配下Mingw編譯器
#if defined(__WXMSW__)
#if defined(__MINGW32__)
#include "wx/setup.h"
#else
#include "msvc/wx/setup.h"
#endif
#else
#include "wx/setup.h"
#endif
不知為什么廊鸥,wxWidgets的開發(fā)人員不寫多幾句,而且都8102了辖所。還有在我們的helloworld的cpp文件開頭加上下面兩句:
#define wxMSVC_VERSION_AUTO
#define WXUSINGDLL //動(dòng)態(tài)鏈接
好吧惰说。如無(wú)意外,再次build應(yīng)該沒問題缘回。
注1吆视、官網(wǎng)下載的庫(kù)文件都是動(dòng)態(tài)鏈接的,所以要加上#define WXUSINGDLL酥宴,除非自己編譯靜態(tài)庫(kù)啦吧。
注2、使用VS時(shí)拙寡,添加wxMSVC_VERSION_AUTO會(huì)自動(dòng)搜索頭文件路徑授滓,就好像是vc_x64_dll上加上版本號(hào)變成vc140_x64_dll,詳細(xì)內(nèi)容可以參考setup.h。
MingW
假如想使用MingW般堆,要選擇對(duì)應(yīng)編譯器在孝,可參考下面這個(gè)Makefile文件:
# makefile
# mingw64-win32-seh-7.2.0
CC=gcc
CXX=g++
LD=g++
#wxWidgets 目錄路徑
WXWIN=../../wxWidgets
#每個(gè)目錄路徑名間隔加空格
INCLUDE=include lib/gcc720_x64_dll/mswu
LIBS=lib/gcc720_x64_dll
#每個(gè)庫(kù)名間隔加空格
WXLIBBASE=wxbase31u wxmsw31u_core
CXXFLG_INCLUDE=$(patsubst %,-I$(WXWIN)/%,$(INCLUDE))
CXXFLG_LIB=$(patsubst %,-L$(WXWIN)/%,$(LIBS))
CXXFLG_LINK=$(patsubst %,-l%,$(WXLIBBASE))
main.exe:main.o
$(CXX) $< -o $@ $(CXXFLG_INCLUDE) $(CXXFLG_LIB) $(CXXFLG_LINK)
%.o:%.cpp
$(CXX) -c $< $(CXXFLG_INCLUDE)
.PHONY:clean test
clean:
rm *.o *.exe
test:
.\main.exe
最后
github上有個(gè)解決方案文件,可以看看淮摔。
https://github.com/Mtcno/wxGuiHelloWorld-wxWidgets3.1.1-vc140
編輯于【2018-5-27】