Compiling and Linking GTK 3 with C project on Ubun

2019-03-14 20:29发布

I believe this is not a duplicate question, I have seen all questions/answers before I post this question. I think I have a different situation here.

I use Ubuntu 12.04 and downloaded GTK 2 and 3. I have copied a simple GTK source code from GNOME's website. But when I use this command in terminal:

gcc `pkg-config --cflags --libs gtk+-3.0`  hello.c -o hello

I get this:

hello.c:(.text+0x17): undefined reference to `gtk_init'
hello.c:(.text+0x23): undefined reference to `gtk_window_new'
hello.c:(.text+0x47): undefined reference to `gtk_main_quit'
hello.c:(.text+0x5b): undefined reference to `g_signal_connect_data'
hello.c:(.text+0x67): undefined reference to `gtk_widget_show'
hello.c:(.text+0x6c): undefined reference to `gtk_main'

here is my code:

#include <gtk/gtk.h>

int
main (int   argc,
char *argv[])
{
  GtkWidget *window;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);

  gtk_widget_show (window);

  gtk_main ();

  return 0;
}

I'm not sure if errors appear because I have two versions of GTK+ or what. I'm extremely newbie in Applications Development in Ubuntu/Linux.

标签: c gcc gtk
2条回答
混吃等死
2楼-- · 2019-03-14 21:04

You could also include the gtk library directly into your project:

  1. In Eclipse expand: Project > Properties > C/C++ Build > Settings > Tool Settings > Cross GCC Linker > Libraries
  2. Set: Library search path (-L) to path to file "libgtk-n.so", with n as your version. After installing (apt-get install libgtk-3-dev) on Lubuntu this was "/usr/lib/x86_64-linux-gnu/" for me.
  3. Then add to Libraries (-l): gtk-n
查看更多
该账号已被封号
3楼-- · 2019-03-14 21:08

You should compile with source file appearing before the libraries as gcc hello.c $(pkg-config --cflags --libs gtk+-3.0) -o hello, the reason being the behavior of linker i.e it does not link the libraries unless the symbols of that library is seen prior in compilation.
Hope this helps!

查看更多
登录 后发表回答