Wednesday, August 18, 2010

My first usage of GTK!

Today I used the GTK for the first time. So what is GTK? GTK expands to GIMP ToolKit. GTK is the library to build graphical user interfaces. It is called GIMP ToolKit because, originally it was developed for the program GIMP (GNU Image Manipulation Program). Now many programs are using it.

I installed GTK in my ubuntu-9.10 desktop. With build essentials already with my system, I issued the following command at the terminal to install GTK+-2.0:

sudo apt-get install libgtk2.0-dev libgtk2.0-doc devhelp

libgtk2.0-dev installs the GTK library. libgtk2.0-doc and devhelp installs the documents of GTK and the browser for the documents respectively.

I wrote a simple hello world program. The program was written to create a window with a button on it. The button would have the label "Hello, World! Bye...". And once the button is clicked, the application would quit.

Download: helloworld_gtk.c

Compiled the hello world program and built the executable for the same by issuing the following command at the terminal:

gcc helloworld_gtk.c -o helloworld `pkg-config --cflags --libs gtk+-2.0`

Here,
  1. pkg-config is the program that reads the .pc which comes with GTK to determine what compiler switches are needed to compile programs that use GTK.
  2. pkg-config --cflags gtk+-2.0 will output a list of include directories for the compiler to look in
  3. pkg-config --libs gtk+-2.0 will output the list of libraries for the compiler to link with and the directories to find them in.

Monday, February 1, 2010

A study on genericizer in GCC

The need for genericizer in GCC depends on how well GENERIC can be made to match up with the source language that is supported in GCC front-end.

I.e. In GCC, if the parser for a language uses GENERIC as intermediate language representation, then a genericizer is not required. If the parser language uses any other intermediate language representation, then a genericizer is required to convert that intermediate language representation to GENERIC.

Note: Any intermediate language representation that seems appropriate for a language can be used. [Ref: Page 93 of GCC Internals for GCC version 4.4.0].


Hence there are following two cases:
  • Case1: Parser uses GENERIC as intermediate language representation
  • Case2: Parser uses any other intermediate language representation


Case1: Parser uses GENERIC as intermediate language representation


In this case, the language parser uses GENERIC and some language specific trees as intermediate language representation. [Ref: Page 93 of GCC Internals for GCC version 4.4.0]. The Gimplifier (which belongs to 'language-independent portions of the compiler') is called directly for the GENERIC representation generated by the parser.

The language specific trees which is generated along with the GENERIC by the parser would be converted into GIMPLE as follows [Ref: Page 201 of GCC Internals for GCC version 4.4.0]:

  1. Convert the language specific trees to GENERIC. And then provide the GENERIC to the Gimplifier to generate GIMPLE.
  2. Convert the language specific trees to GIMPLE straightly.
The above two tasks would be done by the gimplifier hooks [Ref: Page 199 of GCC Internals for GCC version 4.4.0]. Gimplifier hooks are the one that are used to over-ride the standard functions of Gimplifier. This over-riding of standard functions is required because of language specific trees generated as intermediate represenation by parser.

Example:

For C, the language specific trees are converted into GIMPLE straightly (without converting into GENERIC) by the gimplifier hooks [Ref: Page 201of GCC Internals for GCC version 4.4.0].



Case2: Parser uses any other intermediate language representation



In this case, the language parser uses its own intermediate language representation [Ref: Page 93 of GCC Internals for GCC version 4.4.0]. And for this intermediate language representation to be used by Gimplifier (which belongs to 'language-independent portions of the compiler'), it should be converted to GENERIC. For this we require a 'Genericizer'. Genericizer converts the intermediate language representation generated by the parser to the GENERIC form. Hence the genericizer would be the language dependent portion of the compiler.

Thus generated GENERIC is given to the gimplifier.

Example:

Monday, December 28, 2009

Conditional inclusion of header files

Ram (one of my peers, name changed) complained that he was unable to create an object of class (say clsX).

We have the practise of having our class declarations in header files. So the declaration of the class clsX was in X.h file.

To define an object of class clsX in a soure file, Ram had included the header file X.h in the source file. Despite that he was unable to define the object of class clsX. He got compiler error for the same.

Ram complained Ravi (one of my peers, name changed) that he was unable to create an object of class clsX. Ravi is the one who wrote the class clsX.

Ravi worked on it to provide the following report:
  1. I am not sure about why this problem is occurring, but it seems to be because of some issue with C++ compilers, as mentioned in these two posts I found for the error C2143 -
    1. http://sabbour.wordpress.com/2006/12/30/error-c2143-syntax-error-missing-before/
    2. http://www-subatech.in2p3.fr/~photons/subatech/soft/carnac/CPP-INC-1.shtml
  2. I tried moving the class declaration to the source file, and then removed the header file inclusion. The problem was resolved, which means the contents of the header file 'clsX.h' do not have any syntax error.
  3. I removed the inclusion of the header file 'clsX.h' and instead used a forward declaration as follows, and this solution worked fine.
  4. Please use a forward declaration for the class clsTable, as mentioned in point 3 above, to continue your analysis. I shall look into the problem further to ascertain the root cause.
I decided to have a hand on the problem under discussion. By seeing the compiler error, I got that the declaration of the class clsX didn't reach the source file despite the inclusion of header file X.h.

We use preprocessor directives to conditionally include a header file content into other source file. This is to avoid the duplication of declarations.

On checking the macro that is used along with the preprocessor directives in X.h, I found that the macro used is already defined in some other header file (say Y.h). The header file Y.h is already included in the source file in which X.h is also included. Because of which the macro defined in Y.h eliminated the inclusion of X.h contents.

I.e.

Y.h
 #ifndef _Y_H
 #define _Y_H
 ...
 ...
 #endif

X.h
 #ifndef _Y_H
 #define _Y_H
 declaration of class clsX
 #endif

Z.c
 #include "../Y/Y.h"
 #include "../X/X.h"
 define object of class clsX

Here you can see that the contents of header file X.h (I.e. declaration of class clsX), won't be included. This is because the contents of header file X.h would be included in file Z.c only if the macro _Y_H is not defined before. But macro _Y_H is already defined in Z.c by the inclusion of header file Y.h.

I pointed out the mistake of using the same macro for two header files which solved the problem.