wxWidgets on Linux — Newbie Installation and First Program
January 16th, 2008
This web page serves to document my first attempt at using the wxWidgets C++ library.
This is not a precise “how-to guide,” unless you happen to have a PC that is set up exactly like mine, with exactly the same libraries and permissions, etc.
My experience during wxWidgets installation is a very limited and a very specific experience. I acquired the wxWidgets files using the CD-ROM that accompanies the book “Cross-Platform GUI Programming with wxWidgets.” I compiled a “flavor” of wxWidgets that will be used to compile Linux applications using GTK+ 2.
If you are using the same book (ISBN 0-13-147381-6), and you are using Linux and GTK+ 2, then this “guide” might help you a lot.
Otherwise, this might help you a little.
If you are not using the book or are not using Linux, then I will make an educated guess that you have arrived at this page after performing a Google search for one of the errors that I mention. Hopefully this page will help you overcome those errors.
(By the way, I do net yet feel ready to give a full review of the GUI Programming wxWidgets book. I do like the book so far, but I have only read chapters 1 through 6.)
Steps I performed to install and use wxWidgets:
1. Got this book: ISBN 0-13-147381-6
2. Inserted the CD-ROM from the book, then copied the following from the cd to my hard drive:
/media/cdrom0/wxWidgets/
(The wxWidgets 2.6.1 distribution.)
3. (Again, I’m on linux.) Got rid of all files in that folder with extensions:
txt
zip
exe
4. That leaves only:
wxWidgets/wxBase-2.6.1.tar.gz
wxWidgets/wxGTK-2.6.1.tar.gz
wxWidgets/wxMac-2.6.1.tar.gz
wxWidgets/wxMGL-2.6.1.tar.gz
wxWidgets/wxMotif-2.6.1.tar.gz
wxWidgets/wxWidgets-2.6.1-HTB.tar.gz
wxWidgets/wxWidgets-2.6.1-HTML.tar.gz
wxWidgets/wxWidgets-2.6.1-PDF.tar.gz
wxWidgets/wxWidgets-2.6.1.tar.gz
wxWidgets/wxX11-2.6.1.tar.gz
5. un-gzip, un-tar all that. that only gives you 7 folders:
(The HTB, HTML, and PDF stuff all ends up in “wxWidgets-2.6.1/”)
wxWidgets/wxBase-2.6.1
wxWidgets/wxGTK-2.6.1
wxWidgets/wxMac-2.6.1
wxWidgets/wxMGL-2.6.1
wxWidgets/wxMotif-2.6.1
wxWidgets/wxWidgets-2.6.1
wxWidgets/wxX11-2.6.1
6. navigated into the directory wxWidgets/wxWidgets-2.6.1/
7. created a new directory:
wxWidgets/wxWidgets-2.6.1/buildGTK26ud/
8. ran the "configure" script. (may or may not need to be root. The need — or choice — to use “su” or “sudo” depends on you and your scenario.)
localhost:wxWidgets/wxWidgets-2.6.1/buildGTK26ud# ../configure --with-gtk=2 --enable-unicode --enable-debug --disable-shared --disable-monolithic
9. ran make. (may or may not need to be root. The need — or choice — to use “su” or “sudo” depends on you and your scenario.)
localhost:wxWidgets/wxWidgets-2.6.1/buildGTK26ud# make
10. make install. (may or may not need to be root. The need — or choice — to use “su” or “sudo” depends on you and your scenario.)
localhost:wxWidgets/wxWidgets-2.6.1/buildGTK26ud# make install
11. ldconfig. (may or may not need to be root. The need — or choice — to use “su” or “sudo” depends on you and your scenario.)
localhost:wxWidgets/wxWidgets-2.6.1/buildGTK26ud# ldconfig
(not necessary? maybe not necessary since we compiled wxWidgets with “--disable-shared” ….? I am unsure.)
12. After completing all the above steps, I noticed that there are many new files created for me in various subdirectories of “usr/local/”
New inside “/usr/local/lib/”
directory wx/
lib/libwx_baseud-2.6.a
lib/libwx_baseud_net-2.6.a
lib/libwx_baseud_xml-2.6.a
lib/libwx_gtk2ud_adv-2.6.a
lib/libwx_gtk2ud_core-2.6.a
lib/libwx_gtk2ud_html-2.6.a
lib/libwx_gtk2ud_media-2.6.a
lib/libwx_gtk2ud_qa-2.6.a
lib/libwx_gtk2ud_xrc-2.6.a
lib/libwxjpegd-2.6.a
lib/libwxpngd-2.6.a
lib/libwxregexud-2.6.a
lib/libwxtiffd-2.6.aNew inside “/usr/local/bin/”
lrwxrwxrwx 1 root root 54 2008-01-16 09:38 wx-config -> /usr/local/lib/wx/config/gtk2-unicode-debug-static-2.6 lrwxrwxrwx 1 root root 8 2008-01-16 09:38 wxrc -> wxrc-2.6 -rwxr-xr-x 1 root root 7580651 2008-01-16 09:38 wxrc-2.6New inside “/usr/local/include”
/usr/local/include/wx-2.6/wx/ (bunch of header files)
13. Now it is time to test whether we can compile some sample code that was provided with wxWidgets. Go to directory buildGTK26ud/samples/minimal/
localhost:wxWidgets/wxWidgets-2.6.1/buildGTK26ud/samples/minimal# make
localhost:wxWidgets/wxWidgets-2.6.1/buildGTK26ud/samples/minimal# ./minimal
14. Now it is time to create my own program. I need to know whether g++ can find the wxWidgets library no matter where I put my source code, so I will create a sample cpp file in my home directory. Create a program:
//******************************************** // WARNING: THIS IS AN IDIOTIC EXAMPLE. // THIS PROGRAM WILL RUN INDEFINITELY // UNTIL YOU MANUALLY KILL IT. #include "wx/wx.h" class FirstOne : public wxApp { public: virtual bool OnInit(); }; DECLARE_APP(FirstOne) IMPLEMENT_APP(FirstOne) bool FirstOne::OnInit() { return true; } //********************************************
15. You can’t just run g++ on that! You need lots and lots of flags to pass to g++16. To successfully use gcc/g++ to compile a wxWidgets program (on Linux), the easiest thing you can do is use [/usr/local/bin/wx-config]
16. Here is how you can use wx-config to view a list of flags that you will need in order to compile your program:
localhost# /usr/local/bin/wx-config --cxx --cppflags
returns this:
g++
-I/usr/local/lib/wx/include/gtk2-unicode-debug-static-2.6 -I/usr/local/include/wx-2.6 -D__WXDEBUG__ -D__WXGTK__ -D_FILE_OFFSET_BITS=64 -D_LARGE_FILES -D_LARGEFILE_SOURCE=1
17. Here is how you can use wx-config to view a list of library parameters that you will need to pass to g++ so that it knows how to find the libraries it needs for compiling your wxWidgets program:
localhost# /usr/local/bin/wx-config --libs
returns this:
-L/usr/local/lib -pthread -L/usr/X11R6/lib /usr/local/lib/libwx_gtk2ud_xrc-2.6.a /usr/local/lib/libwx_gtk2ud_qa-2.6.a /usr/local/lib/libwx_gtk2ud_html-2.6.a /usr/local/lib/libwx_gtk2ud_adv-2.6.a /usr/local/lib/libwx_gtk2ud_core-2.6.a /usr/local/lib/libwx_baseud_xml-2.6.a /usr/local/lib/libwx_baseud_net-2.6.a /usr/local/lib/libwx_baseud-2.6.a -Wl,--export-dynamic -pthread -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgdk_pixbuf-2.0 -lpangoxft-1.0 -lpangox-1.0 -lpango-1.0 -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lglib-2.0 -lXinerama -lXxf86vm -lexpat -lwxregexud-2.6 -lwxtiffd-2.6 -lwxjpegd-2.6 -lwxpngd-2.6 -lz -ldl -lm
18. Now you will use the information you learned in the previous two steps in order to call g++ correctly.
my file is: “FirstOne.cpp”
instead of this:
g++ FirstOne.cpp -o FirstOne
you need ALL OF THIS:
g++ -I/usr/local/lib/wx/include/gtk2-unicode-debug-static-2.6 -I/usr/local/include/wx-2.6 -D__WXDEBUG__ -D__WXGTK__ -D_FILE_OFFSET_BITS=64 -D_LARGE_FILES -D_LARGEFILE_SOURCE=1 FirstOne.cpp -o FirstOne -L/usr/local/lib -pthread -L/usr/X11R6/lib /usr/local/lib/libwx_gtk2ud_xrc-2.6.a /usr/local/lib/libwx_gtk2ud_qa-2.6.a /usr/local/lib/libwx_gtk2ud_html-2.6.a /usr/local/lib/libwx_gtk2ud_adv-2.6.a /usr/local/lib/libwx_gtk2ud_core-2.6.a /usr/local/lib/libwx_baseud_xml-2.6.a /usr/local/lib/libwx_baseud_net-2.6.a /usr/local/lib/libwx_baseud-2.6.a -Wl,--export-dynamic -pthread -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgdk_pixbuf-2.0 -lpangoxft-1.0 -lpangox-1.0 -lpango-1.0 -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lglib-2.0 -lXinerama -lXxf86vm -lexpat -lwxregexud-2.6 -lwxtiffd-2.6 -lwxjpegd-2.6 -lwxpngd-2.6 -lz -ldl -lm
Errors that I encountered along the way:
FirstOne.cpp:2:19:
wx/wx.h: No such file or directory
FirstOne.cpp:4: error:
forward declaration of `class XXXXX'
The above error happened because I did not understand how to use the “wx-config” command-line utility. Please see the earlier section of this article where I discuss the use of wx-config.
In file included from /usr/local/include/wx-2.6/wx/defs.h:21,
from /usr/local/include/wx-2.6/wx/wx.h:15,
from XXXXX.cpp:2:
/usr/local/include/wx-2.6/wx/platform.h:260:22: wx/setup.h: No such file or directory
In file included from /usr/local/include/wx-2.6/wx/platform.h:263,
from /usr/local/include/wx-2.6/wx/defs.h:21,
from /usr/local/include/wx-2.6/wx/wx.h:15,
from XXXXX.cpp:2:
/usr/local/include/wx-2.6/wx/chkconf.h:84:9: #error "wxUSE_DYNLIB_CLASS must be defined."
/usr/local/include/wx-2.6/wx/chkconf.h:92:9: #error "wxUSE_EXCEPTIONS must be defined."
/usr/local/include/wx-2.6/wx/chkconf.h:100:9: #error "wxUSE_FILESYSTEM must be defined."
/usr/local/include/wx-2.6/wx/chkconf.h:113:9: #error "wxUSE_DYNAMIC_LOADER must be defined."
The above error happened because I did not understand how to use the “wx-config” command-line utility. Please see the earlier section of this article where I discuss the use of wx-config.
/usr/local/include/wx-2.6/wx/window.h:1143: error: storage size of `
m_foregroundColour' isn't known
/usr/local/include/wx-2.6/wx/window.h:1150: error: storage size of `
m_updateRegion' isn't known
The above error happened because I did not understand how to use the “wx-config” command-line utility. Please see the earlier section of this article where I discuss the use of wx-config.
In file included from /usr/local/include/wx-2.6/wx/dc.h:812, from /usr/local/include/wx-2.6/wx/wx.h:33, from XXXXX.cpp:2: /usr/local/include/wx-2.6/wx/gtk/dc.h: In member function `void wxDC::SetColourMap(const wxPalette&)': /usr/local/include/wx-2.6/wx/gtk/dc.h:49: error: `SetPalette' undeclared (first use this function) /usr/local/include/wx-2.6/wx/gtk/dc.h:49: error: (Each undeclared identifier is reported only once for each function it appears in.)
The above error happened because I did not understand how to use the “wx-config” command-line utility. Please see the earlier section of this article where I discuss the use of wx-config.
/tmp/ccfQD91X.o(.text+0x16): In function `wxCreateApp()': : undefined reference to `wxAppConsole::CheckBuildOptions(char const*, char const*)' /tmp/ccfQD91X.o(.text+0x9c): In function `main': : undefined reference to `wxEntry(int&, char**)' /tmp/ccfQD91X.o(.gnu.linkonce.r._ZTV8FirstOne+0x8): undefined reference to `wxApp::GetClassInfo() const' /tmp/ccfQD91X.o(.gnu.linkonce.r._ZTV8FirstOne+0x14): undefined reference to `wxObject::ReservedObjectFunc1()' /tmp/ccfQD91X.o(.gnu.linkonce.r._ZTV8FirstOne+0x18): undefined reference to `wxObject::ReservedObjectFunc2()'
The above error happened because I did not understand how to use the “wx-config” command-line utility. Please see the earlier section of this article where I discuss the use of wx-config.
: undefined reference to `wxApp::~wxApp [not-in-charge]()' /tmp/ccfQD91X.o(.gnu.linkonce.r._ZTI8FirstOne+0x8): undefined reference to `typeinfo for wxApp' /tmp/ccfQD91X.o(.gnu.linkonce.t._ZN12wxAppConsole22SetInitializerFunctionEPFPS_vE+0x7): In function `wxAppConsole::SetInitializerFunction(wxAppConsole* (*)())': : undefined reference to `wxAppConsole::ms_appInitFn' collect2: ld returned 1 exit status
The above error happened because I did not understand how to use the “wx-config” command-line utility. Please see the earlier section of this article where I discuss the use of wx-config.
/usr/lib/gcc-lib/i486-linux/3.3.5/../../../crt1.o(.text+0x18): In function `_start': ../sysdeps/i386/elf/start.S:115: undefined reference to `main' collect2: ld returned 1 exit status
The above error happened because I forgot to write my application using the necessary wxWidgets macros DECLARE_APP and IMPLEMENT_APP. Please see the earlier section of this article where I give code for a sample “dummy” application that will compile successfully.