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