LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-11-2019, 12:38 AM   #1
CollieJim
Member
 
Registered: Mar 2005
Distribution: Gentoo, Kubuntu
Posts: 582

Rep: Reputation: 28
undefined reference to `gtk_init'


I am trying to compile a test GTK program following instructions in the "C_GUI_Programming" document found at https://www.raspberrypi.org/magpi/is...i-programming/ Page 77. I have installed libgtk2.0 and libgtk2.0-dev.
The code:
Code:
#include <gtk/gtk.h>

int main ( int argc, char *argv[] )
{
    gtk_init ( &argc, &argv );
    GtkWidget *win = gtk_window_new ( GTK_WINDOW_TOPLEVEL );
    gtk_widget_show ( win );
    gtk_main ( );
    return 0;
}
Code:
$ gcc `pkg-config --cflags --libs gtk+-2.0` gtktest.c -o gtktest
/tmp/ccQtPQqB.o: In function `main':
gtktest.c:(.text+0x12): undefined reference to `gtk_init'
gtktest.c:(.text+0x18): undefined reference to `gtk_window_new'
gtktest.c:(.text+0x20): undefined reference to `gtk_widget_show'
gtktest.c:(.text+0x24): undefined reference to `gtk_main'
collect2: error: ld returned 1 exit status
The output from pkg-confg follows. I have replaced spaces with newlines to make readable.
Code:
$ pkg-config --cflags --libs gtk+-2.0
-pthread
-I/usr/include/gtk-2.0
-I/usr/lib/arm-linux-gnueabihf/gtk-2.0/include
-I/usr/include/gio-unix-2.0/
-I/usr/include/cairo
-I/usr/include/pango-1.0
-I/usr/include/atk-1.0
-I/usr/include/cairo
-I/usr/include/pixman-1
-I/usr/include/gdk-pixbuf-2.0
-I/usr/include/libpng16
-I/usr/include/pango-1.0
-I/usr/include/harfbuzz
-I/usr/include/pango-1.0
-I/usr/include/glib-2.0
-I/usr/lib/arm-linux-gnueabihf/glib-2.0/include
-I/usr/include/freetype2
-I/usr/include/libpng16
-I/usr/include/freetype2
-I/usr/include/libpng16
-lgtk-x11-2.0
-lgdk-x11-2.0
-lpangocairo-1.0
-latk-1.0
-lcairo
-lgdk_pixbuf-2.0
-lgio-2.0
-lpangoft2-1.0
-lpango-1.0
-lgobject-2.0
-lglib-2.0
-lfontconfig
-lfreetype
What's missing?
 
Old 07-11-2019, 01:15 AM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Have a look at this thread on StackOverflow.

From that thread:

Quote:
Objects that want symbol definitions must be linked before the objects that provide the definitions.
As linked, from man ld
Quote:
The linker will search an archive only once, at the location where it is specified on the command line. If the archive defines a symbol which was undefined in some object which appeared before the archive on the command line, the linker will include the appropriate file(s) from the archive. However, an undefined symbol in an object appearing later on the command line will not cause the linker to search the archive again.
Move the --libs after the object file has been created.

Last edited by astrogeek; 07-11-2019 at 01:28 AM. Reason: Expanded comment
 
1 members found this post helpful.
Old 07-11-2019, 03:05 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
`ld` has been recently changed, the order of objects and libraries now matters, so try this:
Code:
gcc `pkg-config --cflags` gtktest.c -o gtktest `pkg-config--libs gtk+-2.0`
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Notice: Undefined index / Undefined variable in /var/log/httpd/error_log AdultFoundry Linux - Server 3 06-20-2016 06:06 PM
[SOLVED] ldap_add undefined attribute type (17) additional info attribute type undefined yashfire007 Linux - Newbie 6 03-20-2015 09:12 AM
PHP Problem ('undefined index', 'undefined function') zokken Programming 2 12-04-2008 10:18 AM
Problem with gtk_init techrolla Programming 5 11-21-2003 07:17 AM
make error/ undefined reference apljdi Linux - Newbie 1 10-29-2002 01:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration