天天看点

VS2019配置wxWidgets v3.1.5开发环境

编译wxWidgets库

  1. 如果只是使用wxWidgets DLL库可以省略编译这一步,直接下载编译好的库
    • http://wxwidgets.org/downloads/
    • 点击"Download Windows Binarires"
    • 需要下载Header Files, Development Files供开发使用
    • 下载Release DLLs给发布程序使用
  2. 下载源码包,需要要git,否则第三方库源码为空,比如expat, zlib等。不嫌麻烦也可以一个一个在wxWidgets的github网站上下载好再解压到对应的源码目录,例如https://github.com/wxWidgets/libexpat:
    1. git clone --recurse-submodules -j4 https://github.com/wxWidgets/wxWidgets.git
    2. git checkout -b 3.1.5 v3.1.5 #切换到3.1.5发布版本
  3. 在build/msw里双击wx_vc16.sln(对应的是VS2019)
  4. 编译配置:DLL Debug: 调试版本的DLL库,DLL Release:发布版本的DLL库,Debug:调试版本的静态库,Release: 发布版本的静态库
  5. 生成解决方案,编译结果在lib\vc_x64_dll 或 lib\vc_x64_lib
  6. 修改目录名为vc14x_x64_dll 或 vc14x_x64_lib,并复制到C:\Data\wxWidgets\lib\
  7. 复制build\msw\wx_setup.props 到C:\Data\wxWidgets\build\msw\下
  8. 复制wxWidgets根目录下的wxwidgets.props到C:\Data\wxWidgets\下

使用wxWidget库

  1. 创建系统环境变量WXWIN,值为C:\Data\wxWidgets
  2. 因为wxWidgets会优先选择DLL库而不是静态库,如果要选择静态库,请修改C:\Data\wxWidgets\lib\vc14x_x64_dll为别的名字。因为这个选择是在VS2019启动时加载wxwidgets.props里做的,后面再修改这个目录名需要重启VS2019。
  3. 在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)"
  • 开始写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!");
}