I am new to GTK and gtkmm. I have been trying to compile an example hello world code for gtkmm which resulted in a gtkmm/button.h: No such file or directory
I fixed this by providing the header path, but now I am getting this new error which I am unable to fix.
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
Below I am attaching the tree and the code files and the CMake file for reference. Please let me know if more details are required.
Tree:
├── 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)
Edit 1: The changes suggested seem to solve the error but now I am getting this error instead.
/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
CodePudding user response:
Your CMake file does not actually use glibmm, it just searches for it.
Here is a modernized version of your 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)
The IMPORTED_TARGET option creates a special target that automatically configures the header and library search path.
Your main CMakeLists.txt can now use that target as follows:
# 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)