PDA

View Full Version : Glib- How to run this programme???



Praveen30
August 31st, 2011, 09:42 AM
i am learning how to use glib with c language.And this is my first programme.i have installed libglib-dev from synaptic,it is under /usr/include/glib-2.0. but i don't know how to run it..?? help please!!



//compile.c
#include <glib.h>
int main(int argc, char** argv)
{
GList* list=NULL;
list = g_list_append(list,"Hello World!");
printf("The first item is '%s'\n", g_list_first(list)->data);
return 0;
}

Smart Viking
August 31st, 2011, 09:57 AM
Should include stdio.h.


//compile.c
#include <glib.h>
#include <stdio.h>
int main(int argc, char** argv)
{
GList* list=NULL;
list = g_list_append(list,"Hello World!");
printf("The first item is %s\n", g_list_first(list)->data);
return 0;
}


Compile with


$ gcc `pkg-config --cflags --libs glib-2.0` omg.c -o hello

Tony Flury
August 31st, 2011, 10:58 AM
and run it with :


./hello

Assuming you have not changed directory since you compiled it.

Praveen30
August 31st, 2011, 03:00 PM
Should include stdio.h.


//compile.c
#include <glib.h>
#include <stdio.h>
int main(int argc, char** argv)
{
GList* list=NULL;
list = g_list_append(list,"Hello World!");
printf("The first item is %s\n", g_list_first(list)->data);
return 0;
}

Compile with


$ gcc `pkg-config --cflags --libs glib-2.0` omg.c -o hello


I thought %s is fine here, but i am getting an error here-
compile.c: In function ‘main’:
compile.c:7: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘gpointer’
???

JupiterV2
August 31st, 2011, 03:07 PM
I thought %s is fine here, but i am getting an error here-
compile.c: In function ‘main’:
compile.c:7: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘gpointer’
???

The warning is correct. Glib uses a generic pointer (void *) typedef'd as gpointer. This allows Glib containers like the g_list to contain any kind of data. You should always cast your store and your result to the proper pointer type to silence warnings. Glib also provides a set of macros to convert between basic types like: GINT_TO_POINTER and GPOINTER_TO_INT that cast the result for you. They may even implement simple tests to validate the data but I'm not sure on that.

Smart Viking
August 31st, 2011, 03:10 PM
Are you doing exactly the same as me? It works fine for me.

If what you're doing it just a little different, that's obviously critical.

EDIT: Looks like JupiterV2 got the answers.

Bachstelze
August 31st, 2011, 03:11 PM
Are you doing exactly the same as me? It works fine for me.

If what you're doing it just a little different, that's obviously critical.

You never said which compiler/OS you were using. Don't assume everyone is running the same as you.

Smart Viking
August 31st, 2011, 03:15 PM
You never said which compiler/OS you were using. Don't assume everyone is running the same as you.
You're right, I shouln't. I'm probably using an older version of gcc, which doesn't have such a warning present.
I'm using 64Bit gcc-4.4, on Debian.

Praveen30
August 31st, 2011, 03:20 PM
You never said which compiler/OS you were using. Don't assume everyone is running the same as you.

Mine is gcc/Ubuntu-32bit.

Praveen30
August 31st, 2011, 03:26 PM
The warning is correct. Glib uses a generic pointer (void *) typedef'd as gpointer. This allows Glib containers like the g_list to contain any kind of data. You should always cast your store and your result to the proper pointer type to silence warnings. Glib also provides a set of macros to convert between basic types like: GINT_TO_POINTER and GPOINTER_TO_INT that cast the result for you. They may even implement simple tests to validate the data but I'm not sure on that.

thanks for your information but it is a little bit complex for me to understand right now..can you tell me how to cast it so that i can run it properly???

JupiterV2
August 31st, 2011, 03:39 PM
Casts/Changes have been typed in bold.


#include <glib.h>
#include <stdio.h>
int main(int argc, char** argv)
{
GList* list=NULL;
list = g_list_append(list, (gpointer) "Hello World!");
printf("The first item is %s\n", (char *) g_list_first(list)->data);
return 0;
}

Also see: glib-Type-Conversion-Macros (http://developer.gnome.org/glib/2.28/glib-Type-Conversion-Macros.html)

Praveen30
August 31st, 2011, 04:00 PM
Casts/Changes have been typed in bold.


#include <glib.h>
#include <stdio.h>
int main(int argc, char** argv)
{
GList* list=NULL;
list = g_list_append(list, (gpointer) "Hello World!");
printf("The first item is %s\n", (char *) g_list_first(list)->data);
return 0;
}Also see: glib-Type-Conversion-Macros (http://developer.gnome.org/glib/2.28/glib-Type-Conversion-Macros.html)

thanks!!!