0

I am developing in C and after deciding to do GUI application development using GTK, I have an issue with linking the newest GTK3 libraries to build my application with the msys64 version of GCC under windows 7 (64bit). I did the due research on SO and read and tested quite a bit, but nothing solved the problem.

Code-Example from WIKI I am trying to build ("gtk_ex.c"):

#include <gtk/gtk.h>

 void on_button_clicked (GtkButton *button, gpointer data)
{
  g_print ("Knopf '%s' geklickt!\n", gtk_button_get_label (button));
  gtk_main_quit (); /* Quits */
}

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

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Hallo Welt!");
  gtk_container_set_border_width (GTK_CONTAINER (window), 10);

  button = gtk_button_new_with_label ("Hello GTK!");
  gtk_container_add (GTK_CONTAINER (window), button);

  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
  g_signal_connect (button, "clicked", G_CALLBACK (on_button_clicked), NULL);

  gtk_widget_show_all (window);

  gtk_main ();

  return 0;
}

What I already did (status quo):

  • Installed msys64 for win7 (64bit) OS
  • Got relevant packages via pacman

    pacman -S mingw-w64-x86_64-gtk3

    pacman -S mingw-w64-x86_64-toolchain base-devel

Set system PATH evironment variable to C:\msys64/mingw64/bin

I built a batch file by successively adding the dependencies the compiler complained to be missing:

set GINC_PATH="C:\msys64/mingw64/include"
set GLIB_PATH="C:\msys64/mingw64/lib"
gcc -v -g %1.c -o %1 -I %GINC_PATH%/gtk-3.0 -I %GINC_PATH%/glib-2.0 -I %GLIB_PATH%/glib-2.0/include -I %GINC_PATH%/pango-1.0 -I %GINC_PATH%/cairo -I %GINC_PATH%/gdk-pixbuf-2.0 -I %GINC_PATH%/atk-1.0

(In this batch file, I only stated the include options, as the -L and -l commands did not work in any place...) Using this batch file, the build process now reaches the linking part of the build process, where linking completely fails with the following output (excerpt, lots of similar lines):

C:\C/gtk_ex.c:8: undefined reference to `gtk_application_window_new'
C:\C/gtk_ex.c:9: undefined reference to `gtk_window_get_type'
C:\C/gtk_ex.c:9: undefined reference to `g_type_check_instance_cast'
(...)
collect2.exe: error: ld returned 1 exit status

I learned on SO that GCC may be picky about the exact sequence of include and library options on the command line - However, I have this error even after trying out multiple versions of command line versions of GCC input, both manual and by automatic generation by pkg-config in all possible variations... So basically, where do I need to put the -L and -l commands for library binding for a successful build process?

EDIT_1:

I double-checked that all include paths are correct and also ran pkg-config --cflags gtk+-3.0 and pkg-config --libs gtk+-3.0 to generate the output automatically - My batch-file shown above still returns the output "undefined reference to XY" (several lines) plus the final line again

collect2.exe: error: ld returned 1 exit status

Linking in a separate line in the batch file as proposed by ryyker:

gcc -o gtk_ex gtk_ex.o -L "C:\msys64/mingw64/lib" -lgtk -lgdk

returns:

gcc: error: gtk_ex.o: No such file or directory

(So, the *.o-file is not existing, that means compiling failed?)

EDIT_1 END

As a last resort, I am asking here if maybe there is something I missed to try out. I am happy for any hint how I could solve this issue. Otherwise, I will have to revert to an old bundled version of GTK from 2013 which I got working. However, I would prefer to use the current, maintained version...

EDIT_2:

Thank you @ryyker and @Gerhardh, you saved me :) - I took your advice and split compiling and linking to 2 separate lines. I then successively added libraries via -l and checked whether the addition was productive. This modified batch-file then did the trick:

set GINC_PATH="C:\msys64/mingw64/include"

set GLIB_PATH="C:\msys64/mingw64/lib"

gcc -c %1.c -o %1.o -I %GINC_PATH%/gtk-3.0 -I %GINC_PATH%/glib-2.0 -I %GLIB_PATH%/glib-2.0/include -I %GINC_PATH%/pango-1.0 -I %GINC_PATH%/cairo -I %GINC_PATH%/gdk-pixbuf-2.0 -I %GINC_PATH%/atk-1.0

gcc -o %1 %1.o -L %GLIB_PATH% -lgtk-3 -lgdk-3 -lgobject-2.0 -lglib-2.0

%1.exe

Were can I flag this as answered/resolved and give you credit for your input? Thank you all!

Kind regards

Xuttuh

Xuttuh
  • 29
  • 4
  • Welcome to SO. You don't provide any linking option on the command line. You only provide include-paths for headers. – Gerhardh Aug 02 '18 at 11:53
  • Thank you Gerhardh. I specified the description of the batch file, it is a verison where I skipped all -L and -l options. This versions at least allows me to reach the linking step and the compiling is fine - the problem must be with the exact definition of the -L and -l options... – Xuttuh Aug 02 '18 at 12:07
  • Try splitting your GCC command line to 2 lines 1) _compile_, 2) _link_ in the batch file. If you were to do this (or have already tried it) post what the link line looked like. And are you certain all the paths are correct? – ryyker Aug 02 '18 at 12:17
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – IInspectable Aug 02 '18 at 12:23
  • Please show your updated compile command that you use after splitting into 2 lines. Your initial compile command did not create a file with suffix `.o` – Gerhardh Aug 02 '18 at 13:46
  • `pacman -S mingw-w64-x86_64-gtk3`, `pkg-config --cflags gtk+-2.0`. What version of Gtk do you want to use? – Alexander Dmitriev Aug 03 '18 at 09:37
  • Thanks Alexander, the "2.0" was a typo... I corrected it. I want to use gtk 3.0 and build for win 64 bit systems. – Xuttuh Aug 03 '18 at 11:06

1 Answers1

0

Please, save your self some time and use a real build system, like Meson. It's cross-platform, only depends on Python 3 and ninja (ninja is a replacement of make).

Building a GTK+ app is 3 lines of Meson.

liberforce
  • 11,189
  • 37
  • 48