Tutorial: How to write cross-platform GUIs using wxWidgets

last updated in Categories C Programming, Linux, Open source coding, OS X, UNIX, Windows
wxWidgets logo

Windows and X widgets (wxWidgets) is an open source, cross-platform widget toolkit; that is, a library of basic elements for building a graphical user interface (GUI)

wxWidgets lets developers create applications for Win32, Mac OS X, GTK+, X11, Motif, WinCE, and more using one codebase. It can be used from languages such as C++, Python, Perl, and C#/.NET. Unlike other cross-platform toolkits, wxWidgets applications look and feel native. This is because wxWidgets uses the platform’s own native controls rather than emulating them. It’s also extensive, free, open-source, and mature.

With this tutorial you will learn how to use the wxWidgets toolkit to create elegant and highly useful GUIs in your programming language of choice.

But why use wxWidgets? Because you want to be able to write a GUI quickly and easily that runs across platforms. You also want to be able to use the programming language of your choice, and you want your GUI looks cool or same on all platform.

Personally, I prefer to use QT toolkit. But I will give a shot to wxWidgets 😀

Linux: Boost application performance using asynchronous I/O

last updated in Categories C Programming, Linux

Learn when and how to use the POSIX AIO API under Linux to boost application performance.

Linux asynchronous I/O is a part of Linux kernel 2.6.. The basic idea behind AIO is to allow a process to initiate a number of I/O operations without having to block or wait for any to complete. At some later time, or after being notified of I/O completion, the process can retrieve the results of the I/O.

FTA, “The most common input/output (I/O) model used in Linux is synchronous I/O. After a request is made in this model, the application blocks until the request is satisfied. This is a great paradigm because the calling application requires no central processing unit (CPU) while it awaits the completion of the I/O request. But in some cases there’s a need to overlap an I/O request with other processing. The Portable Operating System Interface (POSIX) asynchronous I/O (AIO) application program interface (API) provides this capability. In this article, get an overview of the API and see how to use it.”

Read more at IBM developerworks

Examining gcc behavior

last updated in Categories C Programming

Recently my friend highlighted something that can be use to examining gcc complier behavior.

You can pass following options to gcc

  • -v : Display the commands executed to run the stages of compilation on screen.
  • -x language : I am using C
  • /dev/null : Use /dev/null as input file

For example when I type the following command:

gcc -v -x c /dev/null


Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --enable-languages=c,c++,java,f95,objc,ada,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --program-suffix=-4.0 --enable-__cxa_atexit --enable-clocale=gnu --enable-libstdcxx-debug --enable-java-awt=gtk-default --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-4.0- --enable-mpfr --disable-werror --with-tune=i686 --enable-checking=release i486-linux-gnu
Thread model: posix
gcc version 4.0.4 20060507 (prerelease) (Debian 4.0.3-3)
 /usr/lib/gcc/i486-linux-gnu/4.0.4/cc1 -quiet -v /dev/null -quiet -dumpbase null -mtune=i686 -auxbase null -version -o /tmp/ccMVab5b.s
ignoring nonexistent directory "/usr/local/include/i486-linux-gnu"
ignoring nonexistent directory "/usr/include/i486-linux-gnu"
#include "..." search starts here:
#include <...> search starts here:
End of search list.
GNU C version 4.0.4 20060507 (prerelease) (Debian 4.0.3-3) (i486-linux-gnu)
        compiled by GNU C version 4.0.4 20060507 (prerelease) (Debian 4.0.3-3).
GGC heuristics: --param ggc-min-expand=81 --param ggc-min-heapsize=96075
 as -V -Qy --32 -o /tmp/ccyCAfmi.o /tmp/ccMVab5b.s
GNU assembler version 2.16.91 (i486-linux-gnu) using BFD version 2.16.91 20060413 Debian GNU/Linux
 /usr/lib/gcc/i486-linux-gnu/4.0.4/collect2 --eh-frame-hdr -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 /usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../lib/crt1.o /usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../lib/crti.o /usr/lib/gcc/i486-linux-gnu/4.0.4/crtbegin.o -L/usr/lib/gcc/i486-linux-gnu/4.0.4 -L/usr/lib/gcc/i486-linux-gnu/4.0.4 -L/usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../lib -L/usr/lib/gcc/i486-linux-gnu/4.0.4/../../.. -L/lib/../lib -L/usr/lib/../lib /tmp/ccyCAfmi.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/i486-linux-gnu/4.0.4/crtend.o /usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../lib/crtn.o
/usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../lib/crt1.o: In function `_start':
../sysdeps/i386/elf/start.S:115: undefined reference to `main'
collect2: ld returned 1 exit status

strace and the GDB Debugger mini howto

last updated in Categories C Programming, Howto, Solaris

IBM Developer works has published a simple howto/guide that will gain you a quality knowledge while exploring your UNIX system.

FTA, “…Programming a UNIX system can be fun as well as educational. With the UNIX strace tool and GDB, the GNU Project Debugger, you can really dig deep into the functionality of your system and learn a lot about the various programs that comprise it. Using both tools in concert can be a rewarding experience as you look under the hood of your UNIX machine.

The UNIX family has always provided abundantly for its users. UNIX is a treasure chest of tools with which you can not only do productive work but also educate and entertain yourself as you explore the depths of the operating system. Two useful tools for this purpose are strace, with which you can trace the system calls of any program, and the GDB Debugger, which is a full-featured debugger that allows you to run programs in a controlled environment…”.

Read more

Programming C: Find out name of a terminal

last updated in Categories C Programming

Recently I was writing a small experimental program under GNU/Linux. My tiny application needed input from terminal.

I need to find out name of a terminal. Further, I need to know if a particular file descriptor is a tty device or not.

I found ttyname() function which accepts an open file descriptor as its input argument and returns a pointer to the null-terminated pathname of the terminal device.

But how did I know if descriptor refer to a terminal is tty? Simply use C function isatty(desc) which returns 1 if desc is an open descriptor connected to a terminal and 0 else.

Desc can be any one of the following:

  • 0 : Standard input (like keyboard)
  • 1 : Standard output (like screen)
  • 2 : Standard error (like screen)

Under normal circumstances every Linux program has three streams opened for it when it starts up, one for input, one for output, and one for printing diagnostic or error messages. These are typically attached to the user’s terminal but might instead refer to files or other devices, depending on what the parent process chose to set up.

On program startup, the integer file descriptors associated with the streams stdin, stdout, and stderr are 0, 1, and 2, respectively.

Here is sample C guesstty.c program listing:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv){
 int t,fd;
 if ( argc == 1 ){
   printf("Syntax: %s 0,1,2\n",argv[0]);
 fd = atoi(argv[1]);
 t = isatty(fd);

 if( t )
   printf("fd is %d, which is a tty\n",fd);
   printf("fd is %d, which isn't a tty\n",fd);

 if ( t == 1 ) printf("tty name is %s\n",ttyname(t));


The above code can tell when the standard input is redirected to take data from a file or when the data is coming from terminal.

Compile and run program:
[code]$ make guesstty.c[/code]
[code]$ cc guesstty.c -o guesstty [/code]
Sample run:
[code]$ ./guesstty 0[/code]Output:

fd is 0, which is a tty

tty name is /dev/pts/1
[code]$ ./guesstty 0 fd is 0, which isn’t a tty


  • ttyname(3)
  • isatty(3)

Linux: How to compile program

last updated in Categories C Programming, Howto, Linux

Many newbies find it difficult to compiling programs under Linux (command mentioned below should also work with FreeBSD or any other UNIX like system). The fact that Linux is open source OS, so most of the programs are also open sources. Improvements and security, bug patches needs to us compile not just Kernel but softwares/programs. Please note that you can download program with wget and extract the file with tar command.

If file extensions is .gz then use tar command as follows to extract tar ball:
$ tar -zxvf file.tar.gz

If file extensions is .bz2 then use tar command as follows to extract tar ball:
$ tar -jxvf file.tar.bz2

Three common steps to compiling program under Linux:

Step # 1: Configure (read as prepare) the program for compile on your system/architecture.

Generally configure command is used:
$ ./configure

Step # 2: Compiling the program
Use make command to compiling program:
$ make

Step # 3: Install the program
First become a root user:
$ su -


Next install the software with following command:
# make install