Archive

Archive for April, 2014

FLTK – Hello World

April 2, 2014 1 comment

Currently reading Programming Principles and Practise Using C++ book.

http://www.amazon.com/Programming-Principles-Practice-Using-C/dp/0321543726/ref=cm_cr_pr_product_top/178-2816475-1187760

Faced an issue setting up FLTK (Fast Light Toolkit) on debian.

The installation steps are straight forward as per the README.Unix.txt in the download from www.fltk.org.

#include <FL/Fl.H> 
#include <FL/Fl_Box.H>
#include <FL/Fl_Window.H>

int main()
{
    Fl_Window window(200, 200, "Window title");
    Fl_Box box(0, 0, 200, 200, "Hey, I mean, Hello World!");
    window.show();
    return Fl::run();
}

When I tried to compile it. I got the following errors. This was due to the g++ not knowing where to get the fltk files from.

$ g++ hello_fltk.cpp -o hello_fltk
/tmp/cccdL9E5.o: In function `main’:
hello_fltk.cpp:(.text+0x2d): undefined reference to `Fl_Window::Fl_Window(int, int, char const*)’
hello_fltk.cpp:(.text+0x61): undefined reference to `Fl_Box::Fl_Box(int, int, int, int, char const*)’
hello_fltk.cpp:(.text+0x6d): undefined reference to `Fl_Window::show()’
hello_fltk.cpp:(.text+0x72): undefined reference to `Fl::run()’
hello_fltk.cpp:(.text+0x8c): undefined reference to `Fl_Window::~Fl_Window()’
hello_fltk.cpp:(.text+0xae): undefined reference to `Fl_Window::~Fl_Window()’
/tmp/cccdL9E5.o: In function `Fl_Box::~Fl_Box()’:
hello_fltk.cpp:(.text._ZN6Fl_BoxD2Ev[_ZN6Fl_BoxD5Ev]+0xb): undefined reference to `vtable for Fl_Box’
hello_fltk.cpp:(.text._ZN6Fl_BoxD2Ev[_ZN6Fl_BoxD5Ev]+0x16): undefined reference to `Fl_Widget::~Fl_Widget()’
collect2: error: ld returned 1 exit status

To compile run

$ fltk-config --compile hello_fltk.cpp
g++ -I/usr/local/include -I/usr/local/include/FL/images -I/usr/include/freetype2 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -D_THREAD_SAFE -D_REENTRANT -o 'hello_fltk' 'hello_fltk.cpp' /usr/local/lib/libfltk.a -lXext -lXft -lfontconfig -lpthread -ldl -lm -lX11

From the README

FLTK provides a neat script named “fltk-config” that can provide all the flags
needed to build FLTK applications using the same flags that were used to build
the library itself. Running “fltk-config” without arguments will print a list
options. The easiest call to compile an FLTK application from a single source
file is:

fltk-config –compile myProgram.cxx

This shows you the correct way to compile using g++. On running the file you get the simple window below.

$./hello_fltk

hello_fltk

Useful links:
http://www.stroustrup.com/Programming/
http://seriss.com/people/erco/fltk/

NB.
The book has the header files as

#include <FL/Fl.h> 
#include <FL/Fl_Box.h>
#include <FL/Fl_Window.h>

But the files are saved with .H and will generate an error like the one below.

hello_fltk.cpp:1:19: fatal error: FL/Fl.h: No such file or directory

Advertisements
Categories: cpp Tags: ,