【配置】VS2015下wxWidgets 3.1.1开发环境

闲谈

近来提起写界面的兴趣,虽然会winapi,不过学习新东西(wxWidgets算老物了),东搞西搞,终于把wxWidgets的开发环境搭出来,想想还是写下来。其实libcef也不错,不过终究集成个浏览器,感觉庞大、臃肿。

环境

OS IDE wxWidgets
Win7 VS2015 3.1.1

wxWidgets库文件

先下载 wxWidgets 3.1.1 ,至于你看到的大概是这样:

wxWidgets 3.1.1库

VS2015不是有debug、Release两种编译版本吗?暂时先这样对应。dev是debug版本的连接库,ReleaseDLL是Release的连接库(运行库)。dev,ReleaseDLL大概是这样理解。ReleasePDB是调试用的,不用理。我们需要的是dev、ReleaseDLL。后面跟个x64是64位的,没有的是x86。

头文件

当然,还有头文件。之后解压,重新部署文件,就是下面这个样子:


VS2015 (vc140)

wxGui (解决方案)
+   wxGui(项目)
-   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配置

首先,新建个解决方案。之后,复制个wxWidgets的HelloWorld到我们的项目。就是下面这段内容。

// wxWidgets "Hello World" Program
// For compilers that support precompilation, includes "wx/wx.h".

#include 
#ifndef WX_PRECOMP
    #include 
#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!");
}

好简单,是不是?跟着下来配置下VS。

wxWidgets头文件目录
wxWidgets库文件目录

这里配置头文件目录、库文件目录,我把它放在相对路径,方便(笑)!!注意库文件目录64位跟32位是不同的,不要搞错。vc140_dll是x86,vc140_x64_dll不用说了吧。

运行时环境变量

上面我们弄好头、库目录,还有个运行时环境变量,程序运行时,需要用到wxWidgets库的dll,不配置好,运行时会弹出缺少dll的对话框。


dll路径

在图片中环境里面加上

PATH= dll路径
PATH=$(ProjectDir)..\wxWidgets\lib\vc140_x64_dll

将dll的路径添加到PATH环境变量中,不用加%PATH%,因为我发现一样可以,估计是下面那个合并环境变量的原因吧。vc140_x64_dll是debug版本的,而release的就是ReleaseDLL的文件目录。

setup.h platform.h报错

一切准备都做好,不过会报错。大概就是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"   

其实很简单,就是换成

#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的开发人员不写多几句,而且都8102了。还有在我们的helloworld的cpp文件开头加上下面两句:

#define wxMSVC_VERSION_AUTO 
#define WXUSINGDLL  //动态链接

好吧。如无意外,再次build应该没问题。

注1、官网下载的库文件都是动态链接的,所以要加上#define WXUSINGDLL,除非自己编译静态库。
注2、使用VS时,添加wxMSVC_VERSION_AUTO会自动搜索头文件路径,就好像是vc_x64_dll上加上版本号变成vc140_x64_dll,详细内容可以参考setup.h。

MingW

假如想使用MingW,要选择对应编译器,可参考下面这个Makefile文件:

# makefile
# mingw64-win32-seh-7.2.0

CC=gcc
CXX=g++
LD=g++

#wxWidgets 目录路径
WXWIN=../../wxWidgets

#每个目录路径名间隔加空格
INCLUDE=include lib/gcc720_x64_dll/mswu
LIBS=lib/gcc720_x64_dll

#每个库名间隔加空格
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上有个解决方案文件,可以看看。

https://github.com/Mtcno/wxGuiHelloWorld-wxWidgets3.1.1-vc140

编辑于【2018-5-27】

你可能感兴趣的:(【配置】VS2015下wxWidgets 3.1.1开发环境)