“dyld:库未加载”错误

时间:2014-06-04 19:09:16

标签: ios xcode ios-simulator clang

我已成功编译静态库并将其添加到XCode项目中,就像它在documentation中编写的那样:

  1. libclang.a文件位于项目目录中(添加了文件副本和组)
  2. libclang.a列在"框架"
  3. 我已添加' -Objc'链接器标记为'其他链接器标记'。
  4. libclang.a列在"复制捆绑资源"
  5. libclang.a是为ios模拟器编译的:

    MBA-Anton:llvm_34_build_i386 asmirnov$ lipo -info ../llvm_34_ios_i386_installed/lib/libclang.a
    input file ../llvm_34_ios_i386_installed/lib/libclang.a is not a fat file
    Non-fat file: ../llvm_34_ios_i386_installed/lib/libclang.a is architecture: i386
    

    我能够构建项目但是我在ios模拟器启动应用程序时遇到错误:

    dyld: Library not loaded: @rpath/libclang.dylib
      Referenced from: /Users/asmirnov/Library/Application Support/iPhone Simulator/7.1/Applications/D87A3FA9-7207-40B4-9EA0-5F06CEF6EF7B/StaticLibraryUsage.app/StaticLibraryUsage
      Reason: image not found
    

    我发现了类似的问题,但似乎我做了推荐的所有事情。有什么想法吗?

    PS。我有libclang.dylib文件,但我不确定它是否真的需要,因为我在项目中添加了libclang.a文件。

    PPS。我编译了armv7版本的libclang.a并使用lipo创建了包含i386和armv7的胖库。删除i386版本的libclang.a并添加胖版本的libclang.a后,我有multiple unresolved references for i386 arch build errors.

    PPPS。我已经解决了#34;未解决的参考文献"通过在项目中添加所有libLLVM * .a文件来导致错误。但现在我有了原创的" dyld:图书馆未加载"。

    PPPPS:.m编译命令行:

    CompileC /Users/asmirnov/Library/Developer/Xcode/DerivedData/TestProject-axtxqjlvmrcxqsfnvgobybceosvb/Build/Intermediates/StaticLibraryUsage2.build/Debug-iphonesimulator/StaticLibraryUsage2.build/Objects-normal/i386/ASAppDelegate.o StaticLibraryUsage2/ASAppDelegate.m normal i386 objective-c com.apple.compilers.llvm.clang.1_0.compiler
        cd /Users/asmirnov/Documents/dev/src/iOS_Projects/StaticLibraryUsage2
        export LANG=en_US.US-ASCII
        export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
        /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x objective-c -arch i386 -fmessage-length=0 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit=0 -std=gnu99 -fobjc-arc -fmodules -fmodules-cache-path=/Users/asmirnov/Library/Developer/Xcode/DerivedData/ModuleCache -Wno-trigraphs -fpascal-strings -O0 -Wno-missing-field-initializers -Wno-missing-prototypes -Werror=return-type -Wno-implicit-atomic-properties -Werror=deprecated-objc-isa-usage -Werror=objc-root-class -Wno-receiver-is-weak -Wno-arc-repeated-use-of-weak -Wduplicate-method-match -Wno-missing-braces -Wparentheses -Wswitch -Wunused-function -Wno-unused-label -Wno-unused-parameter -Wunused-variable -Wunused-value -Wempty-body -Wconditional-uninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wconstant-conversion -Wint-conversion -Wbool-conversion -Wenum-conversion -Wshorten-64-to-32 -Wpointer-sign -Wno-newline-eof -Wno-selector -Wno-strict-selector-match -Wundeclared-selector -Wno-deprecated-implementations -DDEBUG=1 -DDEBUG=1 -D__STDC_LIMIT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_CONSTANT_MACROS -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.1.sdk -fexceptions -fasm-blocks -fstrict-aliasing -Wprotocol -Wdeprecated-declarations -g -Wno-sign-conversion -fobjc-abi-version=2 -fobjc-legacy-dispatch -mios-simulator-version-min=7.1 -iquote /Users/asmirnov/Library/Developer/Xcode/DerivedData/TestProject-axtxqjlvmrcxqsfnvgobybceosvb/Build/Intermediates/StaticLibraryUsage2.build/Debug-iphonesimulator/StaticLibraryUsage2.build/StaticLibraryUsage2-generated-files.hmap -I/Users/asmirnov/Library/Developer/Xcode/DerivedData/TestProject-axtxqjlvmrcxqsfnvgobybceosvb/Build/Intermediates/StaticLibraryUsage2.build/Debug-iphonesimulator/StaticLibraryUsage2.build/StaticLibraryUsage2-own-target-headers.hmap -I/Users/asmirnov/Library/Developer/Xcode/DerivedData/TestProject-axtxqjlvmrcxqsfnvgobybceosvb/Build/Intermediates/StaticLibraryUsage2.build/Debug-iphonesimulator/StaticLibraryUsage2.build/StaticLibraryUsage2-all-target-headers.hmap -iquote /Users/asmirnov/Library/Developer/Xcode/DerivedData/TestProject-axtxqjlvmrcxqsfnvgobybceosvb/Build/Intermediates/StaticLibraryUsage2.build/Debug-iphonesimulator/StaticLibraryUsage2.build/StaticLibraryUsage2-project-headers.hmap -I/Users/asmirnov/Library/Developer/Xcode/DerivedData/TestProject-axtxqjlvmrcxqsfnvgobybceosvb/Build/Products/Debug-iphonesimulator/include -I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -I/Users/asmirnov/Library/Developer/Xcode/DerivedData/TestProject-axtxqjlvmrcxqsfnvgobybceosvb/Build/Intermediates/StaticLibraryUsage2.build/Debug-iphonesimulator/StaticLibraryUsage2.build/DerivedSources/i386 -I/Users/asmirnov/Library/Developer/Xcode/DerivedData/TestProject-axtxqjlvmrcxqsfnvgobybceosvb/Build/Intermediates/StaticLibraryUsage2.build/Debug-iphonesimulator/StaticLibraryUsage2.build/DerivedSources -F/Users/asmirnov/Library/Developer/Xcode/DerivedData/TestProject-axtxqjlvmrcxqsfnvgobybceosvb/Build/Products/Debug-iphonesimulator -include /Users/asmirnov/Library/Developer/Xcode/DerivedData/TestProject-axtxqjlvmrcxqsfnvgobybceosvb/Build/Intermediates/PrecompiledHeaders/StaticLibraryUsage2-Prefix-baynabgphydhcmapwpcclpqhagrv/StaticLibraryUsage2-Prefix.pch -MMD -MT dependencies -MF /Users/asmirnov/Library/Developer/Xcode/DerivedData/TestProject-axtxqjlvmrcxqsfnvgobybceosvb/Build/Intermediates/StaticLibraryUsage2.build/Debug-iphonesimulator/StaticLibraryUsage2.build/Objects-normal/i386/ASAppDelegate.d --serialize-diagnostics /Users/asmirnov/Library/Developer/Xcode/DerivedData/TestProject-axtxqjlvmrcxqsfnvgobybceosvb/Build/Intermediates/StaticLibraryUsage2.build/Debug-iphonesimulator/StaticLibraryUsage2.build/Objects-normal/i386/ASAppDelegate.dia -c /Users/asmirnov/Documents/dev/src/iOS_Projects/StaticLibraryUsage2/StaticLibraryUsage2/ASAppDelegate.m -o /Users/asmirnov/Library/Developer/Xcode/DerivedData/TestProject-axtxqjlvmrcxqsfnvgobybceosvb/Build/Intermediates/StaticLibraryUsage2.build/Debug-iphonesimulator/StaticLibraryUsage2.build/Objects-normal/i386/ASAppDelegate.o
    

    link命令行:

    Ld /Users/asmirnov/Library/Developer/Xcode/DerivedData/TestProject-axtxqjlvmrcxqsfnvgobybceosvb/Build/Products/Debug-iphonesimulator/StaticLibraryUsage2.app/StaticLibraryUsage2 normal i386
        cd /Users/asmirnov/Documents/dev/src/iOS_Projects/StaticLibraryUsage2
        export IPHONEOS_DEPLOYMENT_TARGET=7.1
        export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
        /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch i386 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.1.sdk -L/Users/asmirnov/Library/Developer/Xcode/DerivedData/TestProject-axtxqjlvmrcxqsfnvgobybceosvb/Build/Products/Debug-iphonesimulator -L/Users/asmirnov/Documents/dev/src/llvm_34_ios_fat_installed -L/Users/asmirnov/Documents/dev/src/iOS_Projects/StaticLibraryUsage2/StaticLibraryUsage2 -L/Users/asmirnov/Documents/dev/src/llvm_34_ios_i386_installed_2/lib -F/Users/asmirnov/Library/Developer/Xcode/DerivedData/TestProject-axtxqjlvmrcxqsfnvgobybceosvb/Build/Products/Debug-iphonesimulator -filelist /Users/asmirnov/Library/Developer/Xcode/DerivedData/TestProject-axtxqjlvmrcxqsfnvgobybceosvb/Build/Intermediates/StaticLibraryUsage2.build/Debug-iphonesimulator/StaticLibraryUsage2.build/Objects-normal/i386/StaticLibraryUsage2.LinkFileList -Xlinker -objc_abi_version -Xlinker 2 -ObjC -lstdc++ -fobjc-arc -fobjc-link-runtime -Xlinker -no_implicit_dylibs -mios-simulator-version-min=7.1 -lLLVMRuntimeDyld -lLLVMSystemZAsmParser -lLLVMAArch64CodeGen -lLLVMTarget -lLLVMMCJIT -lLLVMX86Disassembler -lLLVMExecutionEngine -lLLVMPowerPCInfo -lLLVMHexagonAsmPrinter -lLLVMX86Utils -lLLVMPowerPCAsmParser -lclangDriver -lLLVMARMInfo -lLLVMAArch64AsmParser -lLLVMHexagonDesc -lLLVMipa -lclangARCMigrate -lLLVMMipsDesc -lLLVMNVPTXCodeGen -lLLVMBitWriter -lLLVMXCoreDisassembler -lLLVMNVPTXDesc -lLLVMX86CodeGen -lLLVMHexagonInfo -lLLVMSystemZAsmPrinter -lLLVMTableGen -lLLVMSystemZCodeGen -lLLVMInstrumentation -lLLVMAArch64Disassembler -lLLVMX86AsmPrinter -lLLVMCppBackendInfo -lclangBasic -lLLVMCodeGen -lLLVMDebugInfo -lLLVMAArch64Utils -lLLVMNVPTXAsmPrinter -framework CoreGraphics -lLLVMAsmParser -lLLVMSparcCodeGen -lclangFormat -lLLVMXCoreDesc -lLLVMipo -lLLVMObject -lclang -lLLVMX86Desc -lLLVMPowerPCDesc -lLLVMXCoreCodeGen -lLLVMVectorize -lclangAST -lclangSema -lclangTooling -lLLVMIRReader -lLLVMMipsCodeGen -lclangRewriteCore -lLLVMXCoreAsmPrinter -lLLVMMSP430Desc -lLLVMInstCombine -lLLVMAArch64AsmPrinter -lLLVMMCParser -lLLVMR600Info -lLLVMMipsAsmPrinter -lLLVMMipsAsmParser -lLLVMMCDisassembler -lLLVMBitReader -lclangAnalysis -lclangStaticAnalyzerCore -lLLVMMipsDisassembler -lLLVMR600AsmPrinter -lLLVMR600CodeGen -lLLVMAArch64Info -lclangDynamicASTMatchers -lLLVMARMAsmPrinter -lLLVMXCoreInfo -lclangStaticAnalyzerFrontend -lLLVMMipsInfo -lLLVMSelectionDAG -lLLVMTransformUtils -lLLVMSparcInfo -lLLVMR600Desc -lLLVMOption -lLLVMAArch64Desc -lLLVMObjCARCOpts -lLLVMJIT -lclangEdit -lLLVMSystemZDesc -lLLVMARMDesc -lLLVMSparcDesc -lLLVMMC -lLLVMMSP430CodeGen -lLTO -lLLVMHexagonCodeGen -lLLVMMSP430AsmPrinter -framework UIKit -lLLVMAnalysis -lLLVMMSP430Info -lLLVMARMCodeGen -lLLVMARMAsmParser -lclangCodeGen -lLLVMLinker -lLLVMPowerPCCodeGen -lLLVMAsmPrinter -lLLVMX86AsmParser -lLLVMSystemZInfo -framework Foundation -lclangASTMatchers -lLLVMScalarOpts -lLLVMPowerPCAsmPrinter -lLLVMARMDisassembler -lLLVMNVPTXInfo -lclangParse -lclangIndex -lclangStaticAnalyzerCheckers -lclangRewriteFrontend -lLLVMInterpreter -lclangLex -lLLVMCppBackendCodeGen -lLLVMX86Info -lLLVMCore -lLLVMLTO -lclangFrontend -lclangFrontendTool -lLLVMSupport -lLLVMSystemZDisassembler -lclangSerialization -Xlinker -dependency_info -Xlinker /Users/asmirnov/Library/Developer/Xcode/DerivedData/TestProject-axtxqjlvmrcxqsfnvgobybceosvb/Build/Intermediates/StaticLibraryUsage2.build/Debug-iphonesimulator/StaticLibraryUsage2.build/Objects-normal/i386/StaticLibraryUsage2_dependency_info.dat -o /Users/asmirnov/Library/Developer/Xcode/DerivedData/TestProject-axtxqjlvmrcxqsfnvgobybceosvb/Build/Products/Debug-iphonesimulator/StaticLibraryUsage2.app/StaticLibraryUsage2
    

    libclang.a添加到可执行命令行:

    CpResource /Users/asmirnov/Documents/dev/src/llvm_34_ios_i386_installed_2/lib/libclang.a /Users/asmirnov/Library/Developer/Xcode/DerivedData/PocketIDE-axtxqjlvmrcxqsfnvgobybceosvb/Build/Products/Debug-iphonesimulator/StaticLibraryUsage2.app/libclang.a
        cd /Users/asmirnov/Documents/dev/src/iOS_Projects/StaticLibraryUsage2
        export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
        builtin-copy -exclude .DS_Store -exclude CVS -exclude .svn -exclude .git -exclude .hg -resolve-src-symlinks /Users/asmirnov/Documents/dev/src/llvm_34_ios_i386_installed_2/lib/libclang.a /Users/asmirnov/Library/Developer/Xcode/DerivedData/PocketIDE-axtxqjlvmrcxqsfnvgobybceosvb/Build/Products/Debug-iphonesimulator/StaticLibraryUsage2.app
    

    用于测试libclang的控制器代码:

    #import "Index.h"
    
    // ...
    
    // show diagnostics
    void showHighlight(CXTranslationUnit TU, const char *src_filename, unsigned filesize)
    {
        if (TU)
            NSLog(@"Translation unit valid: %s\n", src_filename);
    
        CXFile file = clang_getFile(TU, src_filename);
    
        if (file)
            NSLog(@"file is valid, size = %i\n", filesize);
    
        unsigned int line, column, offset;
    
        CXToken *tokens;
        unsigned numTokens;
    
        // range = all the file
        CXSourceLocation beginning = clang_getLocationForOffset(TU, file, 0);
        CXSourceLocation end = clang_getLocationForOffset(TU, file, filesize);
        CXSourceRange range = clang_getRange(beginning, end);
    
        clang_tokenize(TU, range, &tokens, &numTokens);
    
        NSLog(@"%i tokens:\n", numTokens);
    
        for (int i=0; i<numTokens; i++) {
            CXToken token = tokens[i];
    
            CXTokenKind kind = clang_getTokenKind(token);
            CXSourceLocation location = clang_getTokenLocation(TU, token);
            clang_getSpellingLocation(location, &file, &line, &column, &offset);
            CXString filename = clang_getFileName(file);
            CXString spelling = clang_getTokenSpelling(TU, token);
    
            NSLog(@"kind=[%s], [%s %i:%i offset=%i] [%s]\n",
                    kinds[kind], clang_getCString(filename), line, column, offset, clang_getCString(spelling));
    
            clang_disposeString(filename);
            clang_disposeString(spelling);
        }
    
        clang_disposeTokens(TU, tokens, numTokens);
    }
    
    unsigned getfilesize(const char* filename)
    {
        FILE *fp = fopen(filename, "r");
        fseek(fp, 0L, SEEK_END);
        unsigned sz = ftell(fp);
        fclose(fp);
        return sz;
    }
    - (void)testClang
    {
        NSLog(@"started");
    
        // get file from bundle
        NSString *ns_filename = [[NSBundle mainBundle] pathForResource:@"test_hl" ofType:@"cpp"];
        BOOL fileExists = [[NSFileManager defaultManager]fileExistsAtPath:ns_filename];
        NSLog(@"file %@ exists: %d", ns_filename, fileExists);
    
        CXIndex index = clang_createIndex(false, false);
    
        // command-line tu
        const char *filename = [ns_filename UTF8String];
        const char *argv = { filename };
        int argc = 1;
        CXTranslationUnit commandLineTu = clang_parseTranslationUnit(index, 0, &argv, argc, 0, 0, CXTranslationUnit_None);
    
        unsigned filesize = getfilesize(filename);
        showHighlight(commandLineTu, filename, filesize);
    
        clang_disposeTranslationUnit(commandLineTu);
        clang_disposeIndex(index);
    
        NSLog(@"finished");
    }
    
    - (void)viewWillAppear:(BOOL)animated
    {
        [self testClang];
    }
    

1 个答案:

答案 0 :(得分:4)

这个错误令人遗憾地试图告诉你的是,lib clang认为它的@rpath位于你的应用程序包的根目录中,但事实并非如此。

我前几天才想到这一点。 这很难。 你必须使用构建阶段设置使用libclang.a做一些疯狂的事情。 在目标的构建阶段中,您需要添加一些运行脚本阶段。

一个将是这个:

echo "warning: OTOOL BEGIN1";
pwd;
otool -L ${SRCROOT}/Clangwrap/ClangAndLLVM/lib/libclang.dylib;
echo "Warning: OTOOL END1";

这只是做了一些事情。 首先,它很好地用日志将事物夹在中间,看看发生了什么。 第二个是它真的只是在复制之后确认你的lib的路径。 (是的,你应该把它复制到你的项目中以保持理智。) otool这样做。 SRCROOT是项目的根,然后是你在Finder中项目文件夹中你将要使用的lib的相对路径。

同样,这只是证实了它是你认为的地方。

好的,下一步。这真是太过分了。

echo "BEGIN install_name_tool";
install_name_tool -id @executable_path/../Frameworks/libclang.dylib ${SRCROOT}/Clangwrap/ClangAndLLVM/lib/libclang.dylib;
echo "END install_name_tool";

这将运行模糊的命令行工具install_name_tool,用于设置lib认为它位于应用程序包中的路径。如果不这样做,lib会认为它在其他路径中。您将设置此路径以达到预期目标。在Mac应用程序中,我使用了捆绑包中的Frameworks文件夹,因此我将其设置为此类。 install_name_tool的第一个参数是应用程序包中lib的相对路径。第二个参数是lib当前在项目中的位置,以便install_name_tool可以设置其可执行路径。 这实际上修改了一些lib,因此它是可加载的。 lib必须知道自己的加载路径。

请注意,第一步仅适用于您自己的理智。 第二步是必需的,两者都应该在Compile Sources Build阶段之前发生。 单击并拖动以将其向上移动。

现在是最后一步。添加新的复制文件阶段,这将在构建阶段保持最后。 将目标设置为与install_name_tool的第一个arg相同,以便您知道您的相对路径已设置以及您将要放置lib的位置。 我用过Frameworks。 现在将文件添加到此Build Phase,对于iPhone,添加libclang.a(我不认为你可以在iOS上使用libclang.dylib)

注意,你看到我使用了libclang.dylib,因为我在Mac应用程序项目中这样做了。几乎任何事情都应该是相同的过程。

这不是一个简单或可发现的过程,我将它从几个博客和文档拼凑而成。