gtkmm hello world error: glibmm/ustring.h: No such file or directory

gtkmm hello world error: glibmm/ustring.h: No such file or directory

我是 GTK 和 gtkmm 的新手。我一直在尝试为 gtkmm 编译一个示例 hello world 代码,它导致 gtkmm/button.h: No such file or directory 我通过提供 header 路径修复了这个问题,但现在我收到了这个我无法修复的新错误。

In file included from /home/kshitij/Tutorials/HMI/libhelloworld/helloworld.h:4,
                 from /home/kshitij/Tutorials/HMI/main.cc:1:
/usr/include/gtkmm-3.0/gtkmm/button.h:6:10: fatal error: glibmm/ustring.h: No such file or directory
    6 | #include <glibmm/ustring.h>
      |          ^~~~~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/main.dir/build.make:63: CMakeFiles/main.dir/main.cc.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/main.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

下面我附上树和代码文件以及CMake文件以供参考。如果需要更多详细信息,请告诉我。

树:

├── build
├── CMakeLists.txt
├── libhelloworld
│   ├── CMakeLists.txt
│   ├── helloworld.cc
│   └── helloworld.h
└── main.cc

main.cc

#include "libhelloworld/helloworld.h"
#include <gtkmm/application.h>

int main (int argc, char *argv[])
{
  auto app = Gtk::Application::create(argc, argv, "org.gtkmm.example");

  HelloWorld helloworld;

  //Shows the window and returns when it is closed.
  return app->run(helloworld);
}

helloworld.cc

#include "helloworld.h"
#include <iostream>

HelloWorld::HelloWorld()
: m_button("Hello World")   // creates a new button with label "Hello World".
{
  // Sets the border width of the window.
  set_border_width(10);

  // When the button receives the "clicked" signal, it will call the
  // on_button_clicked() method defined below.
  m_button.signal_clicked().connect(sigc::mem_fun(*this,
              &HelloWorld::on_button_clicked));

  // This packs the button into the Window (a container).
  add(m_button);

  // The final step is to display this newly created widget...
  m_button.show();
}

HelloWorld::~HelloWorld()
{
}

void HelloWorld::on_button_clicked()
{
  std::cout << "Hello World" << std::endl;
}

helloworld.h

#ifndef GTKMM_EXAMPLE_HELLOWORLD_H
#define GTKMM_EXAMPLE_HELLOWORLD_H

#include <gtkmm-3.0/gtkmm/button.h>
#include <gtkmm/window.h>

class HelloWorld : public Gtk::Window
{

public:
  HelloWorld();
  virtual ~HelloWorld();

protected:
  //Signal handlers:
  void on_button_clicked();

  //Member widgets:
  Gtk::Button m_button;
};

#endif // GTKMM_EXAMPLE_HELLOWORLD_H

CMake

# 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)

add_subdirectory(libhelloworld)

add_executable(main main.cpp)
target_link_libraries(main PUBLIC libhelloworld)

libhelloworld/CMake

# 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)
find_package(PkgConfig REQUIRED)
pkg_check_modules(gtkmm REQUIRED IMPORTED_TARGET gtkmm-3.0 glibmm-2.4)

add_library(libhelloworld helloworld.cc)

target_link_libraries(libhelloworld PUBLIC PkgConfig::gtkmm)

编辑 1:建议的更改似乎解决了错误,但现在我收到了这个错误。

/usr/bin/ld: cannot find -llibhelloworld
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/main.dir/build.make:104: main] Error 1
make[1]: *** [CMakeFiles/Makefile2:96: CMakeFiles/main.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

您的 CMake 文件实际上并没有使用 glibmm,它只是搜索它。

这是您的 libhelloworld/CMakeLists.txt 的现代化版本:

# 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)
find_package(PkgConfig REQUIRED)
pkg_check_modules(gtkmm REQUIRED IMPORTED_TARGET gtkmm-3.0 glibmm-2.4)

add_library(libhelloworld helloworld.cc)

target_link_libraries(libhelloworld PUBLIC PkgConfig::gtkmm)

IMPORTED_TARGET 选项创建一个特殊目标,自动配置 header 和库搜索路径。

您的主要 CMakeLists.txt 现在可以按如下方式使用该目标:

# 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)

add_subdirectory(libhelloworld)

add_executable(main main.cpp)
target_link_libraries(main PUBLIC libhelloworld)