Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


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

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 Page 77. I have installed libgtk2.0 and libgtk2.0-dev.
The 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;
$ 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.
$ pkg-config --cflags --libs gtk+-2.0
What's missing?
Old 07-11-2019, 01:15 AM   #2
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,157
Blog Entries: 11

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

From that thread:

Objects that want symbol definitions must be linked before the objects that provide the definitions.
As linked, from man ld
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
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,784

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:30 AM.

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