Archive

Archive for the ‘cpp’ Category

FLTK – Hello World

April 2, 2014 1 comment

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

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

Categories: cpp Tags: ,

JLSN – Unit Testing in C++ using boost

July 16, 2013 Leave a comment

Problem: How does unit testing work in C++.

Solution:
Boost.

The function I wanted to test was placed in a header file.

// myfunctions.h
int add(int i, int j)
{
    return i + j;
}

This is the code that does the actual testing.

// mytest.cpp
#define BOOST_TEST_MODULE integertest
#include <boost/test/included/unit_test.hpp>
#include "myfunctions.h"

// name of the test suite is integertest
BOOST_AUTO_TEST_SUITE (integertest) 

BOOST_AUTO_TEST_CASE (test1)
{
    BOOST_CHECK(add(2, 2) == 5); 
}

/*
BOOST_AUTO_TEST_CASE (test2)
{
    BOOST_CHECK(add(2, 2) == 4); 
}*/

BOOST_AUTO_TEST_SUITE_END( )

To run the test.

$ g++ -o mytest mytest.cpp 
$ ./mytest 
Running 1 test case...
mytest.cpp(9): error in "test1": check add(2, 2) == 5 failed

*** 1 failure detected in test suite "integertest"
$ 

After changing the test.

$ g++ -o mytest mytest.cpp 
$ ./mytest 
Running 1 test case...

*** No errors detected
$

Source:
http://www.ibm.com/developerworks/aix/library/au-ctools1_boost/
www.boost.org

Categories: cpp Tags: , ,