Apple TV - 创建和加载动态库 (dylib)

Apple TV - creating and loading dynamic library (dylib)

我想在我的 Apple TV 项目中加载一个 c/c++ 共享对象。 我使用以下教程创建了一个简单的库:

Link

由于我想编译并将其加载到我的 Apple TV 项目中,我做了一些更改:

  1. dlopen 代码在我的 ViewController.m 中,用户按下按钮然后调用 dlopen。

    // Open the library.
    NSString * libraryname = [NSString stringWithFormat:@"libRatings.A"];
    NSString * libraryfullpath = [mainBundle pathForResource:libraryname ofType:@"dylib"];
    
    void *lib_handle = dlopen([libraryfullpath UTF8String], RTLD_NOW);
    if (lib_handle)
    {
     printf("[%s] dlopen(\"%s\", RTLD_NOW): Successful\n", __FILE__,      "library");
    }
    else
    {
        printf("\n\n[%s] Unable to open library: %s\n",
           __FILE__, dlerror());
        exit(EXIT_FAILURE);
    }
    
  2. 将编译命令改为:

    clang -dynamiclib -std=gnu99 -current_version 1.0 -compatibility_version 1.0 -fvisibility=hidden -arch arm64 -mtvos-version-min=9.2 -g -Wno-sign-conversion -fembed-bitcode-marker -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS9.2.sdk Ratings.c -o libRatings.A.dylib
    
  3. 我将我的 dylib 复制到我的项目中并验证它被复制为一个包资源。

当 运行 我的应用程序在 dlopen 调用失败后出现以下错误:

[/Users/gfsrnd/Documents/sample1/sample1/sample1/ViewController.m] Unable to open library: dlopen(/var/containers/Bundle/Application/CFF7EC2A-7DF5-4270-9E99-2D5FAEBB0275/sample1 copy.app/libRatings.A.dylib, 2): no suitable image found.  Did find:
/var/containers/Bundle/Application/CFF7EC2A-7DF5-4270-9E99-2D5FAEBB0275/sample1 copy.app/libRatings.A.dylib: mmap() error 1 at address=0x1024D4000, size=0x00008000 segment=__TEXT in Segment::map() mapping /var/containers/Bundle/Application/CFF7EC2A-7DF5-4270-9E99-2D5FAEBB0275/sample1 copy.app/libRatings.A.dylib

otool 的结果是:

**> otool -TV libRatings.A.dylib**
libRatings.A.dylib:
Table of contents (0 entries)
module name      symbol name

nm 的结果是:

    **>nm -ag libRatings.A.dylib**

             U ___stack_chk_fail
             U ___stack_chk_guard
             U ___strncat_chk
    0000000000007d30 T _addRating
    0000000000007ecc T _clearRatings
    0000000000007db8 T _meanRating
                     U _memset
                     U _printf
    0000000000007ebc T _ratings
                     U _strdup
                     U _strlen
                     U dyld_stub_binder

谁能告诉我我做错了什么?

谢谢

确实,好像没办法动态加载...

我只是使用了静态链接..