Bil*_*ard 12 c++ linux g++ shared-libraries
我正在尝试使用g ++ 从Program-Library-HOWTO编译以下简单的DL库示例代码.这只是一个例子,所以我可以学习如何使用和编写共享库.我正在开发的库的真正代码将用C++编写.
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>
int main(int argc, char **argv) {
void *handle;
double (*cosine)(double);
char *error;
handle = dlopen ("/lib/libm.so.6", RTLD_LAZY);
if (!handle) {
fputs (dlerror(), stderr);
exit(1);
}
cosine = dlsym(handle, "cos");
if ((error = dlerror()) != NULL) {
fputs(error, stderr);
exit(1);
}
printf ("%f\n", (*cosine)(2.0));
dlclose(handle);
}
Run Code Online (Sandbox Code Playgroud)
如果我用gcc编译程序,它工作正常.
gcc -o foo foo.c -ldl
Run Code Online (Sandbox Code Playgroud)
当我将文件名和编译器更改为以下内容时
g++ -o foo foo.cpp -ldl
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
foo.cpp:16:错误:无效转换为'void*'到'double(*)(double)'
我理解(我想我理解,如果这是错的,请纠正我)我不能从C++中的void指针进行隐式转换,但C让我,这就是为什么上面的代码将使用gcc编译而不是使用克++.所以我尝试通过将上面的第16行更改为:
cosine = (double *)dlsym(handle, "cos");
Run Code Online (Sandbox Code Playgroud)
有了这个,我收到以下错误:
foo.cpp:16:错误:在赋值时无法将'double*'转换为'double(*)(double)'
这些问题可能与我自己对正确的C++编码标准的普遍无知有关.任何人都可以向我指出一个关于使用C++示例代码开发Linux动态库的好教程吗?
Com*_*ger 26
C允许隐式转换void *
为任何指针类型(包括函数指针); C++需要显式转换.正如leiflundgren所说,你需要将返回值强制转换为你需要dlsym()
的函数指针类型.
很多人发现C的函数指针语法很尴尬.一种常见的模式是typedef函数指针:
typedef double (*cosine_func_ptr)(double);
Run Code Online (Sandbox Code Playgroud)
您可以将函数指针变量定义cosine
为类型的成员:
cosine_func_ptr cosine;
Run Code Online (Sandbox Code Playgroud)
并使用类型而不是笨拙的函数指针语法进行强制转换:
cosine = (cosine_func_ptr)dlsym(handle, "cos");
Run Code Online (Sandbox Code Playgroud)
dlsym
返回指向符号的指针.(至于void*
通用.)在你的情况下,你应该将它强制转换为函数指针.
double (*mycosine)(double); // declare function pointer
mycosine = (double (*)(double)) dlsym(handle, "cos"); // cast to function pointer and assign
double one = mycosine(0.0); // cos(0)
Run Code Online (Sandbox Code Playgroud)
所以这种罕见的情况之一,编译器错误是一个很好的线索.;)
归档时间: |
|
查看次数: |
19140 次 |
最近记录: |