Search code examples
c++visual-c++wxwidgets

No windows appearing when using WxWidgets on MSVC


I am having an issue in using WxWidgets, my code is as follows:

#include <wx/wx.h>
#include <iostream>

class MyApp : public wxApp
{
public:
    virtual bool OnInit();
};

class MyFrame : public wxFrame
{
public:
    MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
};

bool MyApp::OnInit()
{
    MyFrame* frame = new MyFrame("Hello World", wxPoint(50, 50), wxSize(450, 340));
    frame->Show(true);
    return true;
}

MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
    : wxFrame(NULL, wxID_ANY, title, pos, size)
{
}

wxIMPLEMENT_APP(MyApp);

int main()
{
    std::cout << "Hello world, why isn't there a window showing?\n";
}

It compiles fine, but no window appears except from the output: Hello world, why isn't there a window showing? but it doesn't show any GUI window. The only window that pops up

I have been using vcpkg on MSVC to get the WxWidgets, does any one know how I could resolve these errors?


Solution

  • You're building your application as a console application (/subsystem:console linker option), meaning that its entry point is main(), instead of building it as a Windows application (/subsystem:windows). You can do it like this, but then you're responsible for calling the library entry point (wxEntry()) yourself.

    But unless you have a good reason to do it like this, you should just change the application type in the IDE/linker option if you use makefiles or something else to make it a Windows application. You should then remove main() entirely as it won't be used -- and its absence won't result in linking errors for a Windows application.