Search code examples
c++wxwidgets

WxWidgets Hello World fails to compile: "wchar_t must be available"


When I try to compile the hello world application from wxWidgets in MS Visual Studio 2017 Community Edition, I get a large number of errors. I believe the key error is #error: "wchar_t must be available". As far as I have looked there is only one other person I have found with this error, and when they asked, they didn't get any useful answers. [http://www.cplusplus.com/forum/windows/197245/]

What can I do to make this program compile successfully, and how can I ensure that I don't get the same error on future (real) projects?

I just started learning wxWidgets and I installed version 3.0.4 on my laptop. I downloaded the installer then used Visual Studio to compile the libraries as instructed. I believe the installation completed successfully as all the sample projects compiled and ran successfully. however when I tried to compile the provided hello world application in its own project, I got the aforementioned errors. I have ensured that the wxWidgets headers are being linked, although since I am new to using non-built-in libraries, I may have made a mistake. I am unsure why the sample projects would work when hello world doesn't.

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

This is an exact replica of the code available at https://docs.wxwidgets.org/trunk/overview_helloworld.html I have included the code here in case the code on the website is updated.

As you can imagine I would expect the program to compile and run normally, but instead, I have gotten a large number of errors, all of which are listed in this image: https://media.discordapp.net/attachments/452995181667418119/527236184326275113/unknown.png

Here are some interesting observations I made:

  • The majority of the errors stem from the compiler not being able to find the definition of class wxFrame

  • The class wxApp is still defined and I can inherit the class myApp from it.

  • chartype.h gives an error that wchar_t must be available, even though according to http://www.cplusplus.com/reference/cwchar/wchar_t/ it is built-in to c++ and I don't need to include any other headers.

  • The error in chartype.h is found in this code snippet:


/* Unicode support requires wchar_t */
#if !wxUSE_WCHAR_T
    #error "wchar_t must be available"
#endif /* Unicode */
  • As far as I can tell, the #define is never actually set in any source files I can find, but it represents whether the wchar_t type exists. I added a small variable into my code of this type and it was created fine

Any help is appreciated.


Solution

  • How are you compiling the hello world example? Your image shows errors like E0020 and E0070 which are not standard MSVC compiler errors at all (those would be of Cnnnn form). You need to create a new "Win32 application" project and change the settings as explained in the documentation (as you're using MSVC, last paragraph applies to your case).

    Alternatively, download the more recent v3.1.2 and just reference the new wxwidgets.props file from your project as explained here.