【发布时间】:2012-06-19 08:50:26
【问题描述】:
我创建了一堆.o 文件(通过gcc -c $file.c $someotherops -o $file.o)。现在我想将它们链接到一个静态库中。
我不确定我应该使用ld 还是gcc。在ld 手册中,据说我不应该直接使用它。但是,我无法弄清楚创建静态库的 gcc 参数。
我尝试了ld *.o -static -o libfoo.a,但它抱怨很多丢失的符号(我认为都是来自 libc)。我不明白为什么它会抱怨,因为它应该是一个静态库。我认为一旦我将该静态库链接到其他东西,它就会检查符号。
另一件事:我在这里使用/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/ld(我的目标是iOS)。它抱怨警告ld: warning: using ld_classic。这是关于什么的?
然后我想,也许它需要指定动态库。所以我添加了-lc 来链接libc。但它抱怨can't locate file for: -lc。我添加了-L/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk/usr/lib,还有一个libc.dylib。
有什么想法吗?
关于-lc 错误:在我指定-arch armv6 后它消失了。然后它抱怨一个错误的libcache.dylib(我猜它必须从libc.dylib链接,因为它没有指定它)。添加-L.../usr/lib/system 有帮助。
现在,对于每个 .o 文件,我都会收到警告 ld: warning: CPU_SUBTYPE_ARM_ALL subtype is deprecated。这是关于什么的?
我还有一堆缺失的符号,尤其是:
Undefined symbols for architecture armv6:
"start", referenced from:
-u command line option
(maybe you meant: _PyThread_start_new_thread)
"___udivsi3", referenced from:
_get_len_of_range in bltinmodule.o
_quorem in dtoa.o
_array_resize in arraymodule.o
_newarrayobject in arraymodule.o
_array_fromfile in arraymodule.o
_get_len_of_range in rangeobject.o
_inplace_divrem1 in longobject.o
...
"___unorddf2", referenced from:
_builtin_round in bltinmodule.o
...
我检查了其中一些符号,例如___udivsi3 在get_len_of_range。此函数仅使用 C 算术,没有外部调用。所以这似乎被翻译为使用一些外部函数,如___udivsi3。但这是在哪些库中?
-lgcc_s.1 修复了大部分 ___udivsi3 和相关缺失符号。 start 符号仍然丢失。 -u command line option 是什么意思?
从here,我感觉ld 可能不是正确的工具。在那里,使用了对ar 的简单调用。这似乎更有意义。我会检查这是否有效,然后将其转换为答案。
虽然玩得更多,ar 在构建胖静态库时会向我抛出一些警告。它给了我使用libtool 的提示。这就是我现在正在做的事情,即libtool -static -o libfoo.a *.o。我也将编译器切换到/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang,但不确定这是否重要。
现在,在编译一些链接到该静态库的测试应用程序时,我收到以下警告:
ld: warning: PIE disabled. Absolute addressing (perhaps -mdynamic-no-pic) not allowed in code signed PIE, but used in __PyBuiltin_Init from /Users/az/Programmierung/python-embedded/libpython.a(bltinmodule.o). To fix this warning, don't compile with -mdynamic-no-pic or link with -Wl,-no_pie
ld: warning: 32-bit absolute address out of range (0x1001B70C4 max is 4GB): from _usedpools + 0x00000004 (0x001B70CC) to 0x1001B70C4
ld: warning: 32-bit absolute address out of range (0x1001B70C4 max is 4GB): from _usedpools + 0x00000000 (0x001B70CC) to 0x1001B70C4
它们是关于什么的?我不使用-mdynamic-no-pic。在_PyBuiltin_Init 中我也没有真正看到我如何在那里使用绝对寻址。
另外,这些超出范围的绝对地址是关于什么的? 编辑:这些是一些非常大的分配。我现在刚刚删除了这段代码(如果有人对这些特定的 Python 内部结构感兴趣,这里是 WITH_PYMALLOC)。
当我在 iPhone 上启动它时,我会中止:
dyld: vm_protect(0x00001000, 0x00173000, false, 0x07) failed, result=2 for segment __TEXT in /var/mobile/Applications/C15D9525-E7DC-4463-B05B-D39C9CA24319/...
当我使用-no_pie 进行链接时,它甚至没有链接。它失败了:
Illegal text-relocation to ___stderrp in /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.1.sdk/usr/lib/libSystem.dylib from _read_object in /Users/az/Programmierung/python-embedded/libpython.a(marshal.o) for architecture armv7
我解决了 PIE 禁用,绝对寻址错误。我的命令行 Clang 中有一个 -static。一旦我删除它,警告就消失了,还有 dyld/vm_protect 错误。这是它第一次实际运行一些代码。
直到我点击another strange error about integer comparison。这看起来更像是他们的 Clang 版本中的一个错误。
【问题讨论】:
-
一个问题;为什么不创建 XCoe 静态库项目并使用该项目构建静态库?
-
@meccan:出于各种原因,我想在这种情况下拥有自己的编译脚本(主要是因为对于这个特定的项目,现在这要简单得多)。无论如何,即使我会用 Xcode 来做,这个问题仍然有效/开放,因为我想了解如何手动完成。
-
好的,你知道你可以从 shell 构建一个 Xcode 项目吗?
-
是的,当然。但是在这里拥有一个 Xcode 项目(并更新/维护它)几乎是不可能的,或者至少是非常烦人的。另外,我真的不明白首先拥有它的原因。就是这个项目:python-embedded。无论如何,我想知道这一点,以备将来使用,只是为了更好地了解它是如何工作的。
-
mark.aufflick.com/blog/2010/11/18/…你检查过这个网站吗?
标签: ios gcc build static-libraries ld