Compiling and getting started

From WxWiki
Jump to: navigation, search

This is a guide to help you get started quickly and easily on Linux with wxWidgets built from source.

Before starting

Install all necessary build tools, including g++ and autotools. (On Ubuntu, you will want to install package build-essential)

You will also need gtk+ (don't forget to install the -dev package if your distro uses them). For Ubuntu 8.04 this will be libgtk-2.0-dev. On lucid (10.4) it is libgtk2.0-dev.

It is recommended to use at least GTK+ 2.2.3, and some features are only available when using more recent version, like GTK+ 2.8.

wxWidgets

Download the wxGTK source archive from http://www.wxwidgets.org/downloads/ and expand it. If you intend to use it in place (not install it in system dirs) choose a place where it can stay forever because you won't be able to move it without breaking the build.

Then, the following steps are done on the terminal.

cd /path/to/wxGTK-2.8.x # adapt path as needed
mkdir gtk-build
cd gtk-build

we create a new folder to build from, this enables us to have many wxWidgets builds, for instance a debug and a release one, or a Unicode and an ANSI one, or a static and a shared one, etc.

Then you will need to call configure with something like

../configure --enable-unicode

However, if you want to compile wxWidgets on a 64-bit version of Linux in such a way that the library can be linked to both statically and dynamically, then you want to add the -fPIC flag to the configure call. That is done in the following way:

../configure --enable-unicode CFLAGS="-fPIC" CXXFLAGS="-fPIC"

The options you pass to configure at this point will determine what build you get.

--enable-unicodemakes a Unicode build
--with-openglenables OpenGL support
--disable-sharedbuilds static libs instead of shared ones
--prefix=[...]makes wxWidgets be installed in another location than default /usr/local

(for a list of all possible options, write ../configure --help)

If it stops saying it can't find a package, install it (with -dev packages if any) and run configure again. When it is done without errors, you are ready to build.

make

When the build is done, and if no error occured, you can now install it

sudo make install

Enter your password as needed. wxWidgets will be installed in /usr/local/

On some systems it is also necesary to run sudo ldconfig at this point.

Test installation

Open a terminal and type

wx-config --version

It should be the version you just built.

If it is not:

try running

which wx-config

and see if it is the one you just installed (if you did not change the default value, you installed /usr/local/bin/wx-config)

if you get something like /usr/bin/wx-config while you installed /usr/local/bin/wx-config, it means there is another version of wxWidgets installed somewhere else, probably installed by the system. You can quickly get it out the way with a hacky command like sudo mv /usr/bin/wx-config /usr/bin/wx-config-old1.

Then, to test more in depth, you can look at the samples. Open the Samples folder located at wxGTK-2.8.x/gtk-build/samples (be careful : wxGTK-2.8.4/samples contains the source code, while wxGTK-2.8.4/gtk-build/samples contains the makefiles for your system. for building samples, you will want to cd into the latter, while to study the code you will want to open the former)

cd into one of the samples, and type make. You can then open the sample to see wxWidgets in action.

Building apps with wxWidgets

The tool wx-config allows you to quickly build wxWidgets apps

g++ `wx-config --cppflags` `wx-config --libs` widgetTest.cpp

wx-config --cppflags returns flags necessary to compilation, wx-config --libs returns flags necessary to linking. This little example has a single file so both are used at the same time but in a bigger project compiling and linking will most likely be two different steps. (How to use g++ is beyond the scope of this document)

For more information, see article Wx-Config.

Running wxWidgets projects

If when running a wxWidgets app you get an error like:

./a.out: error while loading shared libraries: libwx_gtk2_aui-2.8.so.0: cannot open shared object file: No such file or directory

this means your system does not search for libs in /usr/local/libs (or wherever you installed wxWidgets) by default. To solve this, any of the following should work :

  • Write export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib on the terminal before opening the executable (from the same terminal). To save having to do this each time, add that line to your ~/.bashrc or similar.
  • Give a --rpath /usr/local/lib/ flag to the linker while building (tip: since you generally don't invoke the linker directly, but rather perform linking through GCC, use GCC's syntax to pass along flags to the linker : -Wl,--rpath,/usr/local/lib/)
  • Become root and execute /sbin/ldconfig /usr/local/lib. This will configure dynamic linker runtime bindings, adding all the libraries in /usr/local/lib, so it's not really a very good idea!