iOS 6.1 Dynamic Library build and link

后端 未结 2 1442
别那么骄傲
别那么骄傲 2020-12-18 11:46

I am trying to create a dynamic library for iOS and load it at runtime. After taking a look at this question and this answer, I have been doing it using iOSOpenDev and deplo

相关标签:
2条回答
  • 2020-12-18 11:59

    As Victor Ronin pointed out, "dlsym" is for C symbols. To obtain objective-C class from dylib that you linked at runtime you can use objc runtime functions. In your case:

    void* dylibLink = dlopen("/usr/lib/libKDylibTwo.dylib", RTLD_NOW);
    id KDylibTwo = [[objc_getClass("KDylibTwo") alloc] init];
    [KDylibTwo run];
    

    First line is linking your library at runtime. This is required in order to use code inside of it.

    Second line creates instance of class KDylibTwo. objc_getClass function returns class object that you can later use to create instances of this class like you would with any objective-C class - using alloc and init methods. Once you obtained class object with objc_getClass you can work with him like nothing happend. At this point you can forget that you dynamically linked you library at runtime.

    Third line is calling run method. As you can see, it's normal objective-C syntax. Nothing is changed because you linked your library at runtime. You can call any method you want.

    0 讨论(0)
  • 2020-12-18 12:23

    I have never used dlsym to get a pointer to objective c method (I believe it's not possible to call objective c method through dlsym, but I may be wrong).

    However, most important bit of information that second parameter of dlsym should be the symbol name.

    Symbol name "run" will work only for C function. Something like:

    EXPORT void run()
    {
     NSLog(@"Run");
    }
    

    Object C method has more complex symbol names and as I said, I am not sure whether they could be passed to dlsym at all.

    0 讨论(0)
提交回复
热议问题