using glib library in c program - c

I want to use hash tables in my c program.
I code:
...
#include <glib.h>
void main(int argc, char **argv)
{
GHashTable *g_hash_table;
...
g_hash_table = g_hash_table_new(g_int_hash, g_int_equal);
...
}
Then I compile:
$ gcc -I/usr/include/glib-2.0
-I/usr/lib/i386-linux-gnu/glib-2.0/include
-lglib-2.0 -o test test.c
or the same command:
$ gcc `pkg-config --cflags --libs glib-2.0` -o test test.c
but anyway the result is:
test.c: underfined reference to `g_int_equal`
test.c: underfined reference to `g_int_hash`
test.c: underfined reference to `g_hash_table_new`
collect2: ld returned 1 exit status
Why I cant compile my program? I do wrong include of glib library?

You need to specify libraries in the command line after the source and object files that use them:
gcc test.c `pkg-config --cflags --libs glib-2.0` -o test

From this pdf at IBM developper works, it's better to use the pkg-config if you have a standard install of glib with this command :
$ gcc `pkg-config --cflags --libs glib-2.0` -o ex-compile ex-compile.c
Your include look right and the way you are using it to. Not sure the ' will change anything but you might want to check the PDF, it contains a lot of examples and explainations.

Related

SDL2 with C undefined reference to `IMG_Load'

I am programming in C using SDL2 and I am getting this error:
`main': main.c:(.text+0x2d0): undefined reference to 'IMG_Load'`
I have these includes:
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdio.h>
For reference I am using Arch Linux and using GCC with this command to compile:
gcc main.c `sdl2-config --libs -lSDL2 -lSDL2_image` -o game
I've looked all over for solutions but none of them seem to rectify the issue.
This:
gcc main.c `sdl2-config --libs -lSDL2 -lSDL2_image` -o game
should be:
gcc main.c `sdl2-config --libs` -lSDL2 -lSDL2_image -o game
In other words, only sdl2-config --libs is the subcommand.

C + GTK + WEBKIT program does not compile: fatal error: webkit2/webkit2.h: No such file or directory

On a Linux Mint 64-bit PC
This C program does not compile:
#include <gtk/gtk.h>
#include <webkit2/webkit2.h>
int main(int argc, char* argv[])
{
gtk_init(&argc, &argv);
return 0;
}
gcc `pkg-config --cflags gtk+-3.0` -o hello hello.c `pkg-config --libs gtk+-3.0`
The compile error message is: hello.c:2:29: fatal error: webkit2/webkit2.h: No such file or directory
I've installed every webkit library I can find and I continue to get this error message!
I have searched and found on my Linux Mint PC:
/usr/include/webkitgtk-4.0/webkit2/webkit2.h
I think the gcc compile command does not include info about webkit2.
I would recommend you to add some include folders explicitely.
For example, in the case of webkit2
gcc -I
Try to find it ith find or locate if the db is updated.
The following gcc compile command seems to work:
gcc `pkg-config --cflags gtk+-3.0 webkit2gtk-4.0` -o hello hello.c `pkg-config --libs gtk+-3.0 webkit2gtk-4.0`

Error compiling with GLib

I'm pretty new to C programming, and am trying to work through the exercises in '21st Century C' second edition. I'm stuck on page 202, Example 9-7, unicode.c. This example starts with:
#include <glib.h>
#include <locale.h> //setlocale
#include "string_utilities.h"
#include "stopif.h"
//Frees instring for you--we can't use it for anything else.
char *localstring_to_utf8(char *instring){
GError *e=NULL;
setlocale(LC_ALL, ""); //get the OS's locale.
char *out = g_locale_to_utf8(instring, -1, NULL, NULL, &e);
free(instring); //done with the original
Stopif(!out, return NULL, "Trouble converting from your locale to UTF-8.");
Stopif(!g_utf8_validate(out, -1, NULL), free(out); return NULL,
"Trouble: I couldn't convert your file to a valid UTF-8 string.");
return out;
}
When I try to compile it with:
c99 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -Wall -O3 -lglib-2.0 unicode.c string_utilities.o -o unicode
I get errors such as:
$ c99 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -Wall -O3 -lglib-2.0 unicode.c string_utilities.o -o unicode
/tmp/ccBDQFiH.o: In function `localstring_to_utf8':
/home/kevin/21st_Century_C/ch09/unicode.c:29: undefined reference to `g_locale_to_utf8'
/home/kevin/21st_Century_C/ch09/unicode.c:32: undefined reference to `g_utf8_validate'
/tmp/ccBDQFiH.o: In function `main':
/home/kevin/21st_Century_C/ch09/unicode.c:48: undefined reference to `g_utf8_strlen'
This seems to indicate that the Glib library is not found, but the compiler didn't complain about this, and the Glib libraries and include files are right where I specified on the command line. I've installed the libglib2.0-dev package in addition to the libglib2.0 package (all installed with 'sudo apt-get ..'). 'pkg-config' seems to find glib-2.0 just fine.
This is all on a Ubuntu 14.04.2 system.
I can't figure out how to correct this error, and don't understand why it can't find the specific Glib functions, if it finds the glib include and lib files.
The order of things in the command line matter. In general it should be something like:
gcc [options] [source files] [object files] [-L stuff] [-lstuff] [-o outputfile]
So give this a whirl instead:
gcc -g -Wall -O3 -std=gnu11 `pkg-config --cflags glib-2.0` \
unicode.c string_utilities.o `pkg-config --libs glib-2.0` \
-o unicode
This is also covered in the Compiling GLib Applications section of the GLib Reference Manual:
$ cc hello.c `pkg-config --cflags --libs glib-2.0` -o hello

Trouble linking to gobject-introspection library

I have the file blah.c containing:
#include <gobject-introspection-1.0/girepository.h>
int main(int argc,char **argv) {
GIRepository *gir = g_irepository_get_default();
return 0;
}
This, of course, is simpler than the file I wish to compile, but has the same problem. I compile with
gcc `pkg-config --cflags --libs gobject-introspection-1.0` blah.c
and get the linking error:
/tmp/cck88oj4.o: In function `main':
blah.c:(.text+0x10): undefined reference to `g_irepository_get_default'
collect2: ld returned 1 exit status
The pkg-config command returns
-pthread -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/gobject-introspection-1.0 -Wl,--export-dynamic -pthread -lgirepository-1.0 -lgobject-2.0 -lgmodule-2.0 -lffi -lgthread-2.0 -lrt -lglib-2.0
and the files libgirepository-1.0.a and libgirepository-1.0.so are both present in /usr/lib.
What is causing this linking error?
The reason for the linking error could be because the linker is linking with -Wl, as-needed option by default. When this option is used, the libraries are not linked until symbol in the library is encountered. In your current case as the source file is added in the end, none of the symbols in the libraries are encountered, thus linking is not done. You could try:
gcc blah.c `pkg-config --cflags --libs gobject-introspection-1.0`
or
gcc -Wl,-no-as-needed `pkg-config --cflags --libs gobject-introspection-1.0` blah.c
Hope this helps!

how to compile a program with gtkmozembed.h

i have written a program under ubuntu, in which i include gtkmozembed.h. I am facing a problem in compiling the program.Below is the simplest form of a program which uses gtkmozembed.
#include <gtk/gtk.h>
#include <stdio.h>
#include <gtkmozembed.h>
int main(){
GtkWidget *mozEmbed;
mozEmbed = gtk_moz_embed_new();
return 0;
}
Eventhough, the above program is doing nothing, compiling that program is a lot for me...
I am trying to comile the above program like below
gcc `pkg-config --libs --cflags gtk+-2.0` test.c -o test
and it is giving the following error...
error: gtkmozembed.h: No such file or directory
I can understand, something else has to be added to the above gcc line,so that the compiler can find the gtkmozembed.h, but not getting what is that, 'something'...Looking for someone's help..Thank you...
Install libxul-dev (sudo apt-get install libxul-dev) and include
#include <gtkmozembed.h>
in the main file(test.c) and compile with
gcc `pkg-config --cflags --libs gtk+-2.0 xulrunner-gtkmozembed` test.c -o test
Your problem is that gtkmozembed.h is not found in the standard include file lookup path (well, the error does tell you that pretty obviously). On my system it lives in $(include)/gtkmozembed/, so you have two options
Change the path of the included file in your source
#include <gtkmozembed/gtkmozembed.h>
or manually add the path to the lookup path
gcc `pkg-config --libs --cflags gtk+-2.0` -I/usr/include/gtkmozembed test.c -o test
You should go with option 1).
This will tell gcc where to find the include file, but as pointed out by Matthew this is not enough: you will most probably also need to add more information for linking and required additional includes. Thankfully gtk-mozembed comes with a pkg-config file, so you can get all the needed information like you did for gtk+-2.0 with
pkg-config --libs --cflags mozilla-gtkmozembed-embedding
or combined with the other call
gcc `pkg-config --libs --cflags gtk+-2.0 mozilla-gtkmozembed-embedding` test.c -o test
You should also (just for kicks) have a look at what pkg-config does. The part in "`" is just what is return by the shell when executing that command. On my machine:
$ pkg-config --libs --cflags mozilla-gtkmozembed-embedding
-DXPCOM_GLUE -fshort-wchar \
-I/usr/include/xulrunner-1.9.2 -L/usr/lib/xulrunner-devel-1.9.2/lib -lxpcomglue
(line breaks added by me). The -I parts just adds additional needed directories to the include file lookup path -- they were emitted because you called with --cflags. The entries with -lxpcomglue is due to calling with --libs and ask for linking against this library, i.e. libxpcomglue.so. It is located in /usr/lib/xulrunner-devel-1.9.2/lib. The rest are a define and a gcc flag needed for gtkmozembed.
Try this:
gcc `pkg-config --libs --cflags gtk+-2.0 mozilla-gtkmozembed-embedding` test.c -o test

Resources