I have the following C GTK+ code and I get the following error trying to compile:
undefined reference to gdk_pixbuf_new_from_file_utf8
Code:
#include <stdlib.h>
#include <gtk/gtk.h>
GdkPixbuf *create_pixbuf(const gchar *filename)
{
GdkPixbuf *pixbuf;
GError *err开发者_如何学Goor = NULL;
pixbuf = gdk_pixbuf_new_from_file(filename, &error);
if (!pixbuf)
{
fprintf(stderr, "%s\n", error->message);
g_error_free(error);
}
return pixbuf;
}
int main (int argc, char *argv[])
{
GtkWidget *window;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Center");
gtk_window_set_default_size(GTK_WINDOW(window), 230, 150);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_icon(GTK_WINDOW(window), create_pixbuf("someimg.jpg"));
gtk_widget_show(window);
g_signal_connect_swapped(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_main();
return 0;
}
Can anybody tell me whats wrong?
Just successfully compiled your code after doing the following:
Add the following two includes:
#include <glib/gerror.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
And compile with:
gcc `pkg-config --cflags --libs gtk+-2.0` test.c -o test
The pkg-config gives the following output:
-I/usr/include/gtk-2.0 -I/usr/lib/gtk-2.0/include -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng12 -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgio-2.0 -lpangoft2-1.0 -lgdk_pixbuf-2.0 -lpangocairo-1.0 -lcairo -lpango-1.0 -lfreetype -lfontconfig -lgobject-2.0 -lgmodule-2.0 -lglib-2.0
So you need to link with the following libraries:
- gtk-x11-2.0
- gdk-x11-2.0
- atk-1.0
- gio-2.0
- pangoft2-1.0
- gdk_pixbuf-2.0
- pangocairo-1.0
- cairo
- pango-1.0
- freetype
- fontconfig
- gobject-2.0
- gmodule-2.0
- glib-2.0
You may be able to leave out a few, if you want to try just start with gdk_pixbuf-2.0 and see if you still get any undefined references when you link.
Just tried, seems that for your code gtk-x11-2.0 and gdk_pixbuf-2.0 are sufficient.
A quick google seems to indicate that in your project options window you should have a linker tab where you can list all the libraries that should be linked with your code. If you're not on linux/unix you may need a diferent gtk library since gtk-x11-2.0 is specific to the X Window System.
gdk_pixbuf_new_from_file
is defined in gtk-2.0/gdk-pixbuf/gdk-pixbuf-core.h
But rather than including that file directly, it looks like the gdk-pixbuf library expects you to use just one header for all functionality. Try adding the following line:
#include <gdk-pixbuf/gdk-pixbuf.h>
(gdk_pixbuf_new_from_file
is a preprocessor macro which simply functions as an alias for gdk_pixbuf_new_from_file_utf8
. This is why the error message shows a different function name than what you actually used in your code example.)
Never used GTK, but 2 seconds googling suggests you need to add:
#include <gdk-pixbuf/gdk-pixbuf.h>
To the top of your file.
精彩评论