我是GTK和gtkmm的新手。我一直试图为gtkmm编译一个hello代码示例,这导致了一个gtkmm/button.h: No such file or directory,我通过提供头路径修复了这个示例,但是现在我得到了这个新的错误,我无法修复这个错误。
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.ccmain.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_HCMake
# 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发布于 2021-12-21 09:18:07
您的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)靶标选项创建一个特殊目标,该目标自动配置标头和库搜索路径。
您的主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)https://stackoverflow.com/questions/70432554
复制相似问题