0

我用 gcc 和 musl-gcc 编译了这个 GTK+ 示例应用程序。两者都产生了一个二进制文件,但是 musl-gcc 产生的二进制文件不能正确执行并显示一堆错误。

注意:我安装了 musl-libc 和 libgtk3 开发版本。
注意: ldd 命令日志对于 gcc 和 musl-gcc 生成的二进制文件几乎是相同的。

GTK+ 示例应用程序:

#include <gtk/gtk.h>

static void activate (GtkApplication* app, gpointer user_data) {
   GtkWidget *window;
   window = gtk_application_window_new (app);
   gtk_window_set_title (GTK_WINDOW (window), "Window");
   gtk_window_set_default_size (GTK_WINDOW (window), 600, 600);
   gtk_widget_show_all (window);
}

int main (int argc, char **argv) {
    GtkApplication *app;
    int status;

    app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
    g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
    status = g_application_run (G_APPLICATION (app), argc, argv);
    g_object_unref (app);

    return status;
}

编译命令:

musl-gcc -Wall -g -o main main.c -I/usr/include/gtk-3.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/include/at-spi-2.0 - I/usr/include/dbus-1.0 -I/usr/lib/x86_64-linux-gnu/dbus-1.0/include -I/usr/include/gtk-3.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/harfbuzz -I/usr/include/pango-1.0 -I/usr/include/atk-1.0 -I/usr /include/cairo -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr /include/gdk-pixbuf-2.0 -I/usr/include/libpng16 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -L/usr/lib /x86_64-linux-gnu -lgtk-3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0

musl-gcc 生成的二进制日志:

Error loading shared library libgtk-3.so.0: No such file or directory (needed by ./main)
Error loading shared library libgdk-3.so.0: No such file or directory (needed by ./main)
Error loading shared library libpangocairo-1.0.so.0: No such file or directory (needed by ./main)
Error loading shared library libpango-1.0.so.0: No such file or directory (needed by ./main)
Error loading shared library libatk-1.0.so.0: No such file or directory (needed by ./main)
Error loading shared library libcairo-gobject.so.2: No such file or directory (needed by ./main)
Error loading shared library libcairo.so.2: No such file or directory (needed by ./main)
Error loading shared library libgdk_pixbuf-2.0.so.0: No such file or directory (needed by ./main)
Error loading shared library libgio-2.0.so.0: No such file or directory (needed by ./main)
Error loading shared library libgobject-2.0.so.0: No such file or directory (needed by ./main)
Error loading shared library libglib-2.0.so.0: No such file or directory (needed by ./main)
Error relocating ./main: gtk_application_new: symbol not found
Error relocating ./main: gtk_application_window_new: symbol not found
Error relocating ./main: g_application_get_type: symbol not found
Error relocating ./main: g_type_check_instance_cast: symbol not found
Error relocating ./main: g_application_run: symbol not found
Error relocating ./main: gtk_window_set_title: symbol not found
Error relocating ./main: gtk_window_set_default_size: symbol not found
Error relocating ./main: g_signal_connect_data: symbol not found
Error relocating ./main: gtk_widget_show_all: symbol not found
Error relocating ./main: g_object_unref: symbol not found
Error relocating ./main: gtk_window_get_type: symbol not found

gcc 生成的二进制文件的ldd命令日志:

linux-vdso.so.1 (0x00007ffe90ff4000)
libgtk-3.so.0 => /usr/lib/x86_64-linux-gnu/libgtk-3.so.0 (0x00007f9f93fec000)
libgio-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgio-2.0.so.0 (0x00007f9f93c4d000)
libgobject-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0 (0x00007f9f939f9000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f9f93608000)
libgdk-3.so.0 => /usr/lib/x86_64-linux-gnu/libgdk-3.so.0 (0x00007f9f93312000)
libgmodule-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgmodule-2.0.so.0 (0x00007f9f9310e000)
libpangocairo-1.0.so.0 => /usr/lib/x86_64-linux-gnu/libpangocairo-1.0.so.0 (0x00007f9f92f01000)
libX11.so.6 => /usr/lib/x86_64-linux-gnu/libX11.so.6 (0x00007f9f92bc9000)
libXi.so.6 => /usr/lib/x86_64-linux-gnu/libXi.so.6 (0x00007f9f929b9000)
libXfixes.so.3 => /usr/lib/x86_64-linux-gnu/libXfixes.so.3 (0x00007f9f927b3000)
libcairo-gobject.so.2 => /usr/lib/x86_64-linux-gnu/libcairo-gobject.so.2 (0x00007f9f925aa000)
libcairo.so.2 => /usr/lib/x86_64-linux-gnu/libcairo.so.2 (0x00007f9f9228d000)
libgdk_pixbuf-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgdk_pixbuf-2.0.so.0 (0x00007f9f92069000)
libatk-1.0.so.0 => /usr/lib/x86_64-linux-gnu/libatk-1.0.so.0 (0x00007f9f91e43000)
libatk-bridge-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libatk-bridge-2.0.so.0 (0x00007f9f91c12000)
libepoxy.so.0 => /usr/lib/x86_64-linux-gnu/libepoxy.so.0 (0x00007f9f91911000)
libpangoft2-1.0.so.0 => /usr/lib/x86_64-linux-gnu/libpangoft2-1.0.so.0 (0x00007f9f916fb000)
libpango-1.0.so.0 => /usr/lib/x86_64-linux-gnu/libpango-1.0.so.0 (0x00007f9f914ae000)
libfontconfig.so.1 => /usr/lib/x86_64-linux-gnu/libfontconfig.so.1 (0x00007f9f91269000)
libglib-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007f9f90f53000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f9f90bb5000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f9f90996000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f9f90779000)
libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f9f90551000)
libresolv.so.2 => /lib/x86_64-linux-gnu/libresolv.so.2 (0x00007f9f90336000)
libmount.so.1 => /lib/x86_64-linux-gnu/libmount.so.1 (0x00007f9f900e2000)
libffi.so.6 => /usr/lib/x86_64-linux-gnu/libffi.so.6 (0x00007f9f8feda000)
/lib64/ld-linux-x86-64.so.2 (0x00007f9f94af6000)
libXinerama.so.1 => /usr/lib/x86_64-linux-gnu/libXinerama.so.1 (0x00007f9f8fcd7000)
libXrandr.so.2 => /usr/lib/x86_64-linux-gnu/libXrandr.so.2 (0x00007f9f8facc000)
libXcursor.so.1 => /usr/lib/x86_64-linux-gnu/libXcursor.so.1 (0x00007f9f8f8c2000)
libXcomposite.so.1 => /usr/lib/x86_64-linux-gnu/libXcomposite.so.1 (0x00007f9f8f6bf000)
libXdamage.so.1 => /usr/lib/x86_64-linux-gnu/libXdamage.so.1 (0x00007f9f8f4bc000)
libxkbcommon.so.0 => /usr/lib/x86_64-linux-gnu/libxkbcommon.so.0 (0x00007f9f8f27d000)
libwayland-cursor.so.0 => /usr/lib/x86_64-linux-gnu/libwayland-cursor.so.0 (0x00007f9f8f075000)
libwayland-egl.so.1 => /usr/lib/x86_64-linux-gnu/libwayland-egl.so.1 (0x00007f9f8ee73000)
libwayland-client.so.0 => /usr/lib/x86_64-linux-gnu/libwayland-client.so.0 (0x00007f9f8ec64000)
libXext.so.6 => /usr/lib/x86_64-linux-gnu/libXext.so.6 (0x00007f9f8ea52000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f9f8e84a000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f9f8e646000)
libfreetype.so.6 => /usr/lib/x86_64-linux-gnu/libfreetype.so.6 (0x00007f9f8e392000)
libxcb.so.1 => /usr/lib/x86_64-linux-gnu/libxcb.so.1 (0x00007f9f8e16a000)
libpixman-1.so.0 => /usr/lib/x86_64-linux-gnu/libpixman-1.so.0 (0x00007f9f8dec5000)
libpng16.so.16 => /usr/lib/x86_64-linux-gnu/libpng16.so.16 (0x00007f9f8dc93000)
libxcb-shm.so.0 => /usr/lib/x86_64-linux-gnu/libxcb-shm.so.0 (0x00007f9f8da90000)
libxcb-render.so.0 => /usr/lib/x86_64-linux-gnu/libxcb-render.so.0 (0x00007f9f8d883000)
libXrender.so.1 => /usr/lib/x86_64-linux-gnu/libXrender.so.1 (0x00007f9f8d679000)
libdbus-1.so.3 => /lib/x86_64-linux-gnu/libdbus-1.so.3 (0x00007f9f8d42c000)
libatspi.so.0 => /usr/lib/x86_64-linux-gnu/libatspi.so.0 (0x00007f9f8d1fc000)
libharfbuzz.so.0 => /usr/lib/x86_64-linux-gnu/libharfbuzz.so.0 (0x00007f9f8cf5e000)
libthai.so.0 => /usr/lib/x86_64-linux-gnu/libthai.so.0 (0x00007f9f8cd55000)
libexpat.so.1 => /lib/x86_64-linux-gnu/libexpat.so.1 (0x00007f9f8cb23000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f9f8c8b1000)
libblkid.so.1 => /lib/x86_64-linux-gnu/libblkid.so.1 (0x00007f9f8c664000)
libXau.so.6 => /usr/lib/x86_64-linux-gnu/libXau.so.6 (0x00007f9f8c460000)
libXdmcp.so.6 => /usr/lib/x86_64-linux-gnu/libXdmcp.so.6 (0x00007f9f8c25a000)
libsystemd.so.0 => /lib/x86_64-linux-gnu/libsystemd.so.0 (0x00007f9f8bfd6000)
libgraphite2.so.3 => /usr/lib/x86_64-linux-gnu/libgraphite2.so.3 (0x00007f9f8bda9000)
libdatrie.so.1 => /usr/lib/x86_64-linux-gnu/libdatrie.so.1 (0x00007f9f8bba2000)
libuuid.so.1 => /lib/x86_64-linux-gnu/libuuid.so.1 (0x00007f9f8b99b000)
libbsd.so.0 => /lib/x86_64-linux-gnu/libbsd.so.0 (0x00007f9f8b786000)
liblzma.so.5 => /lib/x86_64-linux-gnu/liblzma.so.5 (0x00007f9f8b560000)
liblz4.so.1 => /usr/lib/x86_64-linux-gnu/liblz4.so.1 (0x00007f9f8b344000)
libgcrypt.so.20 => /lib/x86_64-linux-gnu/libgcrypt.so.20 (0x00007f9f8b029000)
libgpg-error.so.0 => /lib/x86_64-linux-gnu/libgpg-error.so.0 (0x00007f9f8ae14000)

用于 musl-gcc 生成的二进制文件的ldd命令日志:

linux-vdso.so.1 (0x00007ffeaf2ca000)
libgtk-3.so.0 => /usr/lib/x86_64-linux-gnu/libgtk-3.so.0 (0x00007f03c1a9f000)
libgdk-3.so.0 => /usr/lib/x86_64-linux-gnu/libgdk-3.so.0 (0x00007f03c17a9000)
libpangocairo-1.0.so.0 => /usr/lib/x86_64-linux-gnu/libpangocairo-1.0.so.0 (0x00007f03c159c000)
libpango-1.0.so.0 => /usr/lib/x86_64-linux-gnu/libpango-1.0.so.0 (0x00007f03c134f000)
libatk-1.0.so.0 => /usr/lib/x86_64-linux-gnu/libatk-1.0.so.0 (0x00007f03c1129000)
libcairo-gobject.so.2 => /usr/lib/x86_64-linux-gnu/libcairo-gobject.so.2 (0x00007f03c0f20000)
libcairo.so.2 => /usr/lib/x86_64-linux-gnu/libcairo.so.2 (0x00007f03c0c03000)
libgdk_pixbuf-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgdk_pixbuf-2.0.so.0 (0x00007f03c09df000)
libgio-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgio-2.0.so.0 (0x00007f03c0640000)
libgobject-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0 (0x00007f03c03ec000)
libglib-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007f03c00d6000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f03bfce5000)
libgmodule-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgmodule-2.0.so.0 (0x00007f03bfae1000)
libX11.so.6 => /usr/lib/x86_64-linux-gnu/libX11.so.6 (0x00007f03bf7a9000)
libXi.so.6 => /usr/lib/x86_64-linux-gnu/libXi.so.6 (0x00007f03bf599000)
libXfixes.so.3 => /usr/lib/x86_64-linux-gnu/libXfixes.so.3 (0x00007f03bf393000)
libatk-bridge-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libatk-bridge-2.0.so.0 (0x00007f03bf162000)
libepoxy.so.0 => /usr/lib/x86_64-linux-gnu/libepoxy.so.0 (0x00007f03bee61000)
libpangoft2-1.0.so.0 => /usr/lib/x86_64-linux-gnu/libpangoft2-1.0.so.0 (0x00007f03bec4b000)
libfontconfig.so.1 => /usr/lib/x86_64-linux-gnu/libfontconfig.so.1 (0x00007f03bea06000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f03be668000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f03be449000)
libXinerama.so.1 => /usr/lib/x86_64-linux-gnu/libXinerama.so.1 (0x00007f03be246000)
libXrandr.so.2 => /usr/lib/x86_64-linux-gnu/libXrandr.so.2 (0x00007f03be03b000)
libXcursor.so.1 => /usr/lib/x86_64-linux-gnu/libXcursor.so.1 (0x00007f03bde31000)
libXcomposite.so.1 => /usr/lib/x86_64-linux-gnu/libXcomposite.so.1 (0x00007f03bdc2e000)
libXdamage.so.1 => /usr/lib/x86_64-linux-gnu/libXdamage.so.1 (0x00007f03bda2b000)
libxkbcommon.so.0 => /usr/lib/x86_64-linux-gnu/libxkbcommon.so.0 (0x00007f03bd7ec000)
libwayland-cursor.so.0 => /usr/lib/x86_64-linux-gnu/libwayland-cursor.so.0 (0x00007f03bd5e4000)
libwayland-egl.so.1 => /usr/lib/x86_64-linux-gnu/libwayland-egl.so.1 (0x00007f03bd3e2000)
libwayland-client.so.0 => /usr/lib/x86_64-linux-gnu/libwayland-client.so.0 (0x00007f03bd1d3000)
libXext.so.6 => /usr/lib/x86_64-linux-gnu/libXext.so.6 (0x00007f03bcfc1000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f03bcdb9000)
libfreetype.so.6 => /usr/lib/x86_64-linux-gnu/libfreetype.so.6 (0x00007f03bcb05000)
libthai.so.0 => /usr/lib/x86_64-linux-gnu/libthai.so.0 (0x00007f03bc8fc000)
libpixman-1.so.0 => /usr/lib/x86_64-linux-gnu/libpixman-1.so.0 (0x00007f03bc657000)
libpng16.so.16 => /usr/lib/x86_64-linux-gnu/libpng16.so.16 (0x00007f03bc425000)
libxcb-shm.so.0 => /usr/lib/x86_64-linux-gnu/libxcb-shm.so.0 (0x00007f03bc222000)
libxcb.so.1 => /usr/lib/x86_64-linux-gnu/libxcb.so.1 (0x00007f03bbffa000)
libxcb-render.so.0 => /usr/lib/x86_64-linux-gnu/libxcb-render.so.0 (0x00007f03bbded000)
libXrender.so.1 => /usr/lib/x86_64-linux-gnu/libXrender.so.1 (0x00007f03bbbe3000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f03bb9c6000)
libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f03bb79e000)
libresolv.so.2 => /lib/x86_64-linux-gnu/libresolv.so.2 (0x00007f03bb583000)
libmount.so.1 => /lib/x86_64-linux-gnu/libmount.so.1 (0x00007f03bb32f000)
libffi.so.6 => /usr/lib/x86_64-linux-gnu/libffi.so.6 (0x00007f03bb127000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f03baeb5000)
/lib/ld-musl-x86_64.so.1 => /lib64/ld-linux-x86-64.so.2 (0x00007f03c25a9000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f03bacb1000)
libdbus-1.so.3 => /lib/x86_64-linux-gnu/libdbus-1.so.3 (0x00007f03baa64000)
libatspi.so.0 => /usr/lib/x86_64-linux-gnu/libatspi.so.0 (0x00007f03ba834000)
libharfbuzz.so.0 => /usr/lib/x86_64-linux-gnu/libharfbuzz.so.0 (0x00007f03ba596000)
libexpat.so.1 => /lib/x86_64-linux-gnu/libexpat.so.1 (0x00007f03ba364000)
libdatrie.so.1 => /usr/lib/x86_64-linux-gnu/libdatrie.so.1 (0x00007f03ba15d000)
libXau.so.6 => /usr/lib/x86_64-linux-gnu/libXau.so.6 (0x00007f03b9f59000)
libXdmcp.so.6 => /usr/lib/x86_64-linux-gnu/libXdmcp.so.6 (0x00007f03b9d53000)
libblkid.so.1 => /lib/x86_64-linux-gnu/libblkid.so.1 (0x00007f03b9b06000)
libsystemd.so.0 => /lib/x86_64-linux-gnu/libsystemd.so.0 (0x00007f03b9882000)
libgraphite2.so.3 => /usr/lib/x86_64-linux-gnu/libgraphite2.so.3 (0x00007f03b9655000)
libbsd.so.0 => /lib/x86_64-linux-gnu/libbsd.so.0 (0x00007f03b9440000)
libuuid.so.1 => /lib/x86_64-linux-gnu/libuuid.so.1 (0x00007f03b9239000)
liblzma.so.5 => /lib/x86_64-linux-gnu/liblzma.so.5 (0x00007f03b9013000)
liblz4.so.1 => /usr/lib/x86_64-linux-gnu/liblz4.so.1 (0x00007f03b8df7000)
libgcrypt.so.20 => /lib/x86_64-linux-gnu/libgcrypt.so.20 (0x00007f03b8adc000)
libgpg-error.so.0 => /lib/x86_64-linux-gnu/libgpg-error.so.0 (0x00007f03b88c7000)
4

1 回答 1

0

您不应该手动传递编译参数。这就是文档的编译 GTK+ 应用程序部分中的pkg-config建议和建议。

所以你应该这样做:

musl-gcc -g -Wall $(pkg-config --cflags gtk+-3.0) main.c -o main $(pkg-config --libs gtk+-3.0)

搜索../etc/ld-musl-$(ARCH).path Musl 手册


这也是常见问题解答的摘录:

ldd在哪里?

musl 的动态链接器带有内置的 ldd 功能。只需创建一个从 ld-musl-$ARCH.so 到 /bin/ldd 的符号链接。如果动态链接器以“ldd”启动,它将检测到并打印适当的 DSO 信息。

现在似乎 musl 有一个不同的 ldconfig 不使用您的标准路径,这可以解释您的问题。您可以使用-Lgcc 选项来解决该问题并指定在哪些目录中搜索库,但更好的方法是了解链接器和 musl 如何协同工作,以便编译器查看正确的位置。ldd没有用,它使用的是libc的链接器配置,而不是musl。

于 2019-01-23T12:07:20.697 回答