{"id":3652,"date":"2021-10-31T15:00:07","date_gmt":"2021-10-31T15:00:07","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=3652"},"modified":"2025-07-06T07:56:40","modified_gmt":"2025-07-06T07:56:40","slug":"setting-up-wxwidgets-environment-for-c-in-ubuntu-linux","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2021\/10\/31\/setting-up-wxwidgets-environment-for-c-in-ubuntu-linux\/","title":{"rendered":"Setting up wxWidgets Environment for C++ in Ubuntu Linux"},"content":{"rendered":"\n<p>The objective here is to provide step by step instructions to setup wxWidgets for use in C++ applications under Linux.<\/p>\n\n\n\n<p><strong>Setup Build Environment<\/strong><\/p>\n\n\n\n<p>Ensure that the required libraries and tools for C++ development are installed:<\/p>\n\n\n\n<p><code> sudo apt-get install  build-essential<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  <code>sudo apt-get install libwxgtk3.2-dev<\/code><\/code><\/pre>\n\n\n\n<p><strong>Download and Compile wxWidgets<\/strong><\/p>\n\n\n\n<p>Go to http:\/\/wxwidgets.org\/downloads\/ and download the link which says <em>Source for Linux, MacOS etc.<\/em> This will download a bz2 file.  Unzip this file into a directory of your choice. Now we have to compile the source to form a static library which will be linked to your application when it is built.<\/p>\n\n\n\n<p>If you have unzipped the source into a folder called wxWidgets-3.1.5 then create a new folder within it for creating the library files. You can call it anything but for now we will call it wxWidgetsLib. <\/p>\n\n\n\n<p><code>mkdir wxWidgetsLib<\/code><\/p>\n\n\n\n<p>So now you have<em> wxWidgets-3.1.5\/wxWidgetsLib<\/em><\/p>\n\n\n\n<p>While you are in wxWidgets-3.1.5 run the following commands to build wxWidgets:<\/p>\n\n\n\n<p>.<code>\/configure --enable-shared --enable-u<\/code>tf8<\/p>\n\n\n\n<p>If you want to use wxWebView control to embed a web browser then you have to add extra arguments to configure. For Ubuntu, you need to enable wxWebView and webKitGTK. But before that you need to install webkit for GTK 4.0 as below<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> sudo apt install libwebkit2gtk-4.1-dev \n.\/configure --enable-shared --enable-utf8 --enable-webview --enable-webviewwebkit\n<\/code><\/pre>\n\n\n\n<p><code>make<\/code><\/p>\n\n\n\n<p>If you want to use the latest wxAUI library to get a more modern UI with docks and resizable panels, then you need to enable the AUI library:<\/p>\n\n\n\n<p>.\/configure &#8211;enable-shared &#8211;enable-utf8 &#8211;enable-aui<\/p>\n\n\n\n<p>make<\/p>\n\n\n\n<p>At this stage the wxWidgets library should be ready for use in your applications<\/p>\n\n\n\n<p><strong>Testing With A Sample Program<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;wx\/string.h&gt;\n#include &lt;wx\/crt.h&gt;\n\nint main (int argx, char **argv) {\n\n        wxString str1 = wxT(&quot;Linux&quot;);\n        wxString str2 = wxT(&quot;Windows&quot;);\n        wxString str3 = wxT(&quot;MacOS&quot;);\n\n        wxString str = str1+ wxT(&quot; &quot; ) + str2 + wxT(&quot; &quot;) + str3;\n        wxPuts(str);\n}\n~                                                                               \n~                                                                               \n~                 \n<\/pre><\/div>\n\n\n<p>Lets create a simple console based program as shown above. This can be saved as addition.cpp. We will use g++ to compile and link it into an executable:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">g++ addition.cpp `<code>wx-config --cxxflags --libs` <\/code> -o addition<\/pre>\n\n\n\n<p>Be sure to put the backticks for the arguments as shown above. They have to be backticks and not regular single quotes.<\/p>\n\n\n\n<p>On completion you can run the program with:<\/p>\n\n\n\n<p><code>.\/addition<\/code><\/p>\n\n\n\n<p>The output will be as shown below:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>amit@ubuntu-amit<\/strong>:<strong>\/var\/projects\/c++<\/strong>$ .\/addition\nLinux Windows MacOS\n<\/pre>\n\n\n\n<p>What we did was create a console application using wxWidgets. Lets create a simple program which creates a default window:<\/p>\n\n\n\n<p>window.h<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;wx\/wx.h&gt;\n\nclass Window: public wxFrame {\n\n  public:\n\t  Window(const wxString&amp; title);\n};\n\n\n<\/pre><\/div>\n\n\n<p>window.cpp<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;window.h&quot;\n\nWindow::Window(const wxString&amp; title)\n        : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(250,100)) {\n\n        Centre();\n}\n\n\n<\/pre><\/div>\n\n\n<p>main.h<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;wx\/wx.h&gt;\n\nclass TestApp: public wxApp {\n\n\tpublic :\n\t\tvirtual bool OnInit();\n};\n\n<\/pre><\/div>\n\n\n<p>main.cpp<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;main.h&quot;\n#include &quot;window.h&quot;\n\nIMPLEMENT_APP(TestApp)\n\nbool TestApp::OnInit() {\n\n        Window *window = new Window(wxT(&quot;Test app&quot;));\n        window-&gt;Show(true);\n\n        return true;\n}\n\n\n<\/pre><\/div>\n\n\n<p>To compile and link it run:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">g++ window.cpp main.cpp `<code>wx-config --cxxflags --libs std<\/code>` -o main<\/pre>\n\n\n\n<p>To run it type <\/p>\n\n\n\n<p>.\/main <\/p>\n\n\n\n<p>This will show a small resizable Window <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"250\" height=\"100\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/10\/Screenshot-from-2021-10-31-20-25-36.png\" alt=\"\" class=\"wp-image-3654\"\/><\/figure>\n\n\n\n<p>For simple small programs g++ on the command line will suffice. But for larger projects, it is best to a make file.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>The objective here is to provide step by step instructions to setup wxWidgets for use in C++ applications under Linux. Setup Build Environment Ensure that <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2021\/10\/31\/setting-up-wxwidgets-environment-for-c-in-ubuntu-linux\/\" title=\"Setting up wxWidgets Environment for C++ in Ubuntu Linux\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":3595,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[302,298,365],"tags":[],"class_list":["post-3652","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cc","category-linux","category-wxwidgets"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3652","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=3652"}],"version-history":[{"count":11,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3652\/revisions"}],"predecessor-version":[{"id":4686,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3652\/revisions\/4686"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/3595"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=3652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=3652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=3652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}