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. 为了避免后面应用程序静态链接wxWidgets库时依赖VC Runtime,修改下面的选项:
    1. C/C++ - Code Generation - Runtime Library
      1. Debug: "Multi-threaded Debug (/MTd)" 
      2. Release: "Multi-threaded (/MT)"
    2. 这2个配置在每一个项目文件里,可以使用grepWin工具批量修改build\msw\*.vcxproj文件,替换2个字符串如下。
      • MultiThreadedDLL  替换为 MultiThreaded
      • MultiThreadedDebugDLL 替换为 MultiThreadedDebug
  6. 生成解决方案,编译结果在lib\vc_x64_dll 或 lib\vc_x64_lib
  7. 修改目录名为vc14x_x64_dll 或 vc14x_x64_lib,并复制到C:\Data\wxWidgets\lib\
  8. 复制build\msw\wx_setup.props 到C:\Data\wxWidgets\build\msw\下
  9. 复制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

最新的wxWidgets已经修改了一个头文件来解决这个问题:wx/types.h

参考:https://github.com/wxWidgets/wxWidgets/blob/master/include/wx/types.h

直接修改本地的wx/types.h,增加红色部分。

#if defined(__VISUALC__) && (_MSC_VER < 1800)
    #define wxW64 __w64
#else
    #define wxW64
#endif

  • 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!");
}

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值