Gtk with CMake
Recently I wanted to write a small GTK
application. As my favorite language is C++
, I wanted to use that
language. GTK or better GNOME propose
Meson as a build system. However since I
have worked a lot with CMake and fine with it, I
wanted to use CMake as my build system. Another thing that speaks for CMake, is the fact that it is used by a lot of other C++
projects, so it easy to integrate them.
A problem that I faced when using the CMake build system, was that I
somehow needed to generate the resources file for the user
interface. While searching the internet, I was not able to find a
solution to this problem, so I wanted to post my solution here. I
basically defined a custom command. If there is a better solution, I
like to hear from you and yes I know the use of GLOB_RECURSE
is evil
but I found it too convenient and had never real trouble with it.
project(shaderviewer CXX)
cmake_minimum_required(VERSION 3.18.0)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
find_package(PkgConfig)
pkg_check_modules(GTKMM gtkmm-3.0)
set(RESOURCES "${CMAKE_CURRENT_BINARY_DIR}/resources.cpp")
add_custom_command(
OUTPUT ${RESOURCES}
COMMAND glib-compile-resources
--target=resources.cpp
--generate-source
../app/shaderviewer.gresource.xml
--sourcedir=../app
COMMENT "Generating resources"
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
)
file(
GLOB_RECURSE
HEADER_LIST
CONFIGURE_DEPENDS
"app/*.hpp"
)
file(
GLOB_RECURSE
SOURCE_LIST
CONFIGURE_DEPENDS
"app/*.cpp"
)
add_executable(shaderviewer ${SOURCE_LIST} ${HEADER_LIST} ${RESOURCES})
target_compile_features(shaderviewer PUBLIC cxx_std_17)
target_include_directories(shaderviewer PRIVATE ${GTKMM_INCLUDE_DIRS})
target_link_directories(shaderviewer PRIVATE ${GTKMM_LIBRARY_DIRS})
target_link_libraries(shaderviewer PRIVATE ${GTKMM_LIBRARIES} epoxy)
I used the following directory structure:
app
cmake
CMakeLists.txt
The source code and xml resource files were stored in the app directory.