C (lib*.so) 库中的动态链接
我编写了一段代码,其中将接受可执行文件和[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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果需要在运行时获取函数名称,则需要将其传递到 argv[2] 中,并且不要在 dlsym 中使用 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].您无法在运行时使用标准函数加载可重定位 (
*.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 likeld -shared -o foo.so foo.o
may do the trick.根据您的评论,您只想链接到您的共享库,
将代码更改为:
编译并链接到您的共享库:
您的 libXX.so 需要安装在例如 /usr/lib/ 中才能使上述功能正常工作
请参阅此处了解更多信息。
Based on your comments, you just want to link to your shared library,
change your code to:
Compile and link to your shared library:
Your libXX.so would need to be installed in e.g. /usr/lib/ for the above to work
See here for more info.