在Ubuntu上编译并链接GTK 3与C项目 [英] Compiling and Linking GTK 3 with C project on Ubuntu

查看:518
本文介绍了在Ubuntu上编译并链接GTK 3与C项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信这不是一个重复的问题,我在发布这个问题之前看过所有的问题/答案。我想我在这里有一个不同的情况。



我使用Ubuntu 12.04并下载了 GTK 2和3 。我从GNOME的网站上复制了一个简单的GTK源代码。但是,当我在终端中使用这个命令时:

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

我得到这个:

  hello.c :(。text + 0x17):对`gtk_init'的未定义引用
hello.c :(。text + 0x23):未定义对`gtk_window_new'的引用
的hello.c :(文字+ 0X47):未定义的引用`gtk_main_quit '
的hello.c :(文字+ 0x5b):未定义的引用`g_signal_connect_data'
的hello.c :(的.text + 0x67)中:未定义参考`gtk_widget_show
的hello.c :(文本+ 0x6c):未定义参考`进入主循环

这是我的代码:

  #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();

返回0;
}

我不确定是否出现错误,因为我有两个版本的GTK +或什么。
我在Ubuntu / Linux的应用程序开发中是非常新手的。 库为 gcc hello.c $(pkg-config --cflags --libs gtk + -3.0)-o hello ,原因是链接器的行为,即它不链接图书馆,除非该图书馆的符号在编译前被看到。

希望这有助于!


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.

解决方案

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!

这篇关于在Ubuntu上编译并链接GTK 3与C项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆