如何在 Windows 版 Ubuntu 中编译 C GTK3+ 程序?

如何在 Windows 版 Ubuntu 中编译 C GTK3+ 程序?

我正在使用 gcc-7 和 mingw-w64。我想使用 mingw 在 Linux 中编译 c gtk3+ 程序,以便它将生成可以在 Windows 中运行的 .exe 文件。我能够使用 mingw 编译普通 c 程序,但我不能编译 gtk 程序。我可以用 gcc 编译 gtk 程序,但不能用 mingw 编译。

我已经按照这里所述安装了 mingw :-https://stackoverflow.com/questions/38786014/how-to-compile-executable-for-windows-with-gcc-with-linux-subsystem

现在我想编译从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

所以......(我粘贴最后一行因为这个输出太大了)

 collect2: error: ld returned 1 exit status

我已经完成了本教程:-http://www.tarnyko.net/en/?q=node/45

现在可见再次使用 tutorialal,但这次我已经下载了gtk+-bundle_3.6.4-20131201_win64.zip,现在我得到了这个输出:-

 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

我尝试跟随教程 5 次或更多次,我发现了错误。我从错误的目录中提取命令并下载了错误版本的GTK+ 3 Windows 二进制文件

我的mingw版本是mingw64,因此我需要下载win64版本。

我正在export PKG_CONFIG_PATH=/opt/gtk3-win64/lib/pkgconfig从 /opt/gtk3-win64 执行命令,但我需要从我的 test.c 文件所在的目录执行该命令。

我会让本教程对未来的读者来说更简单。

  1. mingw cross-compiler通过命令安装:

    sudo apt-get install mingw-w64
    
  2. 下载GTK+ 3 Windows 二进制文件从这里(链接本教程已经很旧了,这里是最新链接:http://win32builder.gnome.org/)。下载合适的版本,因为我下载的是错误的版本。不要犯和我一样的错误。

  3. 使用命令解压 zip 文件/opt/gtkwin(推荐)

    sudo mkdir /opt/gtkwin && sudo unzip <name of the zip file> -d /opt/gtkwin
    
  4. 使用您解压 zip 文件的目录。在本例中,我已解压/opt/gtkwin/并执行以下命令:

    find /opt/gtkwin -name '*.pc' -exec sudo sed -e 's@^prefix=.*@prefix=/opt/gtkwin@' -i
    
  5. 转到您要.c编译的文件的目录并执行此命令:

    export PKG_CONFIG_PATH=/opt/gtkwin/lib/pkgconfig
    
  6. 通过以下方式编译程序:

    • 对于 64 位:

      x86_64-w64-mingw32-gcc test.c $(pkg-config --cflags gtk+-3.0 --libs)
      
    • 对于 32 位:

      i586-mingw32msvc-gcc test.c $(pkg-config --cflags --libs gtk+-3.0)
      

你也应该参观本教程因为我从那里学到了这一点。

这将创建一个.exe无法在 UNIX 中运行的文件,因为它的目标是在 DOS 上运行。

答案2

MXE 的结果不错。编译后的可执行文件需要一段时间才能加载,并且还会显示一个空的命令提示符。但是,这是我尝试过的唯一可以远程工作的东西。

概括:

  • 安装 MXE官方教程
  • 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现在应该可以在装有 Windows 的计算机上运行了。将其复制并双击,或者从命令提示符启动它。

细节

(大部分是从他们的教程中总结出来的)
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

这应该会在 中创建交叉编译器、链接器和相应的库/opt/mxe/usr/bin。它们都会被命名为 之类的奇怪名字x86_64-w64-mingw32.static-pkg-config。这需要输入很多内容!使用 makefile 或其他东西,这样您就不必每次都输入所有内容。

添加以下行~/.bashrc(或您的 shell 自动运行的任何脚本):

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

关闭并重新打开终端。echo $PATH应该包括/opt/mxe/usr/bin。现在您可以使用命令编译内容x86_64-w64-mingw32.static-gcc(使用方式与正常一样gcc)。确保使用x86_64-w64-mingw32.static-pkg-config代替常规pkg-config

笔记:

也许 MXE 编译的可执行文件对我来说很慢,因为它秘密嵌入了调试内容。也许 mingw 可以使用标志来排除它?也许还有标志可以不打开空白命令提示符?

有些apt-get包的安装可能是不必要的,我不知道。

您应该这样做,make MXE_TARGETS='x86_64-w64-mingw32.static' cc gtk3而不是仅仅make因为make生成每个包。这会花费很长时间,并且会在随机包上失败。其中一些可以使用 -o 标志排除;例如,make -o cloog -o gdal -o graphicsmagick将排除clooggdalgraphicsmagick包,但这对某些包不起作用。您应该只制作您需要的特定包。

MXE_TARGETS='x86_64-w64-mingw32.static'生成 64 位版本(默认为 32 位)。除了gtk包之外,我们还需要ccgccpkg-config还请注意,它是gtk3,而不是gtk像其文档中所说的那样。

pkg-config完全新手的简短指南:要#includes在 C 标准库和您自己的工作之外的库中使用,您需要pkg-config告诉 C 编译器它们在哪里。基本上,命令pkg-config --cflags --libs LIBRARY_NAME将在一组默认目录中查找LIBRARY_NAME.pc文件,其中包含制作标志的指令,然后可以将其作为参数传递给gcc

出于某种原因, forLIBRARY_NAME是。如果您无法弄清楚for a lib 是什么,通常可以使用带通配符gtk3的命令。例如,。in表示不区分大小写。gtk+-3.0LIBRARY_NAMEfindfind /opt/mxe/usr -Iname gtk*.pcI-Iname

相关内容