how to build gtk program using cmake
This blog is a simple tutorial about how to build gtk program using cmake. cmake is an open source tool to build software program. it is different from Linux command tool. cmake is easy to learn and independent to the compiler. cmake takes inputs from a text file named as CMakeLists.txt and generates Makefile. After getting Makefile we can simply run make command to build the software program. Here is step by step instruction.
how to build gtk program using cmake
- Let us write a simple gtk program main.c
#include<gtk/gtk.h> GtkWidget *win; int main(int argc, char **argv) { gtk_init(&argc, &argv); win =gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_widget_show(win); // connect signal to quit window g_signal_connect(win,"destroy",G_CALLBACK(gtk_main_quit),NULL); gtk_main(); return 0; }
- Write a CMakeLists.txt
#set project name project(hello-gtk) #use pkg-config tool to detect gtk headers and link libs FIND_PACKAGE(PkgConfig REQUIRED) PKG_CHECK_MODULES(GTK3 REQUIRED gtk+-3.0) #include gtk3 headers INCLUDE_DIRECTORIES(${GTK3_INCLUDE_DIRS}) LINK_DIRECTORIES($GTK3_LIBRARY_DIRS}) #add other flags from gtk3 ADD_DEFINITIONS(${GTK3_CFLAGS_OTHER}) #add executabel compiled from main.c ADD_EXECUTABLE(hellogtk main.c) #link the target to the gtk+ libraries TARGET_LINK_LIBRARIES(hellogtk ${GTK3_LIBRARIES})
- Now we have two files main.c and CMakeLists.txt. run cmake . command in the current working directory.
- After running cmake . command , a Makefile would be generated. To build the binary simply run #make command. running make command results a binary ( hellogtk ). Below is video tutorial
Ref: https://cmake.org/
thanks nice,but you forget important things. what need install todo this. sudo apt-get install cmake libgtk-3-dev