3

System: Windows7, 32 bit, GTK 2.24.10, mingw
I am trying to write basic helloworld.c type GTK based application. However, it doesn't run.
These are the steps which I followed.

  1. Install MinGW.
  2. Download GTK+ all in one bundle.
  3. Extract content in C:\gtk folder.
  4. Open cmd and go to C:\gtk\bin directory and run pkg-config --cflags --libs gtk+-win32-2.0
  5. It prints list of compilation flags, and libraries to link your project to. Copy them and create a bath file as follows. set VAR=FLAGS start cmd where VAR = GTK, and FLAGS = output of the previous command (pkg-config). When you want to compile file use command : gcc foo.c %VAR%

D:\gtk>gcc -o project helloworld.c %GTK%
gcc: %GTK%: No such file or directory helloworld.c:1:21: error: gtk/gtk.h: No such file or directory helloworld.c: In function 'main': helloworld.c:5: error: 'GtkWidget' undeclared (first use in this function) helloworld.c:5: error: (Each undeclared identifier is reported only once helloworld.c:5: error: for each function it appears in.) helloworld.c:5: error: 'window' undeclared (first use in this function) helloworld.c:9: error: 'GTK_WINDOW_TOPLEVEL' undeclared (first use in this function)

D:\gtk>gcc -Wall -g helloworld.c -o helloworld pkg-config --cflags gtk+-2.0 pkg-config --libs gtk+-2.0
gcc: pkg-config: No such file or directory
gcc: gtk+-2.0: No such file or directory
gcc: pkg-config: No such file or directory
gcc: gtk+-2.0: No such file or directory
cc1.exe: error: unrecognized command line option "-fcflags"
cc1.exe: error: unrecognized command line option "-flibs"

batch file in D:\gtk

set GTK=-mms-bitfields -IC:/gtk/include/gtk-2.0 -IC:/gtk/lib/gtk-2.0/include -IC:/gtk/include/atk-1.0 -IC:/gtk/include/cairo -IC:/gtk/include/gdk-pixbuf-2.0 -IC:/gtk/include/pango-1.0 -IC:/gtk/include/glib-2.0 -IC:/gtk/lib/glib-2.0/include -IC:/gtk/include -IC:/gtk/include/freetype2 -IC:/gtk/include/libpng14  -LC:/gtk/lib -lgtk-win32-2.0 -lgdk-win32-2.0 -latk-1.0 -lgio-2.0 -lpangowin32-1.0 -lgdi32 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lpango-1.0 -lcairo -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lglib-2.0 -lintl
start cmd

helloworld.c

#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;
}

Reference : Installing gtk and compiling using gcc under windows?

3 Answers 3

6

You could try these manual steps to start with:

1) At your command prompt run the pkg-config command to get your include flags:

c:\dev\gtk224\bin\pkg-config.exe --cflags gtk+-2.0

This is my output:

-mms-bitfields -Ic:/dev/gtk224/include/gtk-2.0 -Ic:/dev/gtk224/lib/gtk-2.0/include -Ic:/dev/gtk224/include/atk-1.0 -Ic:/dev/gtk224/include/cairo -Ic:/dev/gtk224/include/gdk-pixbuf-2.0 -Ic:/dev/gtk224/include/pango-1.0 -Ic:/dev/gtk224/include/glib-2.0 -Ic:/dev/gtk224/lib/glib-2.0/include -Ic:/dev/gtk224/include -Ic:/dev/gtk224/include/freetype2 -Ic:/dev/gtk224/include/libpng14

2) set the output from (1) to a variable GTK_INCLUDES:

C:\dev\1_repo\gtk_scratch>set GTK_INCLUDES=-mms-bitfields -Ic:/dev/gtk224/include/gtk-2.0 -Ic:/dev/gtk224/lib/gtk-2.0/include -Ic:/dev/gtk224/include/atk-1.0 -Ic:/dev/gtk224/include/cairo -Ic:/dev/gtk224/include/gdk-pixbuf-2.0 -Ic:/dev/gtk224/include/pango-1.0 -Ic:/dev/gtk224/include/glib-2.0 -Ic:/dev/gtk224/lib/glib-2.0/include -Ic:/dev/gtk224/include -Ic:/dev/gtk224/include/freetype2 -Ic:/dev/gtk224/include/libpng14

(make sure you use YOUR output from step (1))

3) do the same as step 1 for the library flags:

c:\dev\gtk224\bin\pkg-config.exe --libs gtk+-2.0

This is my output:

-Lc:/dev/gtk224/lib -lgtk-win32-2.0 -lgdk-win32-2.0 -latk-1.0 -lgio-2.0 -lpangowin32-1.0 -lgdi32 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lpango-1.0 -lcairo -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lglib-2.0 -lintl

4) set output from (3) to a variable GTK_LIBS

C:\dev\1_repo\gtk_scratch>set GTK_LIBS=-Lc:/dev/gtk224/lib -lgtk-win32-2.0 -lgdk-win32-2.0 -latk-1.0 -lgio-2.0 -lpangowin32-1.0 -lgdi32 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lpango-1.0 -lcairo -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lglib-2.0 -lintl

(make sure you use YOUR output from step (3))

5) make sure gtk+ and MinGW are on your path:

set PATH=c:\dev\MinGW\bin\;c:\dev\gtk224\bin

(make sure you set your path to YOUR mingw and gtk directories)

6) compile:

c:\dev\MinGW\bin\gcc.exe -g helloworld.c -o helloworld %GTK_INCLUDES% %GTK_LIBS%

7) when you are able to compile OK, copy what you did in steps 2,4,5 and 6 to a batch file so can compile you app just by running the batch file.

Sign up to request clarification or add additional context in comments.

3 Comments

Although it solved my problem. But I am curious about where this set GTK_INCLUDES and GTK_LIBS get saved. I tried looking in Environmental Setting and using power shell get-childitem env: , but could not find those variables.
The SET command only saves the value in memory for the current session (I.e. the command window you run SET in). Other command windows will not see the value that was set (unless the other command windows are children). Did you run the PS command from the same command window (or from a child) that you run SET in?
oops! yes, I used cmd prompt and ps prompt from two different windows. (different instances). thanks for explaining.
3

The error is right here.

pkg-config is a utility which helps (and I strongly recommend) to determine link and lib flags. The issue you got is that gcc interprets it as a parameter if you pass them like you do - you need to exectue them in a subshell (but I have no clue how to do that under windows shell or cygwin) under bash it is either $(pkconfig --libs gtk-2.0) or with backticks around instead of $(...)

D:\gtk>gcc -Wall -g helloworld.c -o helloworld pkg-config --cflags gtk+-2.0 pkg-config --libs gtk+-2.0
gcc: pkg-config: No such file or directory
gcc: gtk+-2.0: No such file or directory
gcc: pkg-config: No such file or directory
gcc: gtk+-2.0: No such file or directory
cc1.exe: error: unrecognized command line option "-fcflags"
cc1.exe: error: unrecognized command line option "-flibs"

3 Comments

Thanks, i found something similar: stackoverflow.com/questions/18799904/…. The ninth steps mentions use of bash.exe which is not in my bin folder, but then again it creates a batch command, don't know how to execute same in cmd prompt.
I strongly recommend Cygwin for situations like this. Use the Cygwin bash shell, and MinGW. Stop shooting yourself in the foot and do it the easy way.
drahnr : your opinion was right, back ticks, were one of the cause of problem.
2

Get same error when running hello, world program. Following solution work for me.

Instead of saving your helloworld.c at any arbitrary place, put it inside of

MinGW > msys > 1.0 > home > "Name of Your home folder" > helloworld.c

Now, open msys.bat and write the command to run program. In my case it was:

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

And it works for me!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.