如何在MacOSX上编译Subsurface的静态二进制文件?

时间:2011-12-10 17:37:22

标签: c macos static-libraries autotools ld

我正在尝试在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和subsurface,我就明白了:

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。

2 个答案:

答案 0 :(得分: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

中的更多详细信息

答案 1 :(得分:0)

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

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

尝试将CoreFoundationlibgcc等作为静态库(即使它们存在)进行链接是没有意义的,因为它们将始终存在。