Setting up wxWidgets Environment for Visual Studio

OVERVIEW

In another blog post, we saw Setting up wxWidgets Environment for C++ in Windows 10 without having to use Visual Studio. The main problem with that method is that we have to take care of everything ourselves, specially the handling of make files. With that in mind, this blog post explains how to leverage Visual Studio to do wxWidgets development. Using an IDE not only makes things easier but faster as well.

This has been tested with Visual Studio 2022. There seem to be some issues if you try this with VS 2019 or older.

INSTALL VCPKG

What we need is a package called vcpkg which is an open source tool developed by Microsoft to help incorporate third-party C++ libraries and frameworks for use in VS. To do that. make sure you have Git installed in your system. Go to the Github repo for vcpkg. https://github.com/microsoft/vcpkg

In the command line, go to the directory of your choice eg. d:\projects and type in git clone https://github.com/microsoft/vcpkg.git

D:\projects>git clone https://github.com/microsoft/vcpkg.git
Cloning into 'vcpkg'...
remote: Enumerating objects: 158493, done.
remote: Counting objects: 100% (477/477), done.
remote: Compressing objects: 100% (257/257), done.
remote: Total 158493 (delta 275), reused 353 (delta 220), pack-reused 158016R
Receiving objects: 100% (158493/158493), 61.41 MiB | 3.80 MiB/s, done.
Resolving deltas: 100% (100285/100285), done.
Checking out files: 100% (9007/9007), done.

Now change the directory to vcpkg eg. cd vcpkg and run the following command:

bootstrap-vcpkg.bat

Now we are going to use vcpkg to setup wxWidgets for 64 bit Windows. To do that run

vcpkg install wxWidgets:x64-windows

This will take a while to install and setup.

Next we integrate wxWidgets libraries to work with Visual Studio via vcpkg. Use the following command:

vcpkg integrate install

The last step is to install the wxWidgets library now. Use the command

vcpkg install wxwidgets:x64-windows

Now we are ready to test out a wxWidgets program in Visual Studio

BUILD SAMPLE WXWIDGETS APPLICATION

  • Open Visual Studio. Choose a new Project Type as C++ Empty Windows Project (C++, Windows, Console)
  • Lets call the project as FourthProject. You can choose whatever name you like .
  • Under Header Files, create a new file FourthProject.h and put the following code:
#pragma once
class MyProjectApp : public wxApp
{
public:
    MyProjectApp();
    virtual ~MyProjectApp();
    virtual bool OnInit() override;
};
  • Under Source Files, create a new file FourthProject.cpp and put the following code:
#include <wx/wx.h>
#include "FourthProject.h"

MyProjectApp::MyProjectApp()
{
}

MyProjectApp::~MyProjectApp()
{
}

bool MyProjectApp::OnInit()
{
	wxFrame* mainFrame = new wxFrame(nullptr, wxID_ANY, L"MyProject");
	mainFrame->Show(true);
	return true;
}

wxIMPLEMENT_APP(MyProjectApp);
wxIMPLEMENT_WXWIN_MAIN_CONSOLE;
  • Build the Solution. If all goes well, the project will be built without errors.
  • Run the project, and you should see the following output.

The only small issue here is that since we have created a Windows Console project, it runs a terminal window first before running the actual application. We have to change the project type from Console to Windows. For that the following steps are required:

  • In the Solution Explorer, right click the Project item and select Properties
  • In the Properties dialog, go to Linker->System
  • In the Subsystem field, change the value to /SUBSYSTEM:WINDOWS and click Ok

Rebuild the project and now the application will run without the background console window.

If you deploy the application on some other computer, then make sure all the dll files in the execution folder are also copied along with the main exe.

Be the first to comment

Leave a Reply

Your email address will not be published.


*