How to compile C GTK3+ program in Ubuntu for windows?

8,180

Solution 1

I attempted to follow this tutorial 5 or maybe more times and I found what was wrong. I was extracting commands from the wrong directories and downloading the 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 from the directory in which my test.c file was located.

I'll make this tutorial easy for future readers.

  1. Install mingw cross-compiler by the command:

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

  3. Extract the zip file in /opt/gtkwin (recommended) by command

    sudo mkdir /opt/gtkwin && sudo unzip <name of the zip file> -d /opt/gtkwin
    
  4. Use the directory in which you have extracted the zip file. In this case I have extracted in /opt/gtkwin/ and executed the following command:

    find /opt/gtkwin -name '*.pc' -exec sudo sed -e 's@^prefix=.*@prefix=/opt/gtkwin@' -i
    
  5. Go to the directory in which you have the .c file which you which to compile and execute this command:

    export PKG_CONFIG_PATH=/opt/gtkwin/lib/pkgconfig
    
  6. Compile the program by:

    • for 64-bit:

      x86_64-w64-mingw32-gcc test.c $(pkg-config --cflags gtk+-3.0 --libs)
      
    • 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.

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

Solution 2

MXE gives ok results. The compiled executable takes a while to load up, and also brings up an empty command prompt. However, this is the only thing I tried that remotely worked.

Summary:

  • Install MXE per the official tutorial
  • x86_64-w64-mingw32.static-gcc -o OUTFILE.exe INFILE.c `x86_64-w64-mingw32.static-pkg-config --cflags --libs gtk+-3.0`
    
  • OUTFILE.exe should now be ready to run on a computer with Windows. Copy it over and double click it, or launch it from the command prompt.

Details

(Mostly summarized from their tutorial)
apt-get install autoconf automake autopoint bash bison bzip2 flex g++ g++-multilib gettext git gperf intltool libc6-dev-i386 libgdk-pixbuf2.0-dev libltdl-dev libssl-dev libtool-bin libxml-parser-perl lzip make openssl p7zip-full patch perl python ruby sed unzip wget xz-utils

git clone https://github.com/mxe/mxe.git
sudo mv mxe /opt/mxe
cd /opt/mxe
make MXE_TARGETS='x86_64-w64-mingw32.static' cc gtk3

That should create the cross compiler, linker, and appropriate libraries in /opt/mxe/usr/bin. They'll all be named something crazy like x86_64-w64-mingw32.static-pkg-config. That's a lot to type! Use makefiles or something so you don't have to type it all out every time.

Add the following line to ~/.bashrc (or whatever script your shell runs automatically):

export PATH=/opt/mxe/usr/bin:${PATH}

Close and reopen your terminal. echo $PATH should include /opt/mxe/usr/bin. You can now compile things with the command x86_64-w64-mingw32.static-gcc (used just like normal gcc). Make sure to use x86_64-w64-mingw32.static-pkg-config in place of regular pkg-config.

Notes:

Maybe MXE-compiled executeables are slow for me it is secretly embedding debug stuff. Maybe mingw can take flags exclude that? Maybe also there are flags to not open the blank command prompt?

Some of the apt-get package installs may be unnecessary, idk.

You should do make MXE_TARGETS='x86_64-w64-mingw32.static' cc gtk3 instead of just make because make generates every single package. That takes very long and will fail on random packages. Some of them can be excluded with the -o flag; for example, make -o cloog -o gdal -o graphicsmagick will exclude the cloog, gdal, and graphicsmagick packages, but that doesn't work on some packages. You should only make the specific packages you need.

MXE_TARGETS='x86_64-w64-mingw32.static' makes the 64 bit version (32 bit is default). Besides the gtk package, we need cc for gcc and pkg-config. Also note that it is gtk3, instead of just gtk like it says in their documentation.

Short guide to pkg-config for complete n00bs: To use #includes in libraries besides the C standard library and your own work, you need pkg-config to tell the C compiler where they are. Basically, the command pkg-config --cflags --libs LIBRARY_NAME, will look through a set of default directories for LIBRARY_NAME.pc files, which contain instructions to make the flags, which can then be passed as arguments to gcc.

The LIBRARY_NAME for gtk3 is gtk+-3.0, for some reason. If you can't figure out what the LIBRARY_NAME for a lib is, you can usually use the find command with a wildcard. For example, find /opt/mxe/usr -Iname gtk*.pc. The I in -Iname means case insensitive.

Share:
8,180
Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

    I am using 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 :- https://stackoverflow.com/questions/38786014/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,
                                                    ^
    
    • steeldriver
      steeldriver over 6 years
      In the first case, where did the files in /opt/gtk3-dos/lib/ come from? In the second case, are you sure you set PKG_CONFIG_PATH appropriately (so that it finds the files for the target platform, rather than those of your host system)?
    • Hi-Angel
      Hi-Angel over 6 years
      I don't have an answer, but the problem is that /usr/include/gtk* headers are platform dependent. I.e. to build gtk with mingw you'd need to use libs and includes from the /usr/i686-w64-mingw32/ (or wherever it is). See also this for details.
    • Chris1804505
      Chris1804505 over 2 years
  • Greenbeard
    Greenbeard almost 6 years
    I know that I am not supposed to use comments for "+1"ing, but I have spent a great deal of time looking for this information and really appreciate your research. I wish that it supported a version of gtk+ newer than 2013, but that is fine by me at this point.
  • CommuniTea
    CommuniTea almost 6 years
    @Greenbeard It really feels nice that it helped you. I'll sleep with more peace today
  • Mene
    Mene about 5 years
    Note: win32builder.gnome.org is also outdated now ;(
  • steve
    steve over 3 years
    I think this site contains the binaries from the broken link (or similar ones): tarnyko.net/dl
  • Chris1804505
    Chris1804505 over 2 years
    @steve GTK+ 3.6.4 is like years and years old. I suppose we now should use MSYS2: gtk.org/docs/installations/windows/…
  • Chris1804505
    Chris1804505 over 2 years
    Well.. actually to correct myself. Msys2 is for Windows machines. So we are talking about cross compiling. Maybe I and you should consider MXE: github.com/mxe/mxe
  • Chris1804505
    Chris1804505 over 2 years
    See also my reply here: stackoverflow.com/a/70726902/518879
  • Chris1804505
    Chris1804505 over 2 years
    See also my answer here: stackoverflow.com/a/70726902/518879
  • Chris1804505
    Chris1804505 about 2 years
    here you can download my pre-build binaries (MXE project): gitlab.melroy.org/melroy/gtk-3-bundle-for-windows