How to compiling C GTK3+ program in Linux mint for

2019-08-08 19:32发布

问题:

I am using Linux mint and gcc-7 and mingw-w64.I want to compile a c gtk3+ program in Linux using mingw so that it will produce .exe file which can be run in windows.I am able to Compile normal c programs using mingw,But I can't compile gtk programs.I can compile gtk programs with gcc but not with mingw.

I had installed mingw as told here :- How to compile executable for Windows with GCC with Linux Subsystem?

Now I want to compile window program which I got from https://developer.gnome.org/gtk3/stable/gtk-getting-started.html#id-1.2.3.5

 smit@smit-Aspire-5742:~/programs/c programs/test$ x86_64-w64-mingw32-gcc test.c `pkg-config --cflags gtk+-3.0 --libs`
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/libgtk-3.dll.a when searching for -lgtk-3
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/libgtk-3.dll.a when searching for -lgtk-3
 /usr/bin/x86_64-w64-mingw32-ld: cannot find -lgtk-3
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/li /libgdk-3.dll.a when searching for -lgdk-3
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/libgdk-3.dll.a when searching for -lgdk-3
 /usr/bin/x86_64-w64-mingw32-ld: cannot find -lgdk-3
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/libpangocairo-1.0.dll.a when searching for -lpangocairo-1.0
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/pangocairo-1.0.lib when searching for -lpangocairo-1.0
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/libpangocairo-1.0.dll.a when searching for -lpangocairo-1.0
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/pangocairo-1.0.lib when searching for -lpangocairo-1.0
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/pangocairo-1.0.lib when searching for -lpangocairo-1.0
 /usr/bin/x86_64-w64-mingw32-ld: cannot find -lpangocairo-1.0
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/libpangoft2-1.0.dll.a when searching for -lpangoft2-1.0
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/pangoft2-1.0.lib when searching for -lpangoft2-1.0
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/libpangoft2-1.0.dll.a when searching for -lpangoft2-1.0
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/pangoft2-1.0.lib when searching for -lpangoft2-1.0
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/pangoft2-1.0.lib when searching for -lpangoft2-1.0
 /usr/bin/x86_64-w64-mingw32-ld: cannot find -lpangoft2-1.0
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/libfreetype.dll.a when searching for -lfreetype
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/libfreetype.dll.a when searching for -lfreetype
 /usr/bin/x86_64-w64-mingw32-ld: cannot find -lfreetype
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/libfontconfig.dll.a when searching for -lfontconfig
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/fontconfig.lib when searching for -lfontconfig
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/libfontconfig.dll.a when searching for -lfontconfig
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/fontconfig.lib when searching for -lfontconfig
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/fontconfig.lib when searching for -lfontconfig
 /usr/bin/x86_64-w64-mingw32-ld: cannot find -lfontconfig
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/libpangowin32-1.0.dll.a when searching for -lpangowin32-1.0
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/pangowin32-1.0.lib when searching for -lpangowin32-1.0
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/libpangowin32-1.0.dll.a when searching for -lpangowin32-1.0
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/pangowin32-1.0.lib when searching for -lpangowin32-1.0
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/pangowin32-1.0.lib when searching for -lpangowin32-1.0
 /usr/bin/x86_64-w64-mingw32-ld: cannot find -lpangowin32-1.0
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/libpango-1.0.dll.a when searching for -lpango-1.0
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/pango-1.0.lib when searching for -lpango-1.0
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/libpango-1.0.dll.a when searching for -lpango-1.0
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/pango-1.0.lib when searching for -lpango-1.0
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/pango-1.0.lib when searching for -lpango-1.0
 /usr/bin/x86_64-w64-mingw32-ld: cannot find -lpango-1.0
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/libatk-1.0.dll.a when searching for -latk-1.0
 /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/atk-1.0.lib when searching for -latk-1.0
  /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/libatk-1.0.dll.a when searching for -latk-1.0
  /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/atk-1.0.lib when searching for -latk-1.0
  /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/atk-1.0.lib when searching for -latk-1.0
  /usr/bin/x86_64-w64-mingw32-ld: cannot find -latk-1.0
  /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/libcairo-gobject.dll.a when searching for -lcairo-gobject
  /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/libcairo-gobject.dll.a when searching for -lcairo-gobject
  /usr/bin/x86_64-w64-mingw32-ld: cannot find -lcairo-gobject
  /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/libcairo.dll.a when searching for -lcairo
  /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/cairo.lib when searching for -lcairo
  /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib/libcairo.dll.a when searching for -lcairo
  /usr/bin/x86_64-w64-mingw32-ld: skipping incompatible /opt/gtk3-dos/lib

and so .......( I am pasting the last line as this output is way more big)

 collect2: error: ld returned 1 exit status

I already went via this tutorial :-http://www.tarnyko.net/en/?q=node/45

Now vis this tutorail again but this time I had downloaded gtk+-bundle_3.6.4-20131201_win64.zip and now I am getting this output:-

 smit@smit-Aspire-5742:~/programs/c programs/test$ x86_64-w64-mingw32-gcc test.c `pkg-config --cflags gtk+-3.0 --libs`
 In file included from /usr/lib/x86_64-linux-gnu/glib-2.0/include/glibconfig.h:9:0,
             from /usr/include/glib-2.0/glib/gtypes.h:32,
             from /usr/include/glib-2.0/glib/galloca.h:32,
             from /usr/include/glib-2.0/glib.h:30,
             from /usr/include/gtk-3.0/gdk/gdkconfig.h:13,
             from /usr/include/gtk-3.0/gdk/gdk.h:30,
             from /usr/include/gtk-3.0/gtk/gtk.h:30,
             from test.c:1:
  /usr/include/glib-2.0/glib/gtypes.h: In function ‘_GLIB_CHECKED_ADD_U64’:
  /usr/include/glib-2.0/glib/gmacros.h:232:53: error: size of array ‘_GStaticAssertCompileTimeAssertion_0’ is negative
   #define G_STATIC_ASSERT(expr) typedef char G_PASTE (_GStaticAssertCompileTimeAssertion_, __COUNTER__)[(expr) ? 1 : -1] G_GNUC_UNUSED
                                                 ^
   /usr/include/glib-2.0/glib/gmacros.h:229:47: note: in definition of macro ‘G_PASTE_ARGS’
   #define G_PASTE_ARGS(identifier1,identifier2) identifier1 ## identifier2
                                           ^
    /usr/include/glib-2.0/glib/gmacros.h:232:44: note: in expansion of macro ‘G_PASTE’
    #define G_STATIC_ASSERT(expr) typedef char G_PASTE (_GStaticAssertCompileTimeAssertion_, __COUNTER__)[(expr) ? 1 : -1] G_GNUC_UNUSED
                                        ^
    /usr/include/glib-2.0/glib/gtypes.h:422:3: note: in expansion of macro ‘G_STATIC_ASSERT’
    G_STATIC_ASSERT(sizeof (unsigned long long) == sizeof (guint64));
       ^
    In file included from /usr/include/glib-2.0/gio/gio.h:46:0,
             from /usr/include/gtk-3.0/gdk/gdkapplaunchcontext.h:28,
             from /usr/include/gtk-3.0/gdk/gdk.h:32,
             from /usr/include/gtk-3.0/gtk/gtk.h:30,
             from test.c:1:
    /usr/include/glib-2.0/gio/gcredentials.h: At top level:
    /usr/include/glib-2.0/gio/gcredentials.h:75:1: error: unknown type name ‘uid_t’
    uid_t            g_credentials_get_unix_user      (GCredentials    *credentials,
     ^
    /usr/include/glib-2.0/gio/gcredentials.h:79:52: error: unknown type name ‘uid_t’
                                                uid_t           uid,
                                                ^

回答1:

I've gone via this tutorial 5 or maybe more times and I found what was wrong.I was exacting commands from wrong directories and downloading wrong version of GTK+ 3 Windows binaries .

My version of mingw is mingw64 thus I needed to download win64 version.

and I was executing command export PKG_CONFIG_PATH=/opt/gtk3-win64/lib/pkgconfig from /opt/gtk3-win64,but I needed to execute this command via directory in which my test.c file was.

I'll make this tutorial easy for future readers.

  • Install mingw cross-compiler by command

    sudo apt-get install mingw-w64
    
  • Then Download GTK+ 3 Windows binaries from here(Link in this tutorial is old,here is latest link):- http://win32builder.gnome.org/ Download appropriate version as I was downloading wrong version.don't make the same mistake as mine.

  • extract the zip file in /opt/gtkwin (recommended) by command

     sudo mkdir /opt/gtkwin && sudo unzip <name of the zip file> -d /opt/gtkwin
    
  • then Go to directory in which you have extracted the zip file.In this case I have extracted in /opt/gtkwin/ like cd /opt/gtkwin and execute the following command

    find -name '*.pc' | while read pc; do sed -e "s@^prefix=.*@prefix=$PWD@" -i "$pc"; done
    
  • Now go to you directory in which you have .c file which you which to compile.and execute this command

    export PKG_CONFIG_PATH=/opt/gtkwin/lib/pkgconfig
    
  • now compile the program by

    x86_64-w64-mingw32-gcc test.c `pkg-config --cflags gtk+-3.0 --libs`
    

    (for 64-bit)

and for 32 bit

       i586-mingw32msvc-gcc test.c `pkg-config --cflags --libs gtk+-3.0`

Also you should visit this tutorial as I had learn this from there :- http://www.tarnyko.net/en/?q=node/45

this will create .exe file which can't run in UNIX as it is targeted to run on DOS