【问题标题】:How do I compile a static binary of Subsurface on MacOSX?如何在 MacOSX 上编译 Subsurface 的静态二进制文件?
【发布时间】:2012-01-17 12:18:42
【问题描述】:

我正在尝试在 MacOSX 上的 Subsurface 上制作静态二进制文件,以便我们可以提供简单的二进制文件下载。我对解决我遇到的问题一无所知,但这可能只是因为我的 C 很烂。

这就是我所做的。我已经从MacPorts 安装了 gconf 和 gtk2,为了简单起见,我正在构建没有 libusb:

git clone git://libdivecomputer.git.sourceforge.net/gitroot/libdivecomputer/libdivecomputer
cd libdivecomputer
autoreconf --install
./configure
make LDFLAGS="-static"
sudo make install
cd ..
git clone git://subsurface.hohndel.org/subsurface.git
cd subsurface
make LDFLAGS="-framework CoreFoundation -static"

结果是这样的:

gcc -framework CoreFoundation -static -o subsurface main.o dive.o profile.o info.o equipment.o divelist.o parse-xml.o save-xml.o libdivecomputer.o print.o uemis.o gtk-gui.o statistics.o macos.o  -L/opt/local/lib -lxml2 -lz -lpthread -liconv -lm -L/opt/local/lib -lxslt -lxml2 -lz -lpthread -liconv -lm -L/opt/local/lib -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lpangocairo-1.0 -lXext -lXrender -lXinerama -lXi -lXrandr -lXcursor -lXcomposite -lXdamage -lgdk_pixbuf-2.0 -lpangoft2-1.0 -lgio-2.0 -lXfixes -lcairo -lX11 -lpng14 -lpango-1.0 -lm -lfreetype -lfontconfig -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lglib-2.0 -lintl    /usr/local/lib/libdivecomputer.a  -lpthread
/usr/bin/ld_classic: can't locate file for: -lcrt0.o
collect2: ld returned 1 exit status
make: *** [subsurface] Error 1

现在,如果我将 /usr/lib/crt0.o 符号链接到 /usr/lib/crt1.o,我会得到这个:

gcc -framework CoreFoundation -static -o subsurface main.o dive.o profile.o info.o equipment.o divelist.o parse-xml.o save-xml.o libdivecomputer.o print.o uemis.o gtk-gui.o statistics.o macos.o  -L/opt/local/lib -lxml2 -lz -lpthread -liconv -lm -L/opt/local/lib -lxslt -lxml2 -lz -lpthread -liconv -lm -L/opt/local/lib -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lpangocairo-1.0 -lXext -lXrender -lXinerama -lXi -lXrandr -lXcursor -lXcomposite -lXdamage -lgdk_pixbuf-2.0 -lpangoft2-1.0 -lgio-2.0 -lXfixes -lcairo -lX11 -lpng14 -lpango-1.0 -lm -lfreetype -lfontconfig -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lglib-2.0 -lintl    /usr/local/lib/libdivecomputer.a  -lpthread
ld: warning: ignoring file /usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1/libgcc_static.a, file was built for archive which is not the architecture being linked (x86_64)
ld: warning: unexpected dylib (/System/Library/Frameworks//CoreFoundation.framework/CoreFoundation) on link line
ld: warning: unexpected dylib (/opt/local/lib/libxml2.dylib) on link line
ld: warning: unexpected dylib (/opt/local/lib/libz.dylib) on link line
ld: warning: unexpected dylib (/usr/lib/libpthread.dylib) on link line
ld: warning: unexpected dylib (/opt/local/lib/libiconv.dylib) on link line
ld: warning: unexpected dylib (/usr/lib/libm.dylib) on link line
ld: warning: unexpected dylib (/opt/local/lib/libxslt.dylib) on link line
ld: warning: unexpected dylib (/opt/local/lib/libgtk-x11-2.0.dylib) on link line
ld: warning: unexpected dylib (/opt/local/lib/libgdk-x11-2.0.dylib) on link line
ld: warning: unexpected dylib (/opt/local/lib/libatk-1.0.dylib) on link line
ld: warning: unexpected dylib (/opt/local/lib/libpangocairo-1.0.dylib) on link line
ld: warning: unexpected dylib (/opt/local/lib/libXext.dylib) on link line
ld: warning: unexpected dylib (/opt/local/lib/libXrender.dylib) on link line
ld: warning: unexpected dylib (/opt/local/lib/libXinerama.dylib) on link line
ld: warning: unexpected dylib (/opt/local/lib/libXi.dylib) on link line
ld: warning: unexpected dylib (/opt/local/lib/libXrandr.dylib) on link line
ld: warning: unexpected dylib (/opt/local/lib/libXcursor.dylib) on link line
ld: warning: unexpected dylib (/opt/local/lib/libXcomposite.dylib) on link line
ld: warning: unexpected dylib (/opt/local/lib/libXdamage.dylib) on link line
ld: warning: unexpected dylib (/opt/local/lib/libgdk_pixbuf-2.0.dylib) on link line
ld: warning: unexpected dylib (/opt/local/lib/libpangoft2-1.0.dylib) on link line
ld: warning: unexpected dylib (/opt/local/lib/libgio-2.0.dylib) on link line
ld: warning: unexpected dylib (/opt/local/lib/libXfixes.dylib) on link line
ld: warning: unexpected dylib (/opt/local/lib/libcairo.dylib) on link line
ld: warning: unexpected dylib (/opt/local/lib/libX11.dylib) on link line
ld: warning: unexpected dylib (/opt/local/lib/libpng14.dylib) on link line
ld: warning: unexpected dylib (/opt/local/lib/libpango-1.0.dylib) on link line
ld: warning: unexpected dylib (/opt/local/lib/libfreetype.dylib) on link line
ld: warning: unexpected dylib (/opt/local/lib/libfontconfig.dylib) on link line
ld: warning: unexpected dylib (/opt/local/lib/libgobject-2.0.dylib) on link line
ld: warning: unexpected dylib (/opt/local/lib/libgmodule-2.0.dylib) on link line
ld: warning: unexpected dylib (/opt/local/lib/libgthread-2.0.dylib) on link line
ld: warning: unexpected dylib (/opt/local/lib/libglib-2.0.dylib) on link line
ld: warning: unexpected dylib (/opt/local/lib/libintl.dylib) on link line
Assertion failed: (_pointerLocations.size() == 0), function size, file /SourceCache/ld64/ld64-123.2.1/src/ld/LinkEditClassic.hpp, line 961.
0  0x10226171c  __assert_rtn + 76
1  0x1023233f5  ld::tool::ExternalRelocationsAtom<x86_64>::size() const + 149
2  0x1022d8a17  ld::tool::OutputFile::updateLINKEDITAddresses(ld::Internal&) + 583
3  0x1022d1aae  ld::tool::OutputFile::write(ld::Internal&) + 142
4  0x102261caa  main + 1178
5  0x1022502b4  start + 52
collect2: ld returned 1 exit status
make: *** [subsurface] Error 1

如果我改为使用“-arch i686”构建 libdivecomputer 和地下,我会得到:

gcc -framework CoreFoundation -arch i686 -static -o subsurface main.o dive.o profile.o info.o equipment.o divelist.o parse-xml.o save-xml.o libdivecomputer.o print.o uemis.o gtk-gui.o statistics.o macos.o  -L/opt/local/lib -lxml2 -lz -lpthread -liconv -lm -L/opt/local/lib -lxslt -lxml2 -lz -lpthread -liconv -lm -L/opt/local/lib -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lpangocairo-1.0 -lXext -lXrender -lXinerama -lXi -lXrandr -lXcursor -lXcomposite -lXdamage -lgdk_pixbuf-2.0 -lpangoft2-1.0 -lgio-2.0 -lXfixes -lcairo -lX11 -lpng14 -lpango-1.0 -lm -lfreetype -lfontconfig -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lglib-2.0 -lintl    /usr/local/lib/libdivecomputer.a  -lpthread
/usr/bin/ld_classic: /usr/lib/crt0.o incompatible, file contains unsupported type of section 2 (__IMPORT,__pointers) in load command 0 (must specify "-dynamic" to be used)
/usr/bin/ld_classic: incompatible flag -framework used (must specify "-dynamic" to be used)
collect2: ld returned 1 exit status
make: *** [subsurface] Error 1

我在 MacOSX Lion 上使用 XCode 4.2.1。

【问题讨论】:

    标签: c macos static-libraries autotools ld


    【解决方案1】:

    除非 gtk 库具有 .la (libtool) 元文件和 .a (静态) 构建,否则我不知道这是否可以轻松完成 - 或者至少是自动完成。至少添加:--disable-shared./configure

    可以尝试:env LDFLAGS="-static-libtool-libs" ./configure --disable-shared,而不是在 make 调用中设置它。

    尝试将CoreFoundationlibgcc 等链接为静态库是没有意义的,即使它们存在,因为它们会一直存在。

    【讨论】:

    • 感谢您尝试解决这个问题!我添加了 CoreFoundation,因为我需要它在这台机器上构建 Subsurface。在我几乎相同的工作机器上,无论出于何种原因,我都不需要它。我尝试按照您所说的方式配置 libdivecomputer,但没有帮助。地下没有配置脚本。为什么它会寻找不存在的 /usr/lib/crt0.o?这里的问题太多了:)
    • 我想知道你是否找到了这个问题的原因?谢谢
    • @user454083 不,但我找到了解决方案。请参阅已接受的答案。 :)
    【解决方案2】:

    解决方案不是静态构建这个东西,而是将库包含在 .app 包中。二进制文件和库都需要使用 install_name_tool 更新以使用相对路径:

    $ otool -L subsurface
    subsurface:
        /opt/local/lib/libxml2.2.dylib (compatibility version 10.0.0, current version 10.8.0)
        /opt/local/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.7)
        ...
    $ install_name_tool -change /opt/local/lib/libxml2.2.dylib \
      @executable_path/../Resources/lib/libxml2.2.dylib subsurface
    $ otool -L subsurface
    subsurface:
        @executable_path/../lib/libxml2.2.dylib (compatibility version 10.0.0, current version 10.8.0)
        /opt/local/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.7)
        ...
    

    gtk-mac-bundler 可用于自动执行此过程。更多详情请关注Subsurface macosx packaging directory

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-15
      相关资源
      最近更新 更多