issues with compiling Hello world program using gtkmm - c++

I am new to C++ and to gtkmm. I am currently trying to compile a tutorial I found online using a window and a button. I am compiling in Ubuntu 12.04. I can compile a single file fine but when I try to compile several files using a Makefile I get an error that I don't understand:
sarah#superawesome:~/gtkexample$ make
g++ -c main.cc
In file included from HelloSarah.h:4:0,
from main.cc:1:
/usr/include/gtkmm-3.0/gtkmm/button.h:7:28: fatal error: glibmm/ustring.h: No such file or directory
compilation terminated.
make: *** [main.o] Error 1
I really don't understand the error, I've been searching for hours. I would really appreciate any help or insight into my problem.
These are my 3 files and Makefile:
#ifndef GTKMM_HELLOSARAH_H
#define GTKMM_HELLOSARAH_H
#include <gtkmm-3.0/gtkmm/button.h>
#include <gtkmm/window.h>
class HelloSarah : public Gtk::Window
{
public:
HelloSarah();
virtual ~HelloSarah();
protected:
//Signal handlers:
void on_button_clicked();
//Member widgets:
Gtk::Button m_button;
};
#endif
and
main.cc
#include "HelloSarah.h"
#include <gtkmm/application.h>
int main (int argc, char *argv[])
{
Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
HelloSarah hellosarah;
//Shows the window and returns when it is closed.
return app->run(hellosarah);
}
and HelloSarah.cc
#include "helloSarah.h"
#include <iostream>
HelloSarah::HelloSarah()
: m_button("Hello Sarah") // creates a new button with label "HelloSarah".
{
// 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,
&HelloSarah::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();
}
HelloSarah::~HelloSarah()
{
}
void HelloSarah::on_button_clicked()
{
std::cout << "Hello Sarah" << std::endl;
}
and finally my Makefile:
app: main.o HelloSarah.o
g++ -o app main.o HelloSarah.o
main.o: main.cc HelloSarah.h
g++ -c main.cc
HelloSarah.o: HelloSarah.cc HelloSarah.h
g++ -c HelloSarah.cc
clean:
rm -f *.o app

The following include statement in your example is not correct. It works only because the file path is relative to the standard /usr/include/ directory, but the include statement in button.h does not, so you get an error message.
#include <gtkmm-3.0/gtkmm/button.h>
You have to tell g++ where the necessary include files and shared objects can be found. You can use the output of pkg-config to do that job.
pkg-config --cflags --libs gtkmm-3.0
The whole g++ command should be something like that.
g++ `pkg-config --cflags --libs gtkmm-3.0` -c HelloSarah.cc
After that you can simply use the include line in gtkmm Hello World.
#include <gtkmm/button.h>

I had this problem too on Ubuntu.
The solution:
sudo apt-get install libgtkmm-3.0-dev
You can use any version as you need.

Related

Makefile issues - how to include a function in a different file in your main file

My main source file
myapp.cpp
#include <iostream>
#include <string>
#include "myfunctions.h"
using namespace std;
int main()
{
cout << "Please enter your name \n"; //Prompts user to enter name
helloName();
return EXIT_SUCCESS;
}
the file containing the helloName() function
#include <iostream>
#include <string>
#include "myfunctions.h"
using namespace std;
void helloName()
{
string name; /// string variable defined as 'name'
cin >> name; //User enters name
cout << "Hello " << name << endl; //Outputs "Hello" followed by the name entered
}
my header file for helloName
myfunctions.h
#ifndef MYFUNCTIONS_H
#define MYFUNCTIONS_H
void helloName(void);
#endif
Makefile
# makefile for myapp
myapp: myapp.cpp myfunctions.cpp
g++ -ggdb -Wall -std=c++20 -fmodules-ts -o myapp myapp.cpp
myfunctions: myfunctions.cpp myfunctions.h
g++ -ggdb -Wall -std=c++20 -fmodules-ts -o myfunctions myfunctions.cpp
clean:
rm *# *~ myapp
When
make myapp
is entered into terminal, the following error is received:
/usr/bin/ld: /tmp/ccv3S2EK.o: in function main:
myapp.cpp:(.text+0x22): undefined reference to `helloName()'
Can anyone spot my issue? Haven't been coding very long so am sure it is probably basic
There are two problems:
You attempt to build myfunctions.cpp into an executable program named myfunctions.
For myapp you build the executable program using only the myapp.cpp source file.
To solve your problem I recommend you rely on implicit rules to create object files from source files, and link the object files into an executable file.
Then your Makefile only have to look like this:
LD = $(CXX)
myapp: myapp.o myfunctions.o
myapp.o: myapp.cpp myfunctions.h
myfunctions.o: myfunctions.cpp myfunctions.h
The first rule tells make to build the executable file myapp by linking the two object files myapp.o and myfunctions.o. The implicit rules will create the object files from their respective source file.
The second and third rules tells make that the two object files depend on their source file and the header file. So if the header file is modified, then both object files will be rebuilt.
The variable definition tells make that the linker is the same program as the C++ compiler.

GTKmm header files seem to be missing when I try to compile my program

I am trying to compile a GTKmm C++ program using g++ (clang), however I can't seem to find the header files that I need. I have run the installation .sh file for GTK+, and I have also tried directly installing GTKmm via Homebrew, but I am still getting this error:
fatal error: 'gtkmm/button.h' file not found
I thought it would be as simple as adding an include directory, but I have tried searching my hard drive for GTK-related things and am still not finding anything. What should I do?
I am using a Macbook running MacOS Big Sur.
Update:
My code is this:
helloworld.h
#ifndef GTKMM_EXAMPLE_HELLOWORLD_H
#define GTKMM_EXAMPLE_HELLOWORLD_H
#include <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
helloworld.cpp
#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;
}
main.cpp
#include "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);
}
This code comes from here.
I am compiling by first navigating to the directory of the files listed above, and running g++ main.cpp -o helloworld.
Because I have installed Homebrew, g++ uses clang++.
Ok, I have figured it out.
I needed to install both GTK+ and GTKmm via Homebrew. This put the library into /usr/local/include. Note that the question code still won't work, because it is using the wrong directory (I installed gtk+3 and gtkmm3). Refer to this answer for more information.

Error: GTKMM 3.0 compiling error

I have this error when compiling my first gtkmm project.
gtkmmgui.cpp:2:10: fatal error: gtkmm-3.0: No such file or directory
I also tried this:
g++ gtkmmgui.cpp `pkg-config --cflags --libs gtkmm3.0`
but it still doesn't work.
Is this because of wrong #include directory?
Source:
#include <iostream>
#include <gtkmm-3.0>
int main() {
Gtk::Main kit(argc, argv);
Gtk::Window window;
Gtk::Window::run(window)
return 0;
}
There should be nothing like
#include "gtkmm-3.0"
in your source.
Typically all includes look like:
#include <gtkmm/application.h>
#include <gtkmm/window.h>
You should provide also your source code here, because the error is something there! But please reduce it to the minimum where we can see your problem. Please never post all your code which is not related to the problem you ask for.
You also can check if your configuration of gtkmm is correct by simply looking in the output of you pkg-config command. Simply enter it on the command line:
> pkg-config gtkmm-3.0 --cflags
It should be something like:
-I/usr/include/gtkmm-3.0 -I/usr/lib64/gtkmm-3.0/include < a lot more >
EDIT: Your example code is broken in so many parts! Please read the manual of gtkmm!
The following works:
#include <gtkmm/window.h>
#include <gtkmm/main.h>
int main(int argc, char *argv[]) {
Gtk::Main kit(argc, argv);
Gtk::Window window;
kit.run(window);
return 0;
}
compiled and linked with:
g++ `pkg-config gtkmm-3.0 --cflags --libs` main.cpp

How to correctly link C++ objects using GLFW functions in Linux?

I've created a very simple program containing multiple files and I'm now trying to link the GLFW library to it. I've only added GLFW functions to the file main.cpp and only include the library there, and only compiling and executing this file using the command g++ main.cpp -lglfw goes fine.
Before I added this library, compiling and linking the entire program also went fine, but even though there are no GLFW functions used in the other files when I want to link everything together (g++ -Wall -g -std=c++11 -lglfw main.o hello_world.o console.o) I suddenly get the error 'undefined reference to' every GLFW function I used. (I got no error while compiling main.cpp: g++ -Wall -g -std=c++11 -lglfw -c main.cpp)
This is the file main.cpp:
#include "basis.h"
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
// Open-GL
#include <GLFW/glfw3.h>
/// Setup:
/// sudo apt-get update
/// sudo apt-get install libglfw3
/// sudo apt-get install libglfw3-dev
/// Compile:
/// g++ main.cpp -lglfw
using namespace std;
void errorCallback(int error, const char* description) {
fprintf(stderr, "Error: %s\n", description);
}
void test() {
GLFWwindow* window = glfwCreateWindow(640, 480, "My Title", NULL, NULL);
if (!window) {
cout << "Window creation failed!" << endl;
}
runConsole();
}
int main(int argc, char* argv[]) {
glfwSetErrorCallback(errorCallback);
if (!glfwInit()) {
cout << "GLFW initialization failed!" << endl;
exit(EXIT_FAILURE);
}
test();
glfwTerminate();
return 0;
}
Furthermore, I do not know where the library is located on my machine. Also, the Linux machine is Virtual Box, and the source code is in a shared folder located on my host windows system, but this his never created problems with libraries before as I compile them on Linux.
Oops, while re-reading the question I thought of a new solution, and it worked :) The correct linker command is: g++ -Wall -g -std=c++11 main.o hello_world.o console.o -lglfw

can't run a c++ file - Eclipse Yoxos

I downloaded Eclipse from Yoxos. This Eclispe includes: c, cpp, java etc..
However, when I opened a new cpp project with MinGW GCC Toolchains, and created a cpp file: hello.cpp, and wrote the following little program:
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
when I run the file, it said - hello.exe has stopped working.
However, When I changed the program to:
(first line in comment)
//#include <iostream>
#include <stdio.h>
int main() {
printf("dsd");
return 0;
}
It worked well!
and when I removed the first line from comment like this:
#include <iostream>
#include <stdio.h>
int main() {
printf("dsd");
return 0;
}
the problem was back.. :(
Someone, Help..?
Thanks in advance! :)
Build Console Output:
20:41:10 **** Incremental Build of configuration Release for project hello ****
Info: Internal Builder is used for build
g++ -O3 -Wall -c -fmessage-length=0 -o helo.o "..\\helo.cpp"
g++ -o hello.exe helo.o
20:41:11 Build Finished (took 610ms)