Author Topic: Interface Programming using C's gtk libraries  (Read 897 times)

0 Members and 2 Guests are viewing this topic.

Offline matias

  • /dev/null
  • *
  • Posts: 6
  • Cookies: 0
    • View Profile
Interface Programming using C's gtk libraries
« on: February 19, 2016, 06:59:07 pm »
Lately I've been watching some lectures about Java online. It amazed me how in my second day of learning I reached the topic of Objects, creating windows, using labels, and so on. Next thing I did was searching on Google how to write such codes and programming user interfaces in C.

It appears to be that GTK C libraries handle this part of C programming, but that's all I could find about the topic until now. I visited the website http://www.gtk.org/ and tried to install the latest version , but after a couple of hours and tons of errors (I use Ubuntu 15.10) I gave up. On the other hand, it seems that I already have an older version of gtk installed, so installing the latest version isn't completely necessary.

Problem is, I tried to do an example, and included the <gtk/gtk.h> library, but the compiler doesn't recognize it...

Anyone with a little experience at this?
printf("%s\n", "Sometimes I like doing redundant things for no reason");

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: Interface Programming using C's gtk libraries
« Reply #1 on: February 19, 2016, 11:36:15 pm »
Don't install GTK from source on Ubuntu.  Use the already-provided packages.  There are development packages available, find them.  Having gtk libraries installed for use is different from having them installed for development.  You should probably be looking for 'libgtk-3-dev', I believe, but I don't run Ubuntu 15.10 so I can't be positive.
-Xires

Offline iikibT

  • Serf
  • *
  • Posts: 41
  • Cookies: 7
    • View Profile
Re: Interface Programming using C's gtk libraries
« Reply #2 on: February 19, 2016, 11:47:59 pm »
You need GTK for development:
Code: [Select]
apt-get install libgtk2.0-devor
Code: [Select]
apt-get install libgtk-3-dev
And then use it when compiling, for example:
Code: [Select]
gcc <your_file> `pkg-config --cflags --libs gtk+-2.0`
Hacking for no fun and no profit

Offline matias

  • /dev/null
  • *
  • Posts: 6
  • Cookies: 0
    • View Profile
Re: Interface Programming using C's gtk libraries
« Reply #3 on: February 20, 2016, 01:15:53 am »
Okey, So I got a program called 'int.c' and I typed the next thing:
gcc int.c pkg-config --cflags --libs gtk+-2.0

And got the next errors:
Code: [Select]
gcc: error: pkg-config: No existe el archivo o el directorio (=file or directory does not exist)
gcc: error: gtk+-2.0: No existe el archivo o el directorio ( " )
gcc: error: unrecognized command line option ‘--cflags’
gcc: error: unrecognized command line option ‘--libs’
printf("%s\n", "Sometimes I like doing redundant things for no reason");

Offline iikibT

  • Serf
  • *
  • Posts: 41
  • Cookies: 7
    • View Profile
Re: Interface Programming using C's gtk libraries
« Reply #4 on: February 20, 2016, 01:22:12 am »
Okey, So I got a program called 'int.c' and I typed the next thing:
gcc int.c pkg-config --cflags --libs gtk+-2.0

You are missing ` around options, that's why gcc thinks you want to compile files named 'pkg-config' etc. and complains that it can't find them. Your command should be:

Code: [Select]
gcc int.c `pkg-config --cflags --libs gtk+-2.0`

EDIT: continuing troubleshooting this command with matias in PM, as it isn't that relevant to the topic and I don't want to spam the thread too hard.
« Last Edit: February 20, 2016, 10:35:22 am by iikibT »
Hacking for no fun and no profit

Offline matias

  • /dev/null
  • *
  • Posts: 6
  • Cookies: 0
    • View Profile
Re: Interface Programming using C's gtk libraries
« Reply #5 on: February 20, 2016, 01:34:34 am »
Sorry! Corrected it. Now I got this:

Code: [Select]
gcc: error: pkg-config --cflags --libs gtk+-2.0: No existe el archivo o el directorio

`~`
printf("%s\n", "Sometimes I like doing redundant things for no reason");

Offline archfox

  • /dev/null
  • *
  • Posts: 12
  • Cookies: 1
    • View Profile
Re: Interface Programming using C's gtk libraries
« Reply #6 on: February 23, 2016, 09:19:25 pm »
*me trying to understand your end goal*

If it is for learning purposes, then it wouldn't hurt to have some experience.

From the perspective of building some end product with UI written in C, then why?
Unless you want to contribute to the development.

Offline matias

  • /dev/null
  • *
  • Posts: 6
  • Cookies: 0
    • View Profile
Re: Interface Programming using C's gtk libraries
« Reply #7 on: February 24, 2016, 02:27:15 pm »
Contributing to the development is not my goal, no. I just see how easy things seem to be done in other languages, and how learning those things in C often gives you a wider understanding of the entire topic in question.

Take structures for example. In my case, it took me a few weeks to finally understand all their applications and how to use them (and I'm still struggling sometimes); pointers to structures, array of structures, functions returning pointers to structures, etc.

In Java, they call them Classes and Objects, tell you a little about the notation, and that's it, at least so far... And I'm not saying Java is a bad programming languaje, not at all. I'm saying: "If it is that easy in Java, sure there must be some badass code in C behind it"

My end goal is to learn it.
printf("%s\n", "Sometimes I like doing redundant things for no reason");

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: Interface Programming using C's gtk libraries
« Reply #8 on: February 25, 2016, 10:07:32 pm »
Structs and classes are very different things.  A structure is just a contiguous spot in memory allocated such that different sections can be accessed as different variables.  I can provide some examples if you need them.

As for GTK+..make sure that you have pkg-config installed(it should be, but it might not be).  Once you're certain it's installed(which can be tested via "pkg-config --version") then you'll want to make sure that it can provide you with the cflags(compilation flags) and libs needed for compiling & linking your project(using "pkg-config --cflags --libs gtk+-3.0"; note there are no backquotes).  Next you'll throw together a simple test program.  Note that the test program need not be an elaborate example(which the documentation-provided 'hello world' is, IMHO), but rather just need something that will compile & link properly with the selected package.

Code: (bash) [Select]
xires@Synthesium:~/proj/code/c$ pkg-config --version
0.26
xires@Synthesium:~/proj/code/c$ pkg-config --cflags --libs gtk+-3.0
-pthread -I/usr/local/include/libpng16 -I/usr/include/gtk-3.0 -I/usr/include/pango-1.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/atk-1.0 -I/usr/include/cair
o -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/freetype2 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/i
nclude/libpng12  -lgtk-3 -lgdk-3 -latk-1.0 -lgio-2.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lcairo-gobject -lpango-1.0 -lcairo -lgobject-2.0 -lglib-2.0 
xires@Synthesium:~/proj/code/c$ cat gtktst.c
Code: (c) [Select]
#include <gtk/gtk.h>

int main(int argc, char **argv) {
    gtk_init(&argc, &argv);

    return 0;
}
Code: (bash) [Select]
xires@Synthesium:~/proj/code/c$ gcc -Wall -Wextra -W -ansi -pedantic -o gtktst{,.c} `pkg-config --cflags --libs gtk+-3.0`
xires@Synthesium:~/proj/code/c$ ldd gtktst | grep gtk
        libgtk-3.so.0 => /usr/lib/x86_64-linux-gnu/libgtk-3.so.0 (0x00007f8a1e107000)
xires@Synthesium:~/proj/code/c$

If you've made it to this point successfully, then you can start working on more elaborate examples and stepping through the documentation.

Personal notes:
Honestly, I've worked with GTK quite a bit in the past and I have to warn you, it's a bit of a horrific mess.  You can certainly get through it but be sure you understand the language properly.  If GTK starts bringing in questions about how C works due to a specific feature or something then it's time to step back and make sure you really know C.

I love C, but I personally feel that GUI development is a better task for C++.  C++ provides the necessary features to help facilitate not just OOP, but also proper event-driven design.  There are also more(and some might say 'better') frameworks available with C++.  GTK is still an option(with gtkmm) but you also get Qt(a FAR better framework to learn, especially if you're just starting out), wxWindows(which is also great, by comparison), and FLTK(which I like, but research it first).  Of the C++-available options, I'd suggest learning Qt first as it has a large following and you'll find it more useful professionally(as in, it's worth putting on your CV).

With GTK often comes Glib, which is its own mess.  It's useful in some ways and provides common data structures, but it lacks...grace.  The documentation for both GTK & Glib is also lacking, in my opinion.  Of course, GTK & Glib are both worth learning, eventually, but be aware that it takes some tenacity to get through.
-Xires