Search code examples
cgccgtk3msys2pkg-config

How do I solve the gcc error on finding pkg-config?


This question seems to have been asked a zillion times, but unfortunately, none of the answers helped me.

I believe I have installed gtk correctly. My pkg-config environment seems okay. I'm not so sure of gcc. I removed all the mingw and cygwin folders from my path environment, as I am only going to be using msys2 packages. So I have my path environment this way:-

GTK_LIB_DIR C:\msys64\mingw64\lib

PKG_CONFIG_PATH C:\msys64\mingw64\lib\pkgconfig

Path - C:\msys64\usr\bin;C:\msys64\mingw64\bin;%GTK_LIB_DIR%

The problem I am having is with compilation. Running this... gcc pkg-config --cflags gtk+-3.0 -o gtkTest C:\dev\test_gtk3.c pkg-config --libs gtk+-3.0

I get...

cc: error: `pkg-config: No such file or directory
gcc: error: gtk+-3.0`: No such file or directory
gcc: error: `pkg-config: No such file or directory
gcc: error: gtk+-3.0`: No such file or directory
gcc: error: unrecognized command line option '--cflags'
gcc: error: unrecognized command line option '--libs'

I can run the following commands separately, with expected results. e.g.

pkg-config --cflags gtk+-3.0
pkg-config --libs gtk+-3.0
gcc -o gtkTest C:\dev\test_gtk3.c

Of course the last command returns an error, since gtk is not referenced. I tried various combinations, some of my own. Some recommended, but one error persists, on pkg-config. e.g. C:\WINDOWS\system32>gcc -o gtkTest C:\dev\test_gtk3.c "pkg-config --cflags gtk+-3.0 pkg-config --libs gtk+-3.0"

gcc: error: pkg-config --cflags gtk+-3.0 pkg-config --libs gtk+-3.0: No such file or directory

The last answer I found says "If pkg-config reports that it couldn't find the package, then you didn't install the Gtk development package as offered by your Linux distribution."

I'm using Windows, but I think I installed gtk correctly, since I followed the instructions, and I get what seems to be the expected results (please correct me if I am mistaken).

C:\WINDOWS\system32>pkg-config --cflags gtk+-3.0

-pthread -mms-bitfields -I/mingw64/include/gtk-3.0 -I/mingw64/include/cairo -I/m
ingw64/include -I/mingw64/include/pango-1.0 -I/mingw64/include/fribidi -I/mingw6
4/include -I/mingw64/include/atk-1.0 -I/mingw64/include/cairo -I/mingw64/include
/pixman-1 -I/mingw64/include -I/mingw64/include/freetype2 -I/mingw64/include -I/
mingw64/include/harfbuzz -I/mingw64/include -I/mingw64/include/libpng16 -I/mingw
64/include/gdk-pixbuf-2.0 -I/mingw64/include -I/mingw64/lib/libffi-3.2.1/include
 -I/mingw64/include/glib-2.0 -I/mingw64/lib/glib-2.0/include -I/mingw64/include

C:\WINDOWS\system32>pkg-config --libs gtk+-3.0

-L/mingw64/lib -L/mingw64/lib/../lib -L/mingw64/lib -lgtk-3 -lgdk-3 -lz -lgdi32
-limm32 -lshell32 -lole32 -Wl,-luuid -lwinmm -ldwmapi -lsetupapi -lcfgmgr32 -lep
oxy -lopengl32 -lgdi32 -lpangocairo-1.0 -lm -lgdi32 -lpangoft2-1.0 -lm -lpangowi
n32-1.0 -lm -lusp10 -lgdi32 -lpango-1.0 -lm -lfribidi -lthai -ldatrie -latk-1.0
-lcairo-gobject -lcairo -lz -lpixman-1 -lm -pthread -lfontconfig -liconv -lexpat
 -lfreetype -lbz2 -lharfbuzz -lm -lusp10 -lgdi32 -lrpcrt4 -lgraphite2 -lpng16 -l
z -lgdk_pixbuf-2.0 -lm -lgio-2.0 -pthread -lintl -lshlwapi -ldnsapi -liphlpapi -
lws2_32 -lgmodule-2.0 -pthread -lintl -lz -lgobject-2.0 -pthread -lintl -lffi -l
glib-2.0 -lintl -lws2_32 -lole32 -lwinmm -lshlwapi -pthread -lm -lpcre

C:\WINDOWS\system32>gcc -o gtkTest C:\dev\test_gtk3.c | pkg-config --cflags --libs gtk+-3.0

-pthread -mms-bitfields -I/mingw64/include/gtk-3.0 -I/mingw64/include/cairo -I/m
ingw64/include -I/mingw64/include/pango-1.0 -I/mingw64/include/fribidi -I/mingw6
4/include -I/mingw64/include/atk-1.0 -I/mingw64/include/cairo -I/mingw64/include
/pixman-1 -I/mingw64/include -I/mingw64/include/freetype2 -I/mingw64/include -I/
mingw64/include/harfbuzz -I/mingw64/include -I/mingw64/include/libpng16 -I/mingw
64/include/gdk-pixbuf-2.0 -I/mingw64/include -I/mingw64/lib/libffi-3.2.1/include
 -I/mingw64/include/glib-2.0 -I/mingw64/lib/glib-2.0/include -I/mingw64/include
-L/mingw64/lib -L/mingw64/lib/../lib -L/mingw64/lib -lgtk-3 -lgdk-3 -lz -lgdi32
-limm32 -lshell32 -lole32 -Wl,-luuid -lwinmm -ldwmapi -lsetupapi -lcfgmgr32 -lep
oxy -lopengl32 -lgdi32 -lpangocairo-1.0 -lm -lgdi32 -lpangoft2-1.0 -lm -lpangowi
n32-1.0 -lm -lusp10 -lgdi32 -lpango-1.0 -lm -lfribidi -lthai -ldatrie -latk-1.0
-lcairo-gobject -lcairo -lz -lpixman-1 -lm -pthread -lfontconfig -liconv -lexpat
 -lfreetype -lbz2 -lharfbuzz -lm -lusp10 -lgdi32 -lrpcrt4 -lgraphite2 -lpng16 -l
z -lgdk_pixbuf-2.0 -lm -lgio-2.0 -pthread -lintl -lshlwapi -ldnsapi -liphlpapi -
lws2_32 -lgmodule-2.0 -pthread -lintl -lz -lgobject-2.0 -pthread -lintl -lffi -l
glib-2.0 -lintl -lws2_32 -lole32 -lwinmm -lshlwapi -pthread -lm -lpcre
C:\dev\test_gtk3.c:1:10: fatal error: gtk/gtk.h: No such file or directory
    1 | #include <gtk/gtk.h>
      |          ^~~~~~~~~~~
compilation terminated.

Has anyone here struggled with this for hours, like I have, and finally got to understand what the problem is. Please share your solution. Thank you.


Solution

  • Using this Installing gtk and compiling using gcc under windows? helped me get past the errors. I used the following suggestion.

    "Instead of a batch file, you may find more convenient to create an user environmental variable and store the flags in there, that way you will be able to compile from within a normal command prompt."

    So I created two new environment variables (e.g. GTK_FLAGS_VARS; GTK_LIBS_VARS), and pasted the outputs in the variable values. Then in path place %GTK_FLAGS_VARS%;%GTK_LIBS_VARS% Afterward gcc -o gtkTest C:\dev\test_gtk3.c %GTK_FLAGS_VARS% %GTK_LIBS_VARS%