Setting up WxWidgets with cmake and Cmake FetchContent
This blog post explains how I setup wxwidgets with cmake and cmake fetchcontent.
What I'll be doing is the bare minimum to setup wxwidgets with cmake fetchcontent. Your free to use any build system. I'll be using make as I'm on linux. You're free to use make, ninja or any other build system depending on your operating system.
I'll start by creating the root directory for the project and navigate it the dir.
I like to split by code, external deps used and build dir. So I'll create that now.
We obviously need a root CMakeLists.txt file and I'll create that.Since the only dependency is wxwidgets, I'll create a wxwidgets folder for it inside the vendors directory along with CMakeLists.txt file for both vendors and the wxwidgets directory.
I'll also need a CMakeLists.txt file for the src directory.
For wxwidgets app code, I have copied a very minimal example available in the wxwidgets website.
Lets create a main.cpp file and fill it in.
#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!");
}
Now lets start working on the cmake files. I'll start with the root CMakeLists.txt file.
cmake_minimum_required(VERSION 3.15)
project(hellowx)
add_subdirectory(vendors)
add_subdirectory(src)
The above contains the bare minimum for a cmake project file. We set the version using cmake_minimum_required
.
project
command lets us set the name of the project. Next we need our project to use the cmake files in the vendors and src directory and add_subdirectory takes care of it.
Next we'll dig deep into the vendors directory.
The CMakeLists.txt file inside the vendors directory has only one task, to include all the dependency directories to the to the project. Since we only have wxwidgets as our dependency, it only needs to add the wxwidgets as a subdirectory.
Moving on to the CMakeLists.txt file in the wxwidgets dir. We're using cmake fetchcontent to fetch wxwidgets by providing the github url.
include(FetchContent)
FetchContent_Declare(
wxwidgets
GIT_REPOSITORY https://github.com/wxWidgets/wxWidgets.git
GIT_TAG master
)
FetchContent_MakeAvailable(wxwidgets)
The above code snippet includes the fetchcontent module first, creates a declaration for the lib we want to fetch by passing the github url and the tag. The makeavailable line loads the library for use within the project.
Thats all for the wxwidgets dir cmake file.
Moving to the file CMakeLists.txt file, for src directory.
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
add_executable(myapp main.cpp)
target_link_libraries(
myapp
PRIVATE
wx::core wx::net wx::base
)
The first line specifies the location for the binaries being built. The location I have set is build/bin/.
The next line specifies the executable and the cpp file containing the main program.
The last line mentiones the libraries we want to link to our app.
Thats all you need to setup wxwidgets using cmake fetchcontent.
Thanks for Reading.