GTK3 - Creating simple window
I wrote this simple note to allow other people easier learning of how to create GTK3 applications.
main.cpp
Create main.cpp file, and set its contents as following:
#include <gtk/gtk.h>
int main(int argc, char * argv[], char * env[]) {
/* Initialize GTK library */
gtk_init(&argc, &argv);
/* Create window, place it on top of other. */
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
/* Call 'gtk_main_quit' function when destroying 'window'. You can
ommit this line, but without it, program won't be closed when the
close button will be clicked. Window will just be hidden.
Check that using 'ps'. */
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
/* Show created window */
gtk_widget_show_all(window);
/* Run program. Enter event loop. */
gtk_main();
}
Makefile
Create Makefile file in the same directory as main.cpp.
CC = g++
FLAGS = -Wall -pedantic -std=c++0x
FLAGS += `pkg-config gtk+-3.0 --cflags --libs`
OBJECTS = main.o
main : $(OBJECTS)
$(CC) $(FLAGS) $(OBJECTS) -o main
clean :
@rm -f $(OBJECTS) *~ *#
%.o: %.cpp
$(CC) $(FLAGS) -c $< -o $@
Compilation
Open terminal, cd to your project directory, and enter:
make main ; ./main
Outro
Simple, isn’t it?