2009-12-12

GObject - First Vala Programming


svn checkout http://gobject-learing.googlecode.com/svn/trunk/ gobject-learing-read-only


為達到最佳效果,請使用以上指令下載範例碼並與以下投影片一起服用。

Coppy from wiki:Vala "Vala is a programming language that was created with the goal of bringing modern language features to C, without additional runtime requirements and with little overhead, by targeting the GObject object system. It was developed by Jürg Billeter and Raffaele Sandrini. The syntax borrows heavily from C#. Rather than being compiled directly to assembler or to an intermediate language, Vala is compiled to C which is then compiled with the platform's standard C compiler."

According above description, any program written by Vala could translate to standard C code. So I add this topic between GObject tutorial. Because it really save our time for development.

I rewrote the sample previous GObject example by using Vala. You will find those boring redundant code are no longer need to self-written.


/* 6-1 - maman-bar.vala */
public class MamanBar : Object {
public int a;
public int b;

public MamanBar(){
a = 1; b = 2;
}
public void dumpall() {
stdout.printf ("a = %d b = %d\n",a,b);
}

}



#> valac -o maman-bar-vala maman-bar.vala main.vala
#> ./maman-bar-vala
a = 1 b = 2


I would like to demo how to connect vala and c together. We use the same C code from previous sample.

int main (int argc, char *argv[])
{
/* this function should be executed first. Before everything */
g_type_init();

/* Create our object */
MamanBar *bar = maman_bar_new();
maman_bar_dumpall( bar );

return 0;
}

The only difference are compile commands.

#> valac -C -H maman-bar.h maman-bar.vala
#> cc `pkg-config --cflags glib-2.0 gtk+-2.0` -c -o main.o main.c
#> cc `pkg-config --cflags glib-2.0 gtk+-2.0` -c -o maman-bar.o maman-bar.c
#> cc -o maman-bar-c main.o maman-bar.o `pkg-config --libs glib-2.0 gtk+-2.0`
#> ./maman-bar-c
a = 1 b = 2

沒有留言:

張貼留言