请大家帮小弟我看个程序
请大家帮我看个程序
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
typedef double (*pfunc)(double);
int main(void)
{
void *handle;
pfunc func;
handle = dlopen("libm.so", RTLD_NOW);
func = dlsym(handle, "sqrt");
if (handle == NULL || func == NULL) {
printf("Open so failed: %s.\n", dlerror());
exit(EXIT_FAILURE);
}
printf("add = %f\n", func(2));
dlclose(handle);
return 0;
}
------解决方案--------------------
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
typedef double (*pfunc)(double);
int main(void)
{
void *handle;
pfunc func;
handle = dlopen("libm.so", RTLD_NOW);
func = dlsym(handle, "sqrt");
if (handle == NULL || func == NULL) {
printf("Open so failed: %s.\n", dlerror());
exit(EXIT_FAILURE);
}
printf("add = %f\n", func(2));
dlclose(handle);
return 0;
}
------解决方案--------------------
- C/C++ code
#include <dlfcn.h> #include <stdio.h> #include <stdlib.h> typedef double (*pfunc)(double); // 定义一个函数指针数据类型pfunc,这种数据类型 // 用来指向参数是double,返回值也是double的函数 int main(void) { void *handle; // 定义一个指针 pfunc func; // 定义一个函数指针func handle = dlopen("libm.so", RTLD_NOW); // 装载动态库libm.so func = dlsym(handle, "sqrt"); // 从动态库中获取函数sqrt的函数指针 if (handle == NULL || func == NULL) // 如果获取失败 { printf("Open so failed: %s.\n", dlerror()); exit(EXIT_FAILURE); } printf("add = %f\n", func(2)); // 计算2的平方根 dlclose(handle); // 关闭动态库 return 0; }