In this post, I’m building wxWidgets on a Mac from source. This is done on a very old MacBook running El Capitan.
Building wxWidgets on a Mac is not very difficult. First, you download the
source code and extract to a location where you want to use it from. I didn’t
want to install it on a system directory, so I extracted it in my home folder
(~/src/wxWidgets
).
Documentation is provided in a txt file. I prefer to build a static library, so
that the generated applications won’t need the wxWidgets library installed or
distributed. That’s mentioned a bit late in the txt file. Create a folder like
~/src/wxWidgets/build-cocoa-static
and from there configure the
installation with ../configure --disable-shared
(you can also pass
--enable-debug
to enable debugging). The
--disable-shared
will make the generated executables larger in
size, but they will be independent.
After configure finishes, run make
to build wxWidgets. This will
take a while. You can also build demos and samples (e.g. cd samples;
make
).
Now you can also install wxWidgets so it’s available system wide. That’s done
with sudo make install
but I didn’t want to do that. Instead, I
added ~/src/wxWidgets/build-cocoa-static
to my PATH. This way, I
don’t pollute my system with files I won’t know how to uninstall and I can
maintain multiple versions of wxWidgets side by side.
The Makefiles that the samples are using are quite complex (to me at least). I
think copying the minimal sample’s Makefile might be a good starting point. I
have created an even smaller Makefile that uses the wx-config
utility. wx-config
generated command line arguments for compilation
tasks. For example:
wx-config --cxx
generates the compiler command that should be used (g++
in this case, with some Mac specific arguments)wx-config --cxxflags
generates the compiler arguments, such as the include directories where wxWidgets headers can be found and the appropriate preprocessor symbolswx-config --libs
generates the linker arguments, i.e. the libraries that the application should be linked against.
Using these, a Makefile can look like this:
CXX=`wx-config --cxx`
all: wxhelloworld
wxhelloworld: wxhelloworld.o
$(CXX) -o wxhelloworld wxhelloworld.o `wx-config --libs`
wxhelloworld.o: wxhelloworld.cpp
$(CXX) -c -o wxhelloworld.o wxhelloworld.cpp `wx-config --cxxflags`
clean:
rm *.o
rm wxhelloworld
Hope this helps.