Difference between revisions of "CMakeForFLTK"

From KitwarePublic
Jump to navigationJump to search
(Remove leading space rectangles from preformatted blocks)
(Replace content with link to new CMake community wiki)
Line 1: Line 1:
==Using CMake to build an FLTK application==

This page has moved [https://gitlab.kitware.com/cmake/community/wikis/doc/tutorials/ForFLTK here].
The Fast Light Tool Kit ("FLTK", pronounced "fulltick") is a cross-platform C++ GUI toolkit for UNIX®/Linux® (X11), Microsoft® Windows®, and MacOS® X. It is available from [http://www.fltk.org http://www.fltk.org].
In addition to the C++ programming interface, FLTK provides the Fast Light User Interface Designer, or FLUID, which is a graphical editor that is used to produce FLTK source code. FLUID edits and saves its state in .fl files. These files are text, and you can (with care) edit them in a text editor, perhaps to get some special effects.
The programs that I have written using FLTK have been personal projects, intended only for my own use on my Linux system, and I have only ever needed simple hand-crafted Makefiles to build them. However, one project looks like it might have a wider audience, but I have little experience with, or access to, the Windows® or MacOS® programming environments. Therefore I started looking for a cross-platform build environment to simplify the task and avoid potential problems. I had experimented with the GNU autotools suite (autoconf, automake, libtool) and was making slow progress, and then CMake came up in the conversation and I decided to investigate further.
After poring over the CMake documentation, and searching the Internet for clues, I eventually managed to glean enough nuggets of useful information to get the build system in place, for Linux at least. What surprised me was that if you distill down all of the different snippets, the required configuration is very short! This page is intended to help others in the same situation by collecting it all together.
===Code Hierarchy===
The main application code lives in <code>foo.cxx</code>. The FLTK callback functions live in <code>callbacks.cxx</code> and were created using a standard editor. The user interface layout is defined in <code>ui.fl</code> and was created using FLUID. The corresponding C++ code could have been written from within the interactive FLUID session, but regenerating it using the command line ensures that everything is consistent. The following simple Makefile shows the build process and dependencies:
    FLTK_CXXFLAGS = `fltk-config --cxxflags`
    FLTK_LDFLAGS = `fltk-config --ldstaticflags`
    foo:    foo.o ui.o callbacks.o
        g++ -o foo foo.o ui.o callbacks.o $(FLTK_LDFLAGS)
    foo.o:  foo.cxx ui.h
        g++ -c $(FLTK_CXXFLAGS) foo.cxx
    callbacks.o:    callbacks.cxx ui.h
        g++ -c $(FLTK_CXXFLAGS) callbacks.cxx
    ui.o:  ui.cxx ui.h
        g++ -c $(FLTK_CXXFLAGS) ui.cxx
    ui.cxx ui.h:    ui.fl
        fluid -c ui.fl
===CMake configuration===
    FLTK_WRAP_UI(Ui ui.fl)
    ADD_LIBRARY(Ui callbacks.cxx ${Ui_FLTK_UI_SRCS})
    ADD_EXECUTABLE(foo foo.cxx)
* <code>CMAKE_MINIMUM_REQUIRED</code> - you could omit this line, but CMake will keep bugging you about it so you might as well include it!
* <code>FIND_PACKAGE(FLTK REQUIRED)</code> - if you search the Internet you will find a bug report where this fails because it is looking for the include file <code>FL/Fl.h</code> instead of <code>FL/Fl.H</code>, but as this works on my Linux box it is clearly fixed.
* <code>FIND_PACKAGE(OpenGL REQUIRED)</code> - the CMake scripting language is not case-sensitive, so you can also write <code>find_package</code> but the names of the things that you are looking for are case-sensitive. This can be confusing, especially when the CMake variables generated change case (e.g. <code>${OPENFL_LIBRARIES}</code>).
* <code>FLTK_WRAP_UI(Ui ui.fl)</code> - the first parameter is used as the prefix of a <code>*_FLTK_UI_SRCS</code> variable name that will contain the names of the C++ files corresponding to the FLUID files given in the remaining parameters. In this case <code>${Ui_FLTK_UI_SRCS}</code> will contain <code>ui.cxx</code> and <code>ui.h</code>. Now that I am adding these notes, maybe it would have been better if I had chosen something a little less confusing than <code>Ui</code> for this example.
* <code>ADD_LIBRARY(Ui callbacks.cxx ${Ui_FLTK_UI_SRCS})</code> - the first parameter defines the name of a library target and the remaining parameters define the sources that are used to build that target. Note the use of the <code>${Ui_FLTK_UI_SRCS}</code> variable defined by the previous step. The library target is called <code>libUi.a</code> on my system.
* <code>ADD_EXECUTABLE(foo foo.cxx)</code> - the first parameter defines the name of an executable target, and the remaining parameters define the non-library sources that are needed for that target.
* <code>ADD_DEPENDENCIES(foo Ui)</code> - tells CMake that the <code>foo</code> target depends on the <code>Ui</code> target, and therefore that <code>foo</code> must be built after <code>Ui</code>.
* <code>TARGET_LINK_LIBRARIES(foo Ui)</code> - tells CMake that <code>foo</code> needs to be linked against the <code>Ui</code> library target.
* <code>TARGET_LINK_LIBRARIES(foo ${FLTK_LIBRARIES})</code> - tells CMake that <code>foo</code> needs to be linked against the libraries in the <code>${FLTK_LIBRARIES}</code> variable. Where does this variable come from? It is defined as part of the <code>FIND_PACKAGE(FLTK REQUIRED)</code> call, but you can only find this out by looking at the <code>Modules/FindFLTK.cmake</code> that comes with the CMake installation, or by browsing the installed CMake documentation.
* <code>TARGET_LINK_LIBRARIES(foo ${OPENGL_LIBRARIES})</code> - see above. Required because my installation of FLTK needs to be linked against OpenGL.
This was my first exposure to CMake and took a bit of trial and error over the course of a weekend. If anyone has any comments on how to improve this configuration, or how to adapt it for non-Linux platforms, please let me know, or add a section to this page.

Latest revision as of 15:41, 30 April 2018

The CMake community Wiki has moved to the Kitware GitLab Instance.

This page has moved here.