0

I'm a beginner in CMake and the ways that can use it. My IDE is Clion and I tried to install gtk3 for c++ and follow all of the instructions but the simple sample code that they suggest for the test is not work for me. you can find the instructions here. this is the code:

#include <gtkmm.h>

int main(int argc, char** argv)
{
    auto app = Gtk::Application::create(argc, argv);

    Gtk::Window window;
    window.set_default_size(600,400);

    return app->run(window);
}

the error is fatal error: gtkmm.h: No such file or directory but when I change the #include <gtkmm.h>to #include <gtkmm-3.0/gtkmm.h> I did not get the error anymore here but I got an error in other subheaders like fatal error: glibmm.h: No such file or directory. it just needs to somehow tell the compiler to look in the subdirectory but I don't know how to do it. I think it can be fixed in the CmakeLists.txt file but I don't know how to do it. this is my CmakeLists.txt file:

cmake_minimum_required(VERSION 3.16)
project(gtk)

set(CMAKE_CXX_STANDARD 14)

add_executable(gtk main.cpp)

please if anybody knows it helps me.

  • Welcome to Stack Overflow! Here we encourage to **search** before asking a question. "I think it can be fixed in the CmakeLists.txt file but I don't know how to do it." - Have you tried to search on this topic? Many questions on Stack Overflow already contain `CMakeLists.txt`, have you tried them? E.g. [that one](https://stackoverflow.com/questions/37301250/cmake-error-with-gtkmm). – Tsyvarev Jul 27 '20 at 15:49
  • To use CMake, you need to write a CMakeLists.txt file. – eerorika Jul 27 '20 at 15:50
  • Possible duplicate of https://stackoverflow.com/questions/2730135/how-do-i-link-gtk-library-more-easily-with-cmake-in-windows –  Jul 30 '20 at 09:56

1 Answers1

2

You are not linking to the gtk library properly.

You need to use find_package() in your CMakeLists.txt as shown below:

find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK REQUIRED gtk+-3.0)

include_directories(${GTK_INCLUDE_DIRS})
link_directories(${GTK_LIBRARY_DIRS})
add_definitions(${GTK_CFLAGS_OTHER})

add_executable(gtkdemo main.cpp)
target_link_libraries(gtkdemo ${GTK_LIBRARIES})

For further information, you may refer to the following:

'undefined reference to' when using GTK with CMake

How do I link gtk library more easily with cmake in windows?