GTK3, C++, CMake, And CLion
October 29, 2020•359 words
Why?
All of the libraries and tools in the title are cross platform.
Also wanted to learn GTK3 so I can develop some apps for the Librem 5.
Work requires I build software for Windows and Mac, though, so cross platform is a must.
Going to outline steps needed to get started with Mac, Linux (Fedora), and Windows.
TL;DR
Install the gtk libraries for the GTK3 C++ bindings.
Mac: brew install gtkmm3
Fedora Linux: sudo dnf install gtkmm30 gtkmm30-devel gtkmm30-doc
CLion Cmake (Confirmed to work on Mac and Fedora Linux).
cmake_minimum_required(VERSION 3.17)
set(CMAKE_CXX_STANDARD 20)
project(hellogtk)
set(SOURCE_FILES main.cpp helloworld.cpp helloworld.h)
# use this to find the brew package.
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTKMM REQUIRED gtkmm-3.0)
# Available variables: GTKMM_INCLUDE_DIRS, GTKMM_LIBRARY_DIRS and GTKMM_LIBRARIES
include_directories(${GTKMM_INCLUDE_DIRS})
link_directories(${GTKMM_LIBRARY_DIRS})
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} ${GTKMM_LIBRARIES})
# Been warned that order in cmake matters.
How
Assuming I have CLion installed. Cmake should be bundled with CLion since it's a core component in how their IntelliSense features function.
Install gtkmm3 with a package manager. gtkmm is the c++ bindings for gtk, and the 3 denotes that we are using the GTK3 version.
Used brew
for MacOS and dnf
for Fedora.
Pretty sure brew installs all the dependencies.
Ran into an issue on Fedora where cmake couldn't find gtkmm3 packages. Either resolved by installing gtkmm30-devel
and/or resetting the cmake cache. Did both for good measure.
Using this wiki page as reference on how to setup the Cmake Above.
Just used the given HelloWorld code
Suggested by the docs that we split the source files into a separate folder.
Seems better that way, but for the Cmake above, everything is in the project root folder.
Notes
Works with CLion toolchains:
- GCC (my default):
- gmake
- gcc-10
- g++-10
- Bundled CMake and GDB
- Xcode Toolchain:
- gmake
- Xcode C Compiler
- Xcode C++ Compiler
- Bundled CMake and LLDB
for some reason, upon close in MacOS, GCC compiled version will throw a malloc error.
hellogtk(14953,0x1195d8dc0) malloc: *** error for object 0x7ffee5519a00: pointer being freed was not allocated
hellogtk(14953,0x1195d8dc0) malloc: *** set a breakpoint in malloc_error_break to debug
Clang is fine though.