Mahmoud Embaby
Mahmoud Embaby

Reputation: 317

undefined reference to `gtk_application_window_new' when C compiling GTK library with starter code

I am totally new to C and I am interested to create a GUI using GTK in my C project.

I use windows 11, I followed all instructions on GTK website for windows installation, most of my problems are now solved but still one last problem is:

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\IDEAPA~1\AppData\Local\Temp\cccLRsbz.o:Interface.c:(.text+0x15): undefined reference to `gtk_application_window_new'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\IDEAPA~1\AppData\Local\Temp\cccLRsbz.o:Interface.c:(.text+0x1e): undefined reference to `gtk_window_get_type'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\IDEAPA~1\AppData\Local\Temp\cccLRsbz.o:Interface.c:(.text+0x2d): undefined reference to `g_type_check_instance_cast'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\IDEAPA~1\AppData\Local\Temp\cccLRsbz.o:Interface.c:(.text+0x3f): undefined reference to `gtk_window_set_title'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\IDEAPA~1\AppData\Local\Temp\cccLRsbz.o:Interface.c:(.text+0x44): undefined reference to `gtk_window_get_type'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\IDEAPA~1\AppData\Local\Temp\cccLRsbz.o:Interface.c:(.text+0x53): undefined reference to `g_type_check_instance_cast'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\IDEAPA~1\AppData\Local\Temp\cccLRsbz.o:Interface.c:(.text+0x66): undefined reference to `gtk_window_set_default_size'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\IDEAPA~1\AppData\Local\Temp\cccLRsbz.o:Interface.c:(.text+0x72): undefined reference to `gtk_widget_show_all'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\IDEAPA~1\AppData\Local\Temp\cccLRsbz.o:Interface.c:(.text+0xa1): undefined reference to `gtk_application_new'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\IDEAPA~1\AppData\Local\Temp\cccLRsbz.o:Interface.c:(.text+0xd6): undefined reference to `g_signal_connect_data'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\IDEAPA~1\AppData\Local\Temp\cccLRsbz.o:Interface.c:(.text+0xdb): undefined reference to `g_application_get_type'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\IDEAPA~1\AppData\Local\Temp\cccLRsbz.o:Interface.c:(.text+0xea): undefined reference to `g_type_check_instance_cast'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\IDEAPA~1\AppData\Local\Temp\cccLRsbz.o:Interface.c:(.text+0xfc): undefined reference to `g_application_run'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\IDEAPA~1\AppData\Local\Temp\cccLRsbz.o:Interface.c:(.text+0x10b): undefined reference to `g_object_unref'
collect2.exe: error: ld returned 1 exit status

I compile my C file using this command: gcc Interface.c -IC:/msys64/mingw64/bin/../include/gtk-3.0 -IC:/msys64/mingw64/bin/../include/pango-1.0 -IC:/msys64/mingw64/bin/../include -IC:/msys64/mingw64/bin/../include/glib-2.0 -IC:/msys64/mingw64/bin/../lib/glib-2.0/include -IC:/msys64/mingw64/bin/../include/harfbuzz -IC:/msys64/mingw64/bin/../include/freetype2 -IC:/msys64/mingw64/bin/../include/libpng16 -mms-bitfields -IC:/msys64/mingw64/bin/../include/fribidi -IC:/msys64/mingw64/bin/../include/cairo -IC:/msys64/mingw64/bin/../include/lzo -IC:/msys64/mingw64/bin/../include/pixman-1 -mms-bitfields -mms-bitfields -mms-bitfields -mms-bitfields -mms-bitfields -mms-bitfields -mms-bitfields -IC:/msys64/mingw64/bin/../include/gdk-pixbuf-2.0 -mms-bitfields -mms-bitfields -mms-bitfields -IC:/msys64/mingw64/bin/../include/atk-1.0 -mms-bitfields -mms-bitfields -mms-bitfields -pthread -mms-bitfields

I use Visual Studio Code.

Starter code:

#include <gtk/gtk.h>

static void
activate (GtkApplication* app,
          gpointer        user_data)
{
  GtkWidget *window;

  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "Window");
  gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
  gtk_widget_show_all (window);
}

int
main (int    argc,
      char **argv)
{
  GtkApplication *app;
  int status;

  app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}

Can you suggest me a way to solve my problem? I don't know why it happens due to my very little knowledge in C.

I followed all other answers to similar questions like mine but none were useful to me. Thanks!

Upvotes: 2

Views: 2563

Answers (1)

Matthew P.
Matthew P.

Reputation: 56

This is a link error, not a compile error so I'd guess you are missing at least the reference to library gtk-3 in your link library dependencies (takes care of the gtk_application_window_new reference). There might be others missing too.

You might take a look here: https://docs.gtk.org/gtk3/compiling.html for a example of what you might need.

Upvotes: 4

Related Questions