If you have previously installed wxWidgets on your system for the purpose of programming cross-platform graphical applications, you probably want to learn how to compile programs. Here are the contents of two source files for a minimal example of wxWidgets. First, the header file, hi.h
:
class hiApp : public wxApp
{
public:
hiApp(void) {};
bool OnInit(void);
};
class hiFrame : public wxFrame
{
public:
hiFrame(void);
void OnQuit(wxCommandEvent&);
void OnAbout(wxCommandEvent&);
void OnMisc(wxCommandEvent&);
private:
wxDECLARE_EVENT_TABLE();
};
enum {
HI_QUIT = wxID_EXIT,
HI_ABOUT = wxID_ABOUT,
HI_MISC
};
And now the implementation file, hi.cpp
:
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
#include "hi.h"
IMPLEMENT_APP(hiApp)
bool hiApp::OnInit(void) {
hiFrame *mainWindow = new hiFrame;
mainWindow->Show(false);
return true;
}
wxBEGIN_EVENT_TABLE(hiFrame, wxFrame)
EVT_MENU(HI_QUIT, hiFrame::OnQuit)
EVT_MENU(HI_ABOUT, hiFrame::OnAbout)
EVT_MENU(HI_MISC, hiFrame::OnMisc)
wxEND_EVENT_TABLE()
hiFrame::hiFrame(void)
: wxFrame(NULL, wxID_ANY, wxT("HITHERE!")) {
wxMenu *file = new wxMenu;
file->Append(HI_QUIT, wxT("E&xit"), wxT("Quit Hi There!"));
file->Append(HI_MISC, wxT("&Misc"), wxT("Miscellaneous"));
wxMenu *help = new wxMenu;
help->Append(HI_ABOUT, wxT("About"), wxT("About Hi There!"));
wxMenuBar *bar = new wxMenuBar;
bar->Append(file, wxT("&File"));
bar->Append(help, wxT("&Help"));
SetMenuBar(bar);
}
void hiFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) {
Close(true);
}
void hiFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) {
wxMessageBox(wxT("Copyright © 2015, Fulcrum Industries, Inc."),
wxT("About Hi There!"), wxOK);
}
void hiFrame::OnMisc(wxCommandEvent& WXUNUSED(event)) { }
Copy the above text to the respective hi.h
and hi.cpp
files, and then you should be ready to compile the source into an executable, like this:
g++ `wx-config --cxxflags --libs` hi.cpp -o hi
Unfortunately, on Mavericks or later, you get a fatal error:
bash-3.2$ g++ `wx-config --cxxflags --libs` hi.cpp -o hi
In file included from hi.cpp:3:
In file included from /usr/local/include/wx-3.0/wx/wx.h:15:
In file included from /usr/local/include/wx-3.0/wx/object.h:19:
In file included from /usr/local/include/wx-3.0/wx/memory.h:15:
In file included from /usr/local/include/wx-3.0/wx/string.h:46:
/usr/local/include/wx-3.0/wx/strvararg.h:30:18: fatal error: 'tr1/type_traits'
file not found
#include <tr1/type_traits>
^
1 error generated.
bash-3.2$
This is because of a change in the default standard library used on XCode since the release of Mavericks. wxWidgets relies on certain assumptions about the standard library being libstdc++
rather than libc++
. You can fix this problem by manually specifying -stdlib=libstdc++
on the command line.
g++ `wx-config --cxxflags --libs` -stdlib=libstdc++ hi.cpp -o hi
The code will now build, but with a ton of warnings about an expression potentially being evaluated with side effects. Although this is generally a good warning to have, wxWidgets uses enough macros to turn this warning into pure noise. To disable it, also add -Wno-potentially-evaluated-expression
to your compile line.
g++ `wx-config --cxxflags --libs` -stdlib=libstdc++ -Wno-potentially-evaluated-expression hi.cpp -o hi
This should successfully (and quietly) build your first wxWidgets application!
I recommend adding the above flags to your Makefile or other build system. I keep a shell script in my build directory: