undefined reference to `gtk_init'

About programming and getting involved with Linux Mint development
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
prem00tta
Level 1
Level 1
Posts: 4
Joined: Tue May 18, 2021 10:44 am

undefined reference to `gtk_init'

Post by prem00tta »

I'm trying to write a simple "Hello World" type program using GTK and a Glade XML file, but I keep getting linker errors about undefined GTK symbols. I've included my main routine, my makefile, and the build output below.

Any ideas?

main.cxx:

Code: Select all

#include <gtk/gtk.h>

//-------------------------------------------------------------------------------------------------
int main
(
  int    argc, 
  char** argv
)
{
  gtk_init( &argc, &argv );

  GtkBuilder* builder = gtk_builder_new_from_file( "src/TaskMaster.glade" );
  if ( builder == nullptr )
    g_error("Error creating libglade interface");

  return 0;
}
Makefile:

Code: Select all

GTKHDRs = $(shell pkg-config --cflags glib-2.0 gtk+-3.0)
GTKLIBs = $(shell pkg-config --libs   glib-2.0 gtk+-3.0)

CXX       := g++
CXX_FLAGS := -Iinclude -Wall -W -std=c++17 -ggdb $(GTKHDRs)
LDFLAGS   := $(GTKLIBs)

BIN     := bin
SRC     := src

EXECUTABLE  := taskmaster

all: $(BIN)/$(EXECUTABLE)

run: clean all
	./$(BIN)/$(EXECUTABLE)

$(BIN)/$(EXECUTABLE): $(SRC)/*.cxx
	$(CXX) $(CXX_FLAGS) $(LDFLAGS) $^ -o $@

clean:
	-rm -f $(BIN)/*

rebuild: clean all
Build Ouptut:

Code: Select all

g++ -Iinclude -Wall -W -std=c++17 -ggdb -pthread -I/usr/include/gtk-3.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/include/at-spi-2.0 -I/usr/include/dbus-1.0 -I/usr/lib/x86_64-linux-gnu/dbus-1.0/include -I/usr/include/gtk-3.0 -I/usr/include/gio-unix-2.0 -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/fribidi -I/usr/include/harfbuzz -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/uuid -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lgtk-3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -lharfbuzz -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 src/main.cxx -o bin/taskmaster
/usr/bin/ld: /tmp/ccHgbbwf.o: in function `main':
/home/niblacks/Documents/Projects/TaskMaster/src/main.cxx:11: undefined reference to `gtk_init'
/usr/bin/ld: /home/niblacks/Documents/Projects/TaskMaster/src/main.cxx:13: undefined reference to `gtk_builder_new_from_file'
/usr/bin/ld: /home/niblacks/Documents/Projects/TaskMaster/src/main.cxx:15: undefined reference to `g_log'
collect2: error: ld returned 1 exit status
make: *** [Makefile:19: bin/taskmaster] Error 1
The terminal process "/bin/bash '-c', 'make'" terminated with exit code: 2.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: undefined reference to `gtk_init'

Post by rene »

Move the libs after the object file(s) in the linker flags. I.e., here $(CXX) $(CXX_FLAGS) $^ $(LDFLAGS) -o $@
prem00tta
Level 1
Level 1
Posts: 4
Joined: Tue May 18, 2021 10:44 am

Re: undefined reference to `gtk_init'

Post by prem00tta »

BINGO! That was it. Thank you.
Locked

Return to “Programming & Development”