CMakeWizard 2.0 is here.
Howdy. It has been a while since I have updated this handy development tool. In case you don’t know, CMakeWizard is a simple set of Python scripts and C++ templates for jump-starting development projects. In short, you can start with an empty folder, run the CMakeWizard script and in seconds have a complete if skeletal C++ project up and running. It can create a shared or static library and an optional executable to test it all. If you want POSIX thread support you can have that too, along with optional sample code so that you can be up and running in no time. Finally, after generating the proper CMake-based project, it creates the C++ source and builds everything to prove that it works.
Now it is even better; it has built-in GTK support…
Recently I had a need to get into GTK+ development and since I did not wish to spend all of my time fooling with the makefiles and such to support it, I decided to see if my CMakeWizard was extensible enough to support GTK. As it turned out, it was. When I initially wrote this wizard some time ago I intended for it to have this kind of extensibility and I am pleased to say that things worked out as I hoped.
New Functionality:
One thing that made GTK a little more difficult to work with was the dependency on pkg-config to find the proper libgtk2.0 headers and libraries. In order to build a typical GTK project you would need to do something like this:
gcc myfile.c -o mybin `pkg-config –cflags gtk+-2.0 –libs gtk+-2.0 `
As it turned out, in the development branch of CMake there was an extension that did the same thing called FindGTK2.cmake. In the default distribution there is a FindGTK.cmake but that is only geared towards finding and dealing with GTK 1.2, an outdated version of the library. You can use this new extension by simply copying the provided FindGTK2.cmake to your CMake Modules folder. I have included the necessary file in the CMakeWizard tarball. It allows one to create CMake project files like so:
FIND_PACKAGE(GTK2 2.6 REQUIRED gtk)
IF(GTK2_FOUND)
ADD_DEFINITIONS( -Wall -ggdb )
INCLUDE_DIRECTORIES(${GTK2_INCLUDE_DIRS})
ADD_LIBRARY( foo SHARED foo.cpp )
ADD_EXECUTABLE(foo_test foo_main.cpp)
TARGET_LINK_LIBRARIES(foo_test foo ${GTK2_LIBRARIES} )
ENDIF(GTK2_FOUND)
This will lookup the right locations of the GTK libraries and headers and provide them via the variables GTK2_INCLUDE_DIRS and GTK2_LIBRARIES.
I have also extended the templating system to optionally generate a working minimal GTK GUI app, the venerable Hello World.
To use this just unpack the contents of the tarball to your home folder; it should create/overwrite a hidden folder called .cmwiz. If this is your initial installation, please read the readme file for finishing the installation.
Once installed the steps to go from absolute nothing to a working GTK GUI app are:
1. Create a new folder and cd into it:
mkdir gtktest && cd gtktest
2. Run the CMakeWizard:
cmwiz
3. When asked if you want to generate a GTK project, say yes and accept all defaults. That is it. When it finishes running you will have a working GTK app that you can now extend to meet your needs. The entire session looks like this:
jeff@jeff-gate:~/dev/gtktest$ cmwiz
CMakeWizard 2.2
A CMake Project jumpstart script
This script is published under the GPL.
CMakeWizard will first ask you a few simple questions
and generate a CMake-based project with source in
the current directory. Finally the project will be built.
Answer the following questions or Ctrl-C to abort:
Project name: gtktest
Use GTK 2.0 (n): y
NOTE: This depends on FindGTK2.cmake being in your modules folder to work
Gen sample GTK code? (y):
Should this build a library (y):
Generate a shared library (y):
Library source name: (gtktest.cpp):
Additional include directories:
Use pthreads?
Should this create a test harness (y):
Test harness source (gtktest_main.cpp):
CMakeLists.txt written
– The C compiler identification is GNU
– The CXX compiler identification is GNU
– Check for working C compiler: /usr/bin/gcc
– Check for working C compiler: /usr/bin/gcc — works
– Detecting C compiler ABI info
– Detecting C compiler ABI info - done
– Check for working CXX compiler: /usr/bin/c++
– Check for working CXX compiler: /usr/bin/c++ — works
– Detecting CXX compiler ABI info
– Detecting CXX compiler ABI info - done
– Found GTK2_GTK: /usr/lib/libgtk-x11-2.0.so
– Configuring done
– Generating done
– Build files have been written to: /home/jeff/dev/gtktest
Scanning dependencies of target gtktest
[ 50%] Building CXX object CMakeFiles/gtktest.dir/gtktest.cpp.o
Linking CXX shared library libgtktest.so
[ 50%] Built target gtktest
Scanning dependencies of target gtktest_test
[100%] Building CXX object CMakeFiles/gtktest_test.dir/gtktest_main.cpp.o
Linking CXX executable gtktest_test
[100%] Built target gtktest_test
jeff@jeff-gate:~/dev/gtktest$
Running the generated binary (gtktest_test) yields the following:
Future directions:
As I learn more about Glade and how to automate that process, I will extend/modify this to use a more generic resource model. As-is, the GUI is pretty hard-coded, more a proof that it works than useful in an of itself. Obviously for any of this new functionality to work you need the proper GTK development libraries installed to begin with….
Enjoy and as usual, any feedback is appreciated.


Related Articles
No user responded in this post
Leave A Reply