GTk and open GL Not working at compile time - opengl

I am trying to make an application using Gtk 3 and OpenGl but when I try to compile the file, the Gl commands are not recognized. Any suggestions? Code and Errors provided below. I am just trying to setup and render a empty gl area.
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <GL/gl.h>
static gboolean
render (GtkGLArea *area, GdkGLContext *context)
{
glClearColor(0, 0, 0, 0);
return TRUE;
}
static void activate (GtkApplication *app, gpointer user_data){
GtkWidget *window;
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Calculator");
gtk_window_resize(GTK_WINDOW(window), 290, 300);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
GtkWidget *gl = gtk_gl_area_new();
g_signal_connect (gl, "render", G_CALLBACK(render), NULL);
gtk_widget_show_all(window);
gtk_main();
}
int main(int argc, char **argv){
GtkApplication *app;
int status;
app = gtk_application_new("com.calculate", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref(app);
return status;
}
Compiled with:
gcc OpenGl.c `pkg-config --cflags gtk+-3.0` -o gl `pkg-config --libs gtk+-3.0`
Error Message:
/usr/bin/ld: /tmp/ccmKOqOJ.o: in function `render':
OpenGl.c:(.text+0x21): undefined reference to `glClearColor'
/usr/bin/ld: OpenGl.c:(.text+0x2b): undefined reference to `glClear'
collect2: error: ld returned 1 exit status

You need to link OpenGL with your program, just like you have linked GTK+. -lOpenGL in your compiler invocation may be all you need.

Related

WebKitWebFrame not found

I'm working on a web application using WebKitGtk2 and I need to use the “window-object-cleared” signal with WebKitWebFrame. When I compile it I got an error:
main.cc:11:1: error: ‘WebKitFrame’ does not name a type
Compiled with:
c++ main.cc -w `pkg-config --cflags --libs gtk+-3.0 webkit2gtk-4.0` -o m
I checked on this and tried to use WebKitFrame instead but same thing happening.
This is my code
GtkWidget *main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(main_window), 800, 600);
WebKitWebView *webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
gtk_container_add(GTK_CONTAINER(main_window), GTK_WIDGET(webView));
g_signal_connect(main_window, "destroy", G_CALLBACK(destroyWindowCb), NULL);
g_signal_connect(webView, "window-object-cleared", G_CALLBACK(woCleared), main_window);
The callback function
void
woCleared (WebKitWebView *web_view,
WebKitWebFrame *frame, /* this line error*/
gpointer context,
gpointer window_object,
gpointer user_data){
// my code
}
Im new with the webkitgtk.

Compilation for GTK+ using OpenGL

Context: I am learning development of GUI using GTK+. I also wanted to draw lines and circles on the GUI. So I started with the tutorials and I am stuck with the part of GtkGLArea. I am following the code given in the GTK+ documentation
The error:
glTrial.cpp:32:13: error: variable or field ‘on_realize’ declared void
on_realize (GtkGLarea *area)
^
glTrial.cpp:32:13: error: ‘GtkGLarea’ was not declared in this scope
glTrial.cpp:32:24: error: ‘area’ was not declared in this scope
on_realize (GtkGLarea *area)
I believe I am not compiling properly and the compiler is not able to find correct headers.
Compilation:
g++ -std=c++14 \`pkg-config --cflags gtk+-3.0\` -o glTrial glTrial.cpp \`pkg-config --libs gtk+-3.0\`
The code:
#include <gtk/gtk.h>
#include <gtkgl-2.0/gtkgl/gdkgl.h>
#include <gtkgl-2.0/gtkgl/gtkglarea.h>
static void
print_hello (GtkWidget *widget,
gpointer user_data)
{
g_print ("Hello World\n");
}
static gboolean
render (GtkGLArea *area, GdkGLContext *context)
{
// inside this function it's safe to use GL; the given
// #GdkGLContext has been made current to the drawable
// surface used by the #GtkGLArea and the viewport has
// already been set to be the size of the allocation
// we can start by clearing the buffer
//glClearColor (0, 0, 0, 0);
// glClear (GL_COLOR_BUFFER_BIT);
// draw your object
// draw_an_object ();
// we completed our drawing; the draw commands will be
// flushed at the end of the signal emission chain, and
// the buffers will be drawn on the window
return TRUE;
}
static void
on_realize (GtkGLarea *area)
{
// We need to make the context current if we want to
// call GL API
gtk_gl_area_make_current (area);
// If there were errors during the initialization or
// when trying to make the context current, this
// function will return a #GError for you to catch
if (gtk_gl_area_get_error (area) != NULL)
return;
// You can also use gtk_gl_area_set_error() in order
// to show eventual initialization errors on the
// GtkGLArea widget itself
GError *internal_error = NULL;
init_buffer_objects (&error);
if (error != NULL)
{
gtk_gl_area_set_error (area, error);
g_error_free (error);
return;
}
init_shaders (&error);
if (error != NULL)
{
gtk_gl_area_set_error (area, error);
g_error_free (error);
return;
}
}
static void
activate (GtkApplication* app,
gpointer user_data)
{
GtkWidget *window;
GtkWidget *grid;
GtkWidget *button;
GtkWidget *gl_area =gtk_gl_area_new();
/* Create a new window, and set its title */
window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(window), "Window");
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
// gtk_window_set_default_size(GTK_WINDOW(window), 200, 200);
/* Here we construct the container that is going to pack the buttons*/
grid = gtk_grid_new();
/* Pack the container in the window */
gtk_container_add (GTK_CONTAINER (window), grid);
button = gtk_button_new_with_label ("Quit");
g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
gtk_grid_attach (GTK_GRID (grid), button, 0, 0, 2, 1);
button = gtk_button_new_with_label ("Hello");
g_signal_connect_swapped (button, "clicked", G_CALLBACK (print_hello), NULL);
gtk_grid_attach (GTK_GRID (grid), button, 0, 1, 2, 1);
/* Trial for GL area*/
g_signal_connect (gl_area, "render", G_CALLBACK(render), NULL);
gtk_grid_attach (GTK_GRID (grid), gl_area, 0, 2, 10, 10);
gtk_widget_show_all (window);
}
int
main (int argc,
char **argv)
{
GtkApplication *app;
int status;
g_application_run (G_APPLICATION (app), argc, argv);
// app = gtk_application_new("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
According to a fast google, you are looking for GtkGLArea, note the uppercase A.
– derhass
you need:
sudo apt install *epoxy*
and
c++ t.c --target=arm-linux-gnu `pkg-config --libs --cflags gtk+-3.0 epoxy ` -o op

How to build a sample project in gtk 3.0

I just started to learn programming in C++ using eclipse cdt and gtk in Ubuntu.
The packages I was able to install and configure are:
Install gtk3.0
install pkg-config from the market place
Added ${PKCFLAGS} in Cross G++ Compiler
Now: ${COMMAND} ${FLAGS} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS}${PKCFLAGS}
Added ${PKCFLAGS} in Cross G++ Linker
Now: ${COMMAND} ${FLAGS} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS}${PKCFLAGS}
I tried to run a sample program:
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
int main (int argc, char *argv[]) {
GtkWidget *window;
gtk_init (&amp;argc, &amp;argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), &quot;Window :-)&quot;);
g_signal_connect (window, &quot;destroy&quot;, G_CALLBACK (gtk_main_quit), NULL);
gtk_widget_show (window);
gtk_main ();
return 0;
}
But I am still getting an error "Unresolved inclusion: "
Run in a terminal
pkg-config --cflags gtk+-3.0
and put the output in the "compiler flags" field somewhere in eclipse's settings.
Run
pkg-config --libs gtk+-3.0
and put the output from this command in the "linker flags" field. Don't use cross compiler settings unless you are really cross compiling. This should compile as long as you code is right.
There were two problems,
I needed to add also gtk+2.0 in C/C++ Build => settings
I did not noticed but there are several issues with the code I attached it's
with some strange symbols, I changed it to this:
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
int main (int argc, char *argv[]) {
GtkWidget *window;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Hello world");
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
gtk_widget_show (window);
gtk_main ();
return 0;
}

gtk/gtk.h not found on Ubuntu c++

I'm rather new to programming and especially when it comes to how to including libraries and alike activities. I've been programming a bit using Python in the past and I've been using GTK to create windows, something I've intended to do when programming with c++ as well. To get things started, here's my code:
#include <stdlib.h>
#include <gtk/gtk.h>
static void helloWorld (GtkWidget *wid, GtkWidget *win)
{
GtkWidget *dialog = NULL;
dialog = gtk_message_dialog_new (GTK_WINDOW (win), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "Hello World!");
gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
}
int main (int argc, char *argv[])
{
GtkWidget *button = NULL;
GtkWidget *win = NULL;
GtkWidget *vbox = NULL;
/* Initialize GTK+ */
g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, (GLogFunc) gtk_false, NULL);
gtk_init (&argc, &argv);
g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, g_log_default_handler, NULL);
/* Create the main window */
win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW (win), 400, 300);
gtk_container_set_border_width (GTK_CONTAINER (win), 8);
gtk_window_set_title (GTK_WINDOW (win), "test");
gtk_window_set_position (GTK_WINDOW (win), GTK_WIN_POS_CENTER);
gtk_widget_realize (win);
g_signal_connect (win, "destroy", gtk_main_quit, NULL);
/* Create a vertical box with buttons */
vbox = gtk_vbox_new (TRUE, 6);
gtk_container_add (GTK_CONTAINER (win), vbox);
button = gtk_button_new_from_stock (GTK_STOCK_DIALOG_INFO);
g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (helloWorld), (gpointer) win);
gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);
button = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
g_signal_connect (button, "clicked", gtk_main_quit, NULL);
gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);
/* Enter the main loop */
gtk_widget_show_all (win);
gtk_main ();
return 0;
}
The code was taken from a "Hello world" example I found on the internet a while back.
I'm aware that this issue have already gotten an answer, but my situation is much more complex (from my perspective at least). First off, I've installed all packages required. I run Ubuntu 14.04 by the way.
When I compile the code using g++ main.cpp I get the following error:
main.cpp:2:21: fatal error: gtk/gtk.h: No such file or directory
#include <gtk/gtk.h>
^
compilation terminated.
There is a fix for this particular error, which is to extend the compile command like this: g++ main.cpp -I/usr/include/gtk-2.0. This however will provide another, similar error:
In file included from /usr/include/gtk-2.0/gdk/gdk.h:32:0,
from /usr/include/gtk-2.0/gtk/gtk.h:32,
from main.cpp:2:
/usr/include/gtk-2.0/gdk/gdkapplaunchcontext.h:30:21: fatal error: gio/gio.h: No such file or directory
#include <gio/gio.h>
^
compilation terminated.
You can fix this as well by extending the command like this (all commands are fund on the internet and I don't quite understand it all): g++ -g -Wall -c -o program.o main.cpp -I/usr/include/gtk-2.0 $(pkg-config --libs --cflags glib-2.0). There will now be an error with cairo.h.
As you can see there are similar errors. I have no idea what's wrong but I must believe there is a relatively easy fix.
Also, I tried a fresh install of Ubuntu (just installed packages necessary) and the same errors occur.
The pkgconfig command gives you all of the necessary -I (include) and -l (linker) statements for the compiler, when including a certain package.
Take a look what is given by running:
pkgconfig --cflags --libs gtk+-2.0
I've tried compiling your code on my Ubuntu 14.04.1 and it went fine, when I used:
g++ main.cpp `pkg-config --cflags --libs gtk+-2.0`
Probably pkg-config --libs --cflags glib-2.0 wasn't enough to provide all of the necessary include paths and shared libraries.
It's works (Ubuntu):
export CPATH=$CPATH:/usr/include/gtk-2.0
export CPATH=$CPATH:/usr/include/glib-2.0/
export CPATH=$CPATH:/usr/lib/glib-2.0/include/
export CPATH=$CPATH:/usr/include/pango-1.0/
export CPATH=$CPATH:/usr/lib/gtk-2.0/include/
export CPATH=$CPATH:/usr/include/atk-1.0/
With Fedora >=25:
export CPATH=$CPATH:/usr/include/gtk-3.0
export CPATH=$CPATH:/usr/include/glib-2.0/
export CPATH=$CPATH:/lib64/glib-2.0/include/
export CPATH=$CPATH:/usr/include/pango-1.0/
export CPATH=$CPATH:/usr/lib64/gtk-3.0/3.0.0/
export CPATH=$CPATH:/usr/include/atk-1.0/
export CPATH=$CPATH:/usr/include/cairo/
into ~/.bashrc
or
into ~/.zshrc
Remember to run: source ~/.bashrc
When you don't have a file installed on Ubuntu, the place to go is http://packages.ubuntu.com and use the second search form to look for the filename.
In this case, the answer is (for trusty):
File Package
-----------------------------------------------------------------
/usr/include/gtk-2.0/gtk/gtk.h libgtk2.0-dev
/usr/include/gtk-3.0/gtk/gtk.h libgtk-3-dev
/usr/lib/R/site-library/RGtk2/include/RGtk2/gtk.h r-cran-rgtk2
You need to select which one of those is most suitable, and install it.
I'm guessing this one: libgtk2.0-dev
You can then repeat the process for the gio/gio.h header file.
I also immigranted to C++ from Python and I am just not used with how the C++ include way of working. Here's what I do to make life easier in C++. I make a Makefile with the following contents:
all:
g++-5 main.cpp -o main `pkg-config --libs --cflags gtkmm-3.0`
Instead of main.cpp, include all your *.cpp files and headers. (Also, please notice I use gtkmm-3.0) And in gtkmm-3.0, your Application with one window, one button and the button being linked to a function [which says: Hello World! of course] would look like this
#include <iostream>
#include <gtkmm/application.h>
#include <gtkmm/window.h>
#include <gtkmm/button.h>
class HelloWorld : public Gtk::Window{
public:
HelloWorld();
virtual ~HelloWorld();
protected:
//Signal handlers:
void on_button_clicked();
//Member widgets:
Gtk::Button m_button;
};
// implementing the class HelloWorld
HelloWorld::HelloWorld(): m_button("Click me, Xweque"){
set_border_width(20); // Sets the border width of the window.
// Connecting the button's clicked signal to an arbitrary function
m_button.signal_clicked().connect(sigc::mem_fun(*this, &HelloWorld::on_button_clicked));
add(m_button);
m_button.show();
}
HelloWorld::~HelloWorld()
{
}
void HelloWorld::on_button_clicked(){
std::cout << "Hello World" << std::endl;
}
int main(int argc, char** argv){
Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
HelloWorld mywindow;
app->run(mywindow);
return 0;
}

C++ GTK make MinGW error simple program

I really need help on getting a simple GTK program in c++ running using MinGW. Here's my program:
# Makefile for Hello World Program (lab0).
all: lab0
lab0: lab0.o
g++ -Wall lab0.o -o lab0 -L C:/Users/Vic/Desktop/MinGW/lib -lgtk
lab0.o: lab0.c
g++ -Wall -I C:/Users/Vic/Desktop/MinGW/include/gtk-2.0/gtk -c lab0.c -o lab0.o
Program:
#include <gtk/gtk.h>
int main (int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *label;
gtk_init (&argc, &argv);
/* create the main, top level, window */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
/* give it the title */
gtk_window_set_title (GTK_WINDOW (window), "Hello World");
/* Connect the destroy signal of the window to gtk_main_quit
* When the window is about to be destroyed we get a notification and
* stop the main GTK+ loop
*/
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
/* Create the "Hello, World" label */
label = gtk_label_new ("Hello, World");
/* and insert it into the main window */
gtk_container_add (GTK_CONTAINER (window), label);
/* make sure that everything, window and label, are visible */
gtk_widget_show_all (window);
/* start the main loop, and let it rest there until the application is closed */
gtk_main ();
return 0;
}
when i compile this using minGW i get this error:
g++ -Wall lab0.o -o lab0 -LC:/users/vic/desktop/mingw/lib -lgtk
/bin/Id: cannot find -lgtk
collect2: Id returned 1 exit status
make: *** [lab0] Error 1
I need to fix this problem and I need to figure out how to run gtk from my makefile.
Ideally, you would use pkg-config to help you find the header and library paths:
g++ -Wall lab0.o -o lab0 `pkg-config --cflags --libs gtk+-win32-2.0`
Or just the library path:
g++ -Wall lab0.o -o lab0 -LC:/users/vic/desktop/mingw/lib -lgtk `pkg-config --libs gtk+-win32-2.0`