当前位置: 动力学知识库 > 问答 > 编程问答 >

xcode - How to re-export a dylib symbol from a dependent bundle

问题描述:

Working with Xcode, I'm looking to re-export a symbol (a function specifically) from a mach-o bundle binary, where the symbol is originally defined in a dylib.

I've tried the -sub_library linker switch but that doesn't seem to re-export the dylib symbols, probably because I'm not building a dylib myself(?)

And the reexport-l / reexport_library switches seem to be unsupported in Xcode's linker.

Any ideas?

网友答案:

This might be what you're looking for, if I understood you correctly. I'll be using libpthread as the hypothetical dylib containing functions you want to re-export.

mybundle.c:

#include <pthread.h>
#include <stdio.h>
void *foo(void *ctx) {
    puts((char *)ctx);
    return 0;
}

mybundle.exp:

_foo
_pthread_create
_pthread_join

Compile the bundle, dynamically linking to libpthread.dylib:

josh$ gcc -bundle -lpthread -Wl,-exported_symbols_list,mybundle.exp -o mybundle.so mybundle.c

myloader.c:

#include <dlfcn.h>
#include <pthread.h>    // merely for type definitions
#include <assert.h>
#include <stdio.h>

int main() {
    void *(*foo)(void *ctx);
    /* the following cannot be declared globally without changing their names, 
       as they are already (and unnecessarily) declared in <pthread.h> */
    int (*pthread_create)(pthread_t *thrd, const pthread_attr_t *attr, void *(*proc)(void *), void *arg);
    int (*pthread_join)(pthread_t thrd, void **val);

    void *bundle;
    assert(bundle = dlopen("mybundle.so", RTLD_NOW));
    assert(foo = dlsym(bundle, "foo"));
    assert(pthread_create = dlsym(bundle, "pthread_create"));
    assert(pthread_join = dlsym(bundle, "pthread_join"));

    pthread_t myThrd;
    pthread_create(&myThrd, 0, foo, "in another thread");
    pthread_join(myThrd, 0);

    return 0;
}

Compile the loader:

josh$ gcc myloader.c -o myloader

Run:

josh$ ./myloader
in another thread

Observe that myloader is in no way linked to pthread, yet pthread functions are loaded and available at runtime through the bundle.

分享给朋友:
您可能感兴趣的文章:
随机阅读: