FLTK – Hello World

April 2, 2014 Leave a 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+0×61): 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+0×72): 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]+0×16): 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: ,

Genarating summary data with awk

March 31, 2014 Leave a comment

Problem:
A collegue asked me to do this. Generating a summary based on the week numbers.

File input

$ less file1.txt
2013-03-01, Week 1, 20
2013-03-05, Week 1, 30
2013-03-04, Week 1, 20
2013-03-17, Week 1, 30
2013-03-19, Week 1, 40
2013-03-21, Week 1, 50
2013-03-01, Week 2, 20
2013-03-05, Week 2, 30
2013-03-04, Week 2, 20
2013-03-17, Week 2, 30
2013-03-19, Week 2, 40

File output

2013-03-01, 20, Week 1
2013-03-04, 20, Week 1
2013-03-05, 30, Week 1
2013-03-17, 30, Week 1
2013-03-19, 40, Week 1
2013-03-21, 50, Week 1
Total,190, Week 1
2013-03-01, 20, Week 2
2013-03-04, 20, Week 2
2013-03-05, 30, Week 2
2013-03-17, 30, Week 2
2013-03-19, 40, Week 2
Total,140, Week 2

Solution:
Using awk.

$ awk -v FS="," -v OFS="," '{sum[$2]+= $3; print $1, $3, $2}
END {for (var in sum){print "Total", sum[var], var}}' file1.txt | sort -t, -k3
2013-03-01, 20, Week 1
2013-03-04, 20, Week 1
2013-03-05, 30, Week 1
2013-03-17, 30, Week 1
2013-03-19, 40, Week 1
2013-03-21, 50, Week 1
Total,190, Week 1
2013-03-01, 20, Week 2
2013-03-04, 20, Week 2
2013-03-05, 30, Week 2
2013-03-17, 30, Week 2
2013-03-19, 40, Week 2
Total,140, Week 2

Source:

http://www.unix.com/shell-programming-scripting/245209-generating-summary-data-use-awk.html

Categories: awk Tags:

JLSN – incron, inotifywait

March 30, 2014 Leave a comment

Problem:
Needed to run a script every time a file was placed in a certain folder. So you want the script triggered when a file is created or deleted.

Solution:
To run in a shell install inotify-tools, you will get inotifywait. But if you want a daemon that runs and calls a script or performs an action then install incron. YMMV.

Read more about incron:
http://zipizap.wordpress.com/2013/11/15/incron/
http://www.howtoforge.com/triggering-commands-on-file-or-directory-changes-with-incron

Delete specific line numbers in a file – awk, sed

March 14, 2014 Leave a comment

Problem:
You want to delete certain lines in a file. Say 3,6,9,12 etc.

$ less file
first
second
third
fourth
fifth
sixth
seventh
eigth
nineth
tenth

Solution:
1. Using awk.

$ awk 'NR%3' file
first
second
fourth
fifth
seventh
eigth
tenth

My understanding of how it works.

$ man awk

NR The total number of input records seen so far.

$ awk 'NR' file 

Will print all the rows as NR returns a value which equates to true and thus gets printed.
If you want to only print the x row

$ awk 'NR == 2' file
second
$ awk 'NR == 5' file
fifth

The below example prints nothing. It is like saying print but the result is false so nothing gets printed.

$ awk '0' file

So what happens with NR%3 is that anything that is not 0 gets printed.
1%3 result is 1 it gets printed.
2%3 result is 2 it gets printed.
3%3 result is 0 it does not get printed.
4%3 result is 1 it gets printed.
and so on.

2. Using sed.

$ sed 'n;n;d' file
first
second
fourth
fifth
seventh
eigth
tenth

How I think it works.

$ sed 'n;n;d' file

n – move to the next line
n – move to the next line
d – delete that line.

I think that due to d repeating the next cycle, since we still have lines in the file, it starts over and repeats the same till EOF.

$ info sed

`n’
If auto-print is not disabled, print the pattern space, then,
regardless, replace the pattern space with the next line of input.
If there is no more input then `sed’ exits without processing any
more commands.

`d’
Delete the pattern space; immediately start next cycle.

NB: File must not contain blank lines.

Source:
http://www.unix.com/shell-programming-scripting/245088-how-delete-line-number-3-6-9-12-15-so.html

Categories: awk, Interesting, sed Tags:

Pidgin and Google Talk

February 5, 2014 Leave a comment

Problem:
Trying to add a google talk account on pidgin. As I prefer to use my mail client to look at my emails and not the browser.

Solution:
This took up quite a bit of my morning :-(

1. I installed pidgin via synaptic.
2. Followed instructions from google support, https://support.google.com/a/answer/49147?hl=en which did not work.
3. Made the following changes that finally worked.

Connection Security : “Use old-style SSL”
Connect port : 5223
Connect sever : talk.google.com

Source:
http://askubuntu.com/questions/264623/pidgin-unable-to-connect-to-gtalk

Categories: Interesting Tags: ,

Interesting – Vimbits

February 4, 2014 Leave a comment

Vimbits

A vimbit is a snippet of a .vimrc. Share your coolest trick, mapping, setting, or custom command for the Vim editor. Find new bits and vote up the best ones.

Looks like a really cool site. Just posting this here as a reminder to look at this again.

Simple HTTP Server with Python

January 20, 2014 Leave a comment

A quick way to share some files locally.

$ cd /home/somedir
$ python -m SimpleHTTPServer

Source:
http://www.linuxjournal.com/content/tech-tip-really-simple-http-server-python

Categories: Interesting, python Tags:
Follow

Get every new post delivered to your Inbox.

Join 32 other followers