C 中的后期绑定

发布于 2024-09-06 19:32:43 字数 86 浏览 2 评论 0原文

C语言如何实现后期绑定? 任何人都可以提供一个例子吗?

我认为可以使用 dlopen 和 dlsym 来实现,但我不确定。如果我错了,请纠正我!

How can late binding can be achieved in C language?
Can anybody please provide an example.

I think it can be achieved using dlopen and dlsym but I am not sure about it.Please correct me if I am wrong!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧情别恋 2024-09-13 19:32:43

后期(又称为动态)绑定本身与动态加载模块(这就是 dlopen 和 dlsym 的作用)没有任何关系。相反,它是将调用哪个函数的决定延迟到运行时。

在 C 中,这是使用函数指针来完成的(这也是几乎所有 C++ 实现对虚拟函数执行此操作的方式)。

模拟这种情况的一种方法是传递函数指针结构,然后仅通过给定的函数指针调用函数。

一个例子:

typedef struct Animal {
    void (*sayHello)(struct Animal *a, const char *name);
} Animal;

static void sayQuakQuak( Animal *a, const char *name ) {
    printf( "Quak quak %s, says the duck at 0x%x!\n", name, a );
}

/* This function uses late binding via function pointer. */
void showGreeting( Animal *a, const char *name ) {
    a->sayHello( a, name );
}


int main() {
    struct Animal duck = {
        &sayQuakQuak
    };
    showGreeting( &duck, "John" );
    return 0;
}

Late (AKA Dynamic) Binding doesn't have anything to do with dynamically loaded modules (which is what dlopen and dlsym are about) per se. Instead, it is about delaying the decision about which function is called until runtime.

In C, this is done using function pointers (which is also how virtually any C++ implementation does it for virtual functions).

One way to emulate this is to pass structs of function pointers around and then only call functions via the given function pointers.

An example:

typedef struct Animal {
    void (*sayHello)(struct Animal *a, const char *name);
} Animal;

static void sayQuakQuak( Animal *a, const char *name ) {
    printf( "Quak quak %s, says the duck at 0x%x!\n", name, a );
}

/* This function uses late binding via function pointer. */
void showGreeting( Animal *a, const char *name ) {
    a->sayHello( a, name );
}


int main() {
    struct Animal duck = {
        &sayQuakQuak
    };
    showGreeting( &duck, "John" );
    return 0;
}
伤痕我心 2024-09-13 19:32:43

@Frerich Raabe:基本的后期绑定机制可以按照您所说的方式实现,但是您可以使用 dlopen/dlclose/dlsym 和函数指针的组合来获得类似的内容:

void *libraryHandle;
void (*fp)(void);

if (something)
        libraryHandle = dlopen("libmylibrary0.1");
else
    libraryHandle = dlopen("libmylibrary0.2");
fp = dlsym(libraryHandle, "my_function");
fp();

我认为这就是本杰明·巴顿正在寻找的东西。

@Frerich Raabe: The basic late binding mechanism can be implemented as your said, but you can use a combination of dlopen/dlclose/dlsym and pointers to function to get something like:

void *libraryHandle;
void (*fp)(void);

if (something)
        libraryHandle = dlopen("libmylibrary0.1");
else
    libraryHandle = dlopen("libmylibrary0.2");
fp = dlsym(libraryHandle, "my_function");
fp();

I think this is what Benjamin Button is looking for.

话少心凉 2024-09-13 19:32:43

正如 Frerich Raabe 提到的,这一切都是为了延迟调用哪个函数的决定,仅此而已。

为了强调它是 C/C++ 中的通用机制,我喜欢考虑下面的纯 C(和 C 风格编码,即 - 不存在任何“OO 概念”),以便掌握早期/后期绑定概念:

#include <stdio.h>

int add(int a, int b)
{
    return a + b;
}

int sub(int a, int b)
{
    return a - b;
}

int mul(int a, int b)
{
    return a * b;
}

int divide(int a, int b)
{
    return a / b;
}

int main(int argc, char** argv)
{
    int input;
    printf("main - please enter your choice of operation:1 - add, 2 - substruct, 3 - mulitply, 4 - division\n");
    scanf("%d", &input);
    int (*funcPointer)(int, int); 
    int a = 6, b = 2;
    switch(input)
    {
        case 1: funcPointer = &add;
                break;
        case 2: funcPointer = ⊂
                break;
        case 3: funcPointer = &mul;
                break;
        case 4: funcPointer = ÷
                break;
    }

    printf("main - running the desired function\n");

    // this is where LATE binding takes place
    int res = (*funcPointer)(a, b);
    printf("main - result of the desired function is:%d\n", res);

    // this is where EARLY binding takes place
    res = divide(a, b);
    printf("main - result of the divide function is:%d\n", res);
    return 0;
} 

Just as Frerich Raabe mentioned, it is all about delaying the decision of what function to invoke, that's it.

To emphasis that it is a general mechanism in C/C++, I love to consider the below pure C (and C style coding, i.e. - no presence of any "OO concepts") in order to grasp the early/late binding concept:

#include <stdio.h>

int add(int a, int b)
{
    return a + b;
}

int sub(int a, int b)
{
    return a - b;
}

int mul(int a, int b)
{
    return a * b;
}

int divide(int a, int b)
{
    return a / b;
}

int main(int argc, char** argv)
{
    int input;
    printf("main - please enter your choice of operation:1 - add, 2 - substruct, 3 - mulitply, 4 - division\n");
    scanf("%d", &input);
    int (*funcPointer)(int, int); 
    int a = 6, b = 2;
    switch(input)
    {
        case 1: funcPointer = &add;
                break;
        case 2: funcPointer = ⊂
                break;
        case 3: funcPointer = &mul;
                break;
        case 4: funcPointer = ÷
                break;
    }

    printf("main - running the desired function\n");

    // this is where LATE binding takes place
    int res = (*funcPointer)(a, b);
    printf("main - result of the desired function is:%d\n", res);

    // this is where EARLY binding takes place
    res = divide(a, b);
    printf("main - result of the divide function is:%d\n", res);
    return 0;
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文