本文介绍了检测并在Objective-C运行使用可选的外部C库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个SDK的iPhone开发者可以在其项目。这是作为一个编制了。一个无源$ C ​​$ C。让我们把我的SDKAAA。

I am building an SDK that iPhone developers can include in their projects. It is delivered as a compiled ".a", without source code. Let's call my SDK "AAA".

在他的项目的客户(我们称之为BBB),除了使用AAA,也可以使用所谓的CCC第三方库 - 其中还附带pre-编译闭源。我不卖CCC,这是一个不同的公司。

The customer in his project (let's call it "BBB"), in addition to use AAA, may also use a 3rd party library called "CCC" - which also comes pre-compiled, closed-source. I do not sell CCC, it's a different company.

我的SDK,AAA,可以选择使用CCC来改进产品,使用这些第三方功能。例如,假设CCC是一个安全的SDK来加密的东西。 AAA不需要CCC,但会更安全,如果客户选择包括CCC在他们的项目以及

My SDK, AAA, can optionally use CCC to improve the product, using those 3rd party features. For example, let's say CCC is a security SDK to encrypt something. AAA does not require CCC, but will be more secured if the customer chooses to include CCC in their project as well.

现在这里是一个额外的棘手的部分 - CCC库,是纯C code,国产C结构和C函数 - 没有面向对象吧

Now here is an extra tricky part - the CCC library, is pure C code, made of C Structs and C functions - nothing object-oriented about it.

的问题是:


  • 如何编译我AAA SDK使用从CCC功能/结构,而不包括在我的项目CCC(而不是法律允许,不想要跟上的最新版本更新)。

  • 我如何可以检测如果客户在其项目有CCC,仅可用?
  • 来使用这些额外的功能
  • How can I compile my AAA SDK to use functions/structs from CCC, without including CCC in my project (not legally allowed to, and don't want to keep up to date with version updates).
  • How can I detect if the customer has CCC in his project, to use those extra features only if available?

推荐答案

使用度日函数名的C函数指针。如果能找到他们,他们在那里。否则,他们不是。只需使用 RTLD_DEFAULT 作为第一个参数。

Use dlsym to get the C function pointers by function name. If it can find them, they're there. Otherwise they're not. Just use RTLD_DEFAULT as the first parameter.

编辑:抛下周围的是iOS示例,请参阅迈克灰的write达PLWeakCompatibility ,特别是在下降到'一节中。你会看到他检查 objc_loadWeakRetained (运行时调用相关的弱引用)是present。在5+这是和他的版本直接调用真正之一。在4也不是那么他的版本做别的东西来代替。

having cast around for an iOS example, see Mike Ash's write up of PLWeakCompatibility, particularly the section on 'Falling Through'. You'll see he checks whether objc_loadWeakRetained (a runtime call related to weak references) is present. Under 5+ it is and his version calls the real one directly. Under 4 it isn't so his version does something else instead.

EDIT2:样品code:

sample code:

示例1:

#import <Foundation/Foundation.h>
#include <dlfcn.h>

int main(int argc, char *argv[]) 
{
    @autoreleasepool
    {
        NSLog(@"%p", dlsym(RTLD_DEFAULT, "someFunc"));
    }
}

输出为0x0 。示例2:

#import <Foundation/Foundation.h>
#include <dlfcn.h>

void someFunc()
{

}

int main(int argc, char *argv[])
{
    @autoreleasepool
    {
        NSLog(@"%p", dlsym(RTLD_DEFAULT, "someFunc"));
    }
}

输出比为0x0以外的地址。

Outputs an address other than 0x0.

示例3:

#import <Foundation/Foundation.h>
#include <dlfcn.h>

void someFunc()
{
    NSLog(@"Hi!");
}

int main(int argc, char *argv[])
{
    @autoreleasepool
    {
        void (* func)();
        func = dlsym(RTLD_DEFAULT, "someFunc");
        func();
    }
}

输出你好!

结构体没有presence在某或其他地方在运行时,他们只是在如何格式化数据的指令编译器。所以你需要包括任何实际的结构或它们在你的code兼容的重述。

Structs have no presence in a .a or elsewhere at runtime, they're just instructions to the compiler on how to format data. So you'll need to include either the actual structs or a compatible restatement of them in your code.

这篇关于检测并在Objective-C运行使用可选的外部C库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 22:41