2

Im very new to development in C and would like to create a GUI using GTK. Ive already downloaded and installed gtk 3.6.4 bundle. Im trying to compile some example code like this:

#include <gtk/gtk.h>

int main(int argc, char *argv[])
{
    GtkWidget *window;
    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_widget_show(window);
    gtk_main();
    return 0;
}

I'm really having trouble understanding how to include the header files. I've seen a couple threads which discuss using a pkg-config command like :

pkg-config --cflags --libs gtk+-3.0

Where should this code be inserted? It runs on the command window and generates some output but this doesnt solve the error I get in MS visual studio in my code. I apologize in advance for repeating a question but I still am having trouble understanding where this pkg-config code should be run and that is not clear to me from the other responses.

Barney
  • 2,355
  • 3
  • 22
  • 37
DannyMoshe
  • 6,023
  • 4
  • 31
  • 53

1 Answers1

4

The pkg-config line is a shell command that produces on standard output the compiler flags you would need to pass to use the function you want. For example,

pkg-config --cflags gtk+-3.0

produces for me

-pthread -I/usr/include/gtk-3.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/include/at-spi-2.0 -I/usr/include/dbus-1.0 -I/usr/lib/x86_64-linux-gnu/dbus-1.0/include -I/usr/include/gtk-3.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/mirclient -I/usr/include/mircommon -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/harfbuzz -I/usr/include/pango-1.0 -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng12 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/libpng12 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include 

What you want to do is use a feature of your shell in which the output of a command is dropped into the command line of another one. In bash, this is done with `...`:

gcc -o program program.c `pkg-config --cflags --libs gtk+-3.0`

For bash, another syntax is $(...):

gcc -o program program.c $(pkg-config --cflags --libs gtk+-3.0)

For GNU make makefiles, if you want to use $(...), you will need to use the shell function:

gcc -o program program.c $(shell pkg-config --cflags --libs gtk+-3.0)

--cflags produces the C compiler flags, --libs produces the linker flags. If you're building using a makefile, you'll want to only provide --cflags to the recipe that produces the .o files and only provide --libs to the recipe that produces the final executable. For compiling a single C file directly into an executable with a single command, provide both.

andlabs
  • 11,290
  • 1
  • 31
  • 52
  • When i write the pkg-config --cflag in a dos prompt i get a huge output which doesnt really make sense. Regarding gcc -o program program.c `pkg-config --cflags --libs gtk+-3.0` command you mention above, where does this code physically need to go? Im not familiar with **shell** or **bash** and am just executing the c file in visual studio. This command gives me errors when written in my c file. Still feel like im missing something here. – DannyMoshe May 28 '15 at 00:41
  • 1
    Ah, I see. I had missed the "visual studio" in your original post and had thought you were using a Unix system/command line. In that case [this](http://www.gtk.org/download/win32_tutorial.php) explains you take the output of `pkg-config` and use it as compiler options in your Visual Studio project options. – andlabs May 28 '15 at 01:44
  • 1
    This [page](http://stackoverflow.com/questions/15906580/how-to-configure-gtk-on-visual-studio-2010) explain how to configure Visual Studio. You have to pass --msvc-syntax to pkg-config to get the syntax for Visual Studio. – Bertrand125 May 28 '15 at 06:39