Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CMake and GTK+ 3
# Thanks to @danger89 and @Ilothar for updating the gist.
# Set the name and the supported language of the project
project(hello-world C CXX)
# Set the minimum version of cmake required to build this project
cmake_minimum_required(VERSION 3.10)
# Use the package PkgConfig to detect GTK+ headers/library files
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK REQUIRED gtkmm-3.0)
add_executable(hello main.cpp)
target_link_libraries(hello PRIVATE ${GTKMM_LIBRARIES})
# Add other flags to the compiler
target_compile_definitions(hello PRIVATE ${GTKMM_CFLAGS_OTHER})
# Setup CMake to use GTK+, tell the compiler where to look for headers
# and to the linker where to look for libraries
target_include_directories(hello PRIVATE ${GTKMM_INCLUDE_DIRS})
target_link_directories(hello PRIVATE ${GTKMM_LIBRARY_DIRS})
#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), "Hello GNOME");
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);
}
@dholmes215

This comment has been minimized.

Copy link

@dholmes215 dholmes215 commented Oct 22, 2017

If anybody's trying this today and it's not working, try replacing "GTK3" in the CMakeLists with just "GTK". Apparently that's what it's supposed to be now.

@Lu-Yi-Hsun

This comment has been minimized.

Copy link

@Lu-Yi-Hsun Lu-Yi-Hsun commented Feb 23, 2018

thank that work!

@chillaxdev

This comment has been minimized.

Copy link

@chillaxdev chillaxdev commented Feb 8, 2019

Thank you @dholmes215

@WaltWilliams

This comment has been minimized.

Copy link

@WaltWilliams WaltWilliams commented Dec 18, 2019

Most excellent. This worked.
Thanks.

@SY-HHK

This comment has been minimized.

Copy link

@SY-HHK SY-HHK commented Feb 4, 2020

ty

@Medghofran

This comment has been minimized.

Copy link

@Medghofran Medghofran commented Mar 5, 2020

it worked for me as well, thank you so much !

@mulderp

This comment has been minimized.

Copy link

@mulderp mulderp commented Mar 21, 2020

thanks, that is a nice quick start. Also working with OpenSuse I found the gtk3-devel package helpful see a full list here https://de.opensuse.org/GTK%2B-Entwicklung

@gp42

This comment has been minimized.

Copy link

@gp42 gp42 commented Apr 26, 2020

thx :)

@x0rld

This comment has been minimized.

Copy link

@x0rld x0rld commented May 11, 2020

for people on linux you must add set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -rdynamic") to make work the signals for functions

@leomorpho

This comment has been minimized.

Copy link

@leomorpho leomorpho commented May 22, 2020

Thank you! That worked on macos with the brew version!

@hykilpikonna

This comment has been minimized.

Copy link

@hykilpikonna hykilpikonna commented Jan 18, 2021

For gtkmm-3.0:

# Use the package PkgConfig to detect GTK+ headers/library files
FIND_PACKAGE(PkgConfig REQUIRED)
PKG_CHECK_MODULES(GTK REQUIRED gtkmm-3.0)

# Setup CMake to use GTK+, tell the compiler where to look for headers
# and to the linker where to look for libraries
INCLUDE_DIRECTORIES(${GTK_INCLUDE_DIRS})
LINK_DIRECTORIES(${GTK_LIBRARY_DIRS})

# Add other flags to the compiler
ADD_DEFINITIONS(${GTK_CFLAGS_OTHER})

add_executable(hello main.cpp)
TARGET_LINK_LIBRARIES(hello ${GTK_LIBRARIES})
@Honghe

This comment has been minimized.

Copy link

@Honghe Honghe commented Feb 24, 2021

Don't forget sudo apt install libgtk-3-dev

@triplejam

This comment has been minimized.

Copy link

@triplejam triplejam commented Mar 12, 2021

@hykilpikonna thanks

@danger89

This comment has been minimized.

Copy link

@danger89 danger89 commented Apr 4, 2021

For gtkmm-3.0:

# Use the package PkgConfig to detect GTK+ headers/library files
FIND_PACKAGE(PkgConfig REQUIRED)
PKG_CHECK_MODULES(GTK REQUIRED gtkmm-3.0)

# Setup CMake to use GTK+, tell the compiler where to look for headers
# and to the linker where to look for libraries
INCLUDE_DIRECTORIES(${GTK_INCLUDE_DIRS})
LINK_DIRECTORIES(${GTK_LIBRARY_DIRS})

# Add other flags to the compiler
ADD_DEFINITIONS(${GTK_CFLAGS_OTHER})

add_executable(hello main.cpp)
TARGET_LINK_LIBRARIES(hello ${GTK_LIBRARIES})

I think it should be:

# Use the package PkgConfig to detect GTK+ headers/library files
FIND_PACKAGE(PkgConfig REQUIRED)
PKG_CHECK_MODULES(GTK REQUIRED gtkmm-3.0)

# Setup CMake to use GTK+, tell the compiler where to look for headers
# and to the linker where to look for libraries
INCLUDE_DIRECTORIES(${GTKMM_INCLUDE_DIRS})
LINK_DIRECTORIES(${GTKMM_LIBRARY_DIRS})

# Add other flags to the compiler
ADD_DEFINITIONS(${GTKMM_CFLAGS_OTHER})

add_executable(hello main.cpp)
TARGET_LINK_LIBRARIES(hello ${GTKMM_LIBRARIES})
@llothar

This comment has been minimized.

Copy link

@llothar llothar commented Apr 9, 2021

Please for god sake, update this example to modern CMake.

Its one of the top lists when you google "Gtk CMakeLists.txt" but its 9 years old.

@fracek

This comment has been minimized.

Copy link
Owner Author

@fracek fracek commented Apr 9, 2021

Is @danger89 version working? I can update the gist to it.

@danger89

This comment has been minimized.

Copy link

@danger89 danger89 commented Apr 9, 2021

@fracek Yes that is working, I'm using this myself on libreweb project:

https://gitlab.melroy.org/libreweb/browser/-/blob/master/src/CMakeLists.txt#L24

@llothar

This comment has been minimized.

Copy link

@llothar llothar commented Apr 9, 2021

Its working but it shouldn't be used anymore, this is old style and obsoleted for more than 10 years.


# Set the name and the supported language of the project
project(hello-world C CXX)

# Set the minimum version of cmake required to build this project
cmake_minimum_required(VERSION 3.10)

# Use the package PkgConfig to detect GTK+ headers/library files
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK REQUIRED gtkmm-3.0)


add_executable(hello main.cpp)
target_link_libraries(hello PRIVATE ${GTKMM_LIBRARIES})


# Add other flags to the compiler
target_compile_definitions(hello PRIVATE ${GTKMM_CFLAGS_OTHER})


# Setup CMake to use GTK+, tell the compiler where to look for headers
# and to the linker where to look for libraries
target_include_directories(hello PRIVATE ${GTKMM_INCLUDE_DIRS})
target_link_directories(hello PRIVATE ${GTKMM_LIBRARY_DIRS})

@danger89

This comment has been minimized.

Copy link

@danger89 danger89 commented Apr 9, 2021

Its working but it shouldn't be used anymore, this is old style and obsoleted for more than 10 years.


# Set the name and the supported language of the project
project(hello-world C CXX)

# Set the minimum version of cmake required to build this project
cmake_minimum_required(VERSION 3.10)

# Use the package PkgConfig to detect GTK+ headers/library files
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK REQUIRED gtkmm-3.0)


add_executable(hello main.cpp)
target_link_libraries(hello PRIVATE ${GTKMM_LIBRARIES})


# Add other flags to the compiler
target_compile_definitions(hello PRIVATE ${GTKMM_CFLAGS_OTHER})


# Setup CMake to use GTK+, tell the compiler where to look for headers
# and to the linker where to look for libraries
target_include_directories(hello PRIVATE ${GTKMM_INCLUDE_DIRS})
target_link_directories(hello PRIVATE ${GTKMM_LIBRARY_DIRS})

I don't know what you are talking about. But what you quoted is the new way of doing it.

@llothar

This comment has been minimized.

Copy link

@llothar llothar commented Apr 9, 2021

Now in "modern" cmake everything is a target and all dependencies are either Private/Public/Interface. Compared to the old way you did it where everything was a put into global variables.

The Internet now shows the problem of coming of age and you still rank #1 for "gtk cmake example" on google. Thats why i suggest going modern. Thanks for changing it

@g0bel1n

This comment has been minimized.

Copy link

@g0bel1n g0bel1n commented Nov 3, 2021

For gtkmm-3.0:

# Use the package PkgConfig to detect GTK+ headers/library files
FIND_PACKAGE(PkgConfig REQUIRED)
PKG_CHECK_MODULES(GTK REQUIRED gtkmm-3.0)

# Setup CMake to use GTK+, tell the compiler where to look for headers
# and to the linker where to look for libraries
INCLUDE_DIRECTORIES(${GTK_INCLUDE_DIRS})
LINK_DIRECTORIES(${GTK_LIBRARY_DIRS})

# Add other flags to the compiler
ADD_DEFINITIONS(${GTK_CFLAGS_OTHER})

add_executable(hello main.cpp)
TARGET_LINK_LIBRARIES(hello ${GTK_LIBRARIES})

That was the way it worked for me, changing every GTKMM to GTK !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment