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

时间:2016-05-10 05:21:23

标签: objective-c xcode macos dlopen apple-tv

我想在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复制到我的项目中并验证它是否被复制为捆绑资源。

  4. 运行我的应用程序时,在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
    

    任何人都可以告诉我我做错了什么?

    由于

1 个答案:

答案 0 :(得分:0)

事实上,似乎没有办法动态加载...

我刚刚使用静态链接..