C (lib*.so) 库中的动态链接

发布于 2024-09-26 00:04:57 字数 1089 浏览 10 评论 0原文

我编写了一段代码,其中将接受可执行文件和[lib*.so]库作为我的参数并链接@运行时。

我想还可以在运行时获取 (*.o) 文件中的函数并链接它。 但我不知道该怎么做。

编辑1: 我尝试链接的函数是 lib*.so 库中 .o 文件的一部分。 所以我想指定库名称以及位于同一库@运行时的函数名称。

例如。如果我的库包含两个函数(即 *.o 文件),链接器应该编译我想使用 @Run-Time 的函数。

我已经发布了代码,请帮忙:

#include <stdio.h>
#include <unistd.h>
#include <dlfcn.h>    // use -ldl

typedef float (*DepFn)(short, short);

int main(int argc, char* argv[])
{
    void* lib;
    DepFn df;

    if(argc < 2)
        return printf("USAGE: %s lib-file\n", argv[0]);

    lib = dlopen(argv[1], RTLD_NOW);
    if(lib == NULL)
        return printf("ERROR: Cannot load library\n");

    df = dlsym(lib, "Depreciation");
    if(df)
    {
        short l, i;

        printf("Enter useful-life of asset: ");
        scanf("%hd", &l);

        for(i = 1; i <= l; i++)
        {
            float d = 100 * df(l, i);
            printf("%hd\t%.1f%%\n", i, d);
        }
    }
    else
        printf("ERROR: Invalid library\n");

    dlclose(lib);
}

I have written a code where in it would take in a executable file and the [lib*.so] library as my arguments and link @ Run-time.

I want to also take in the function in the (*.o) file @ run-time and link it.
But I don't know how to go about it.

EDIT 1:
The function I'm trying to link is a part of the .o file in the lib*.so library.
So I want to specify the library name and also the function name which is in the same library @ Run-Time.

For eg. If my library contains two functions(i.e *.o files) the linker should compile the function which I want to use @Run-Time.

I have posted the code,please help :

#include <stdio.h>
#include <unistd.h>
#include <dlfcn.h>    // use -ldl

typedef float (*DepFn)(short, short);

int main(int argc, char* argv[])
{
    void* lib;
    DepFn df;

    if(argc < 2)
        return printf("USAGE: %s lib-file\n", argv[0]);

    lib = dlopen(argv[1], RTLD_NOW);
    if(lib == NULL)
        return printf("ERROR: Cannot load library\n");

    df = dlsym(lib, "Depreciation");
    if(df)
    {
        short l, i;

        printf("Enter useful-life of asset: ");
        scanf("%hd", &l);

        for(i = 1; i <= l; i++)
        {
            float d = 100 * df(l, i);
            printf("%hd\t%.1f%%\n", i, d);
        }
    }
    else
        printf("ERROR: Invalid library\n");

    dlclose(lib);
}

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

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

发布评论

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

评论(3

后知后觉 2024-10-03 00:04:57

如果需要在运行时获取函数名称,则需要将其传递到 argv[2] 中,并且不要在 dlsym 中使用 argv[2] 硬编码函数名称。

if(argc < 3)
        return printf("USAGE: %s lib-file function-name\n", argv[0]);

    lib = dlopen(argv[1], RTLD_NOW);
    if(lib == NULL)
        return printf("ERROR: Cannot load library\n");

    df = dlsym(lib, argv[2]);

If you need to take the function name at runtime, you need to pass it in argv[2], and instead of hardcoding function-name in the dlsym use argv[2].

if(argc < 3)
        return printf("USAGE: %s lib-file function-name\n", argv[0]);

    lib = dlopen(argv[1], RTLD_NOW);
    if(lib == NULL)
        return printf("ERROR: Cannot load library\n");

    df = dlsym(lib, argv[2]);
┾廆蒐ゝ 2024-10-03 00:04:57

您无法在运行时使用标准函数加载可重定位 (*.o)。您需要确保该对象被编译为与位置无关的代码(例如-fPIC),然后从中创建一个共享对象。像 ld -shared -o foo.so foo.o 这样的东西可能会成功。

You cannot load a relocatable (*.o) at run time using standard functions. You need to make sure the object is compiled as position independent code (e.g. -fPIC) and then make a shared object out of it. Something like ld -shared -o foo.so foo.o may do the trick.

瑕疵 2024-10-03 00:04:57

根据您的评论,您只想链接到您的共享库,

将代码更改为:

extern float Depreciation(short i,k); //should rather go in a header file

int main(int argc, char* argv[])
{
    short l, i;

        printf("Enter useful-life of asset: ");
        scanf("%hd", &l);

        for(i = 1; i <= l; i++)
        {
            float d = 100 * Depreciation(l, i);
            printf("%hd\t%.1f%%\n", i, d);
        }
    }

编译并链接到您的共享库:

 gcc -o myprogram myprogram.c -lXX

您的 libXX.so 需要安装在例如 /usr/lib/ 中才能使上述功能正常工作
请参阅此处了解更多信息。

Based on your comments, you just want to link to your shared library,

change your code to:

extern float Depreciation(short i,k); //should rather go in a header file

int main(int argc, char* argv[])
{
    short l, i;

        printf("Enter useful-life of asset: ");
        scanf("%hd", &l);

        for(i = 1; i <= l; i++)
        {
            float d = 100 * Depreciation(l, i);
            printf("%hd\t%.1f%%\n", i, d);
        }
    }

Compile and link to your shared library:

 gcc -o myprogram myprogram.c -lXX

Your libXX.so would need to be installed in e.g. /usr/lib/ for the above to work
See here for more info.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文