【问题标题】:Including external C library with Xcode使用 Xcode 包含外部 C 库
【发布时间】:2011-09-30 04:00:34
【问题描述】:

我有一个内置的 C 静态库(Antlr 3 C 库)。它已正确安装并且可以正常工作(即,我可以运行 gcc -o parser lexer.c parser.c -lantlr3c 就好了)。

然而,在 Xcode 中,我得到一个错误。我在“其他链接器标志”构建设置中添加了-lantlr3c

ld: library not found for -lantlr3c
Command /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/llvm-gcc-4.2 failed with exit code 1

我在这里找到的其他几个问题(1234)通常都有针对 Xcode 3 的答案。我在 iOS 静态库项目中使用 Xcode 4.1。我目前正在构建单元测试的东西。


编辑:应@LaceySnr 的要求,详细 gcc 和 Xcode 错误的输出。

gcc 的输出:

$ gcc -v -o parser.o BFMathParser.c BFMathLexer.c -lantlr3c -arch i386
Using built-in specs.
Target: i686-apple-darwin11
Configured with: /private/var/tmp/llvmgcc42/llvmgcc42-2335.15~25/src/configure --disable-checking --enable-werror --prefix=/Developer/usr/llvm-gcc-4.2 --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-prefix=llvm- --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin11 --enable-llvm=/private/var/tmp/llvmgcc42/llvmgcc42-2335.15~25/dst-llvmCore/Developer/usr/local --program-prefix=i686-apple-darwin11- --host=x86_64-apple-darwin11 --target=i686-apple-darwin11 --with-gxx-include-dir=/usr/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)
 /usr/llvm-gcc-4.2/bin/../libexec/gcc/i686-apple-darwin11/4.2.1/cc1 -quiet -v -iprefix /usr/llvm-gcc-4.2/bin/../lib/gcc/i686-apple-darwin11/4.2.1/ -D__DYNAMIC__ BFMathParser.c -fPIC -quiet -dumpbase BFMathParser.c -mmacosx-version-min=10.7.0 -mtune=core2 -auxbase BFMathParser -version -o /var/folders/5v/9rvx60c17vl7kc3b_r8gz0wc0000gn/T//ccistonB.s
ignoring nonexistent directory "/usr/llvm-gcc-4.2/bin/../lib/gcc/i686-apple-darwin11/4.2.1/../../../../i686-apple-darwin11/include"
ignoring nonexistent directory "/Developer/usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1/../../../../i686-apple-darwin11/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/llvm-gcc-4.2/bin/../lib/gcc/i686-apple-darwin11/4.2.1/include
 /usr/local/include
 /Developer/usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
GNU C version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00) (i686-apple-darwin11)
    compiled by GNU C version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00).
GGC heuristics: --param ggc-min-expand=150 --param ggc-min-heapsize=131072
Compiler executable checksum: edef36ecca2ce3f93d0f78610b5af911
 /usr/llvm-gcc-4.2/bin/../libexec/gcc/i686-apple-darwin11/4.2.1/as -arch i386 -force_cpusubtype_ALL -o /var/folders/5v/9rvx60c17vl7kc3b_r8gz0wc0000gn/T//ccqP9bOO.o /var/folders/5v/9rvx60c17vl7kc3b_r8gz0wc0000gn/T//ccistonB.s
 /usr/llvm-gcc-4.2/bin/../libexec/gcc/i686-apple-darwin11/4.2.1/cc1 -quiet -v -iprefix /usr/llvm-gcc-4.2/bin/../lib/gcc/i686-apple-darwin11/4.2.1/ -D__DYNAMIC__ BFMathLexer.c -fPIC -quiet -dumpbase BFMathLexer.c -mmacosx-version-min=10.7.0 -mtune=core2 -auxbase BFMathLexer -version -o /var/folders/5v/9rvx60c17vl7kc3b_r8gz0wc0000gn/T//ccistonB.s
ignoring nonexistent directory "/usr/llvm-gcc-4.2/bin/../lib/gcc/i686-apple-darwin11/4.2.1/../../../../i686-apple-darwin11/include"
ignoring nonexistent directory "/Developer/usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1/../../../../i686-apple-darwin11/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/llvm-gcc-4.2/bin/../lib/gcc/i686-apple-darwin11/4.2.1/include
 /usr/local/include
 /Developer/usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
GNU C version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00) (i686-apple-darwin11)
    compiled by GNU C version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00).
GGC heuristics: --param ggc-min-expand=150 --param ggc-min-heapsize=131072
Compiler executable checksum: edef36ecca2ce3f93d0f78610b5af911
 /usr/llvm-gcc-4.2/bin/../libexec/gcc/i686-apple-darwin11/4.2.1/as -arch i386 -force_cpusubtype_ALL -o /var/folders/5v/9rvx60c17vl7kc3b_r8gz0wc0000gn/T//ccepo2q0.o /var/folders/5v/9rvx60c17vl7kc3b_r8gz0wc0000gn/T//ccistonB.s
 /usr/llvm-gcc-4.2/bin/../libexec/gcc/i686-apple-darwin11/4.2.1/collect2 -dynamic -arch i386 -macosx_version_min 10.7.0 -weak_reference_mismatches non-weak -o parser.o -lcrt1.10.6.o -L/usr/lib/i686-apple-darwin11/4.2.1 -L/usr/llvm-gcc-4.2/bin/../lib/gcc/i686-apple-darwin11/4.2.1 -L/usr/llvm-gcc-4.2/bin/../lib/gcc -L/Developer/usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1 -L/usr/lib/gcc/i686-apple-darwin11/4.2.1 -L/usr/llvm-gcc-4.2/bin/../lib/gcc/i686-apple-darwin11/4.2.1/../../.. -L/Developer/usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1/../../.. /var/folders/5v/9rvx60c17vl7kc3b_r8gz0wc0000gn/T//ccqP9bOO.o /var/folders/5v/9rvx60c17vl7kc3b_r8gz0wc0000gn/T//ccepo2q0.o -lantlr3c -lSystem -lgcc -lSystem

Xcode 的输出:

Libtool /Users/bf/Library/Developer/Xcode/DerivedData/Calculator-dggaoccanosfnrampxhlfrbuuyoi/Build/Products/Debug-iphonesimulator/libcalclib.a normal i386
    cd /Users/bf/Desktop/calclib
    setenv MACOSX_DEPLOYMENT_TARGET 10.6
    setenv PATH "/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
    /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/libtool -static -arch_only i386 -syslibroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk -L/Users/bf/Library/Developer/Xcode/DerivedData/Calculator-dggaoccanosfnrampxhlfrbuuyoi/Build/Products/Debug-iphonesimulator -filelist /Users/bf/Library/Developer/Xcode/DerivedData/Calculator-dggaoccanosfnrampxhlfrbuuyoi/Build/Intermediates/calclib.build/Debug-iphonesimulator/calclib.build/Objects-normal/i386/calclib.LinkFileList -lantlr3c -ObjC -framework Foundation -o /Users/bf/Library/Developer/Xcode/DerivedData/Calculator-dggaoccanosfnrampxhlfrbuuyoi/Build/Products/Debug-iphonesimulator/libcalclib.a

Command /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/libtool failed with exit code 1

【问题讨论】:

    标签: c ios xcode static-libraries antlr3


    【解决方案1】:

    在导航器中单击您的项目,选择适当的目标,然后展开 Linked Frameworks and Libraries 部分。单击加号,单击添加其他...,然后浏览到您的静态对象。

    您还必须删除 -lantlr3c 标志。

    【讨论】:

    • 我选择了测试目标,但找不到“链接框架和库”部分。我确实在“Build Phases”选项卡中找到了“Link Binary with Libraries”部分,但在那里添加 libantlr3c.a 库不起作用。 (我也取出了 -lantlr3c 标志,但无济于事。)
    • 我很确定我为 i386 编译了它,当我在模拟器中测试时,它现在应该足够好了。我将如何检查图书馆的架构?
    • 你可以使用file命令:file /path/to/your/file,它会列出你的库支持的架构。
    • 是i386,好吧。如果它是正确的拱门并且位于正确的位置,为什么我会收到“找不到库”错误?
    • @CajunLuke,具体的错误是什么?除了这个之外,不应该有对库的任何直接引用,因此如果它没有链接,则库中的每个函数都会出现“未找到符号”错误,而不是“未找到库”。跨度>
    【解决方案2】:

    您可以在其他链接器标志中使用它,但您还需要确保该路径也在库搜索路径中。

    还要确保您使用正确的配置(调试/发布/其他)进行构建——您可以为每个库指定不同的链接,因此如果您已将其设置为调试但您正在构建发布它显然不会链接。

    【讨论】:

    • 如何查找/更改库搜索路径?我相当确定我的目标是正确的 - 测试目标,每次调试模式。
    • 我没有注意到这是针对 xcode 4 的——尝试在您的目标/项目设置中使用框架搜索路径。
    • 我很确定它可以找到库 - 如果我删除库或 -lantlr3c 标志,我不会收到任何“未找到符号”错误。
    • 这很奇怪。我能建议的最好的方法是将详细的 gcc 输出与 Xcode 的输出进行比较,看看可能会发生什么不同。
    • 我认为米格尔一针见血,见下文。当我在模拟器和设备之间切换进行测试时忘记构建我的库的设备版本时,我总是遇到问题。
    【解决方案3】:

    当您在 Mac 上使用 gcc 测试此库时,您使用的是 x86 版本。 iOS 链接器没有找到它,因为您可能没有构建该库的 arm 版本。

    【讨论】:

    • 我不确定如何为 ARM 构建库,我正在为 i386 构建的 iOS 模拟器中进行测试。
    • 我认为你可以像 x86 一样构建 ARM 库,但在 gcc 命令行中添加 -arch armv6 或 -arch armv7。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-22
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 2015-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多