【发布时间】:2012-01-04 23:06:36
【问题描述】:
我就是不明白,为什么第 22 行编译失败?
#include <stdexcept>
#include <dlfcn.h>
#include "Library.h"
int main(int argc, char *argv[])
{
try
{
void* libHandle = 0;
libHandle = dlopen("libExpandableTestLibrary.so", RTLD_LAZY);
if(!libHandle)
throw std::logic_error(dlerror());
std::cout << "Libary opened gracefully" << std::endl;
void* fuPtr = 0;
fuPtr = dlsym(libHandle, "createLibrary");
if(!fuPtr)
throw std::logic_error(dlerror());
Library* libInstance = static_cast<Library* ()>(fuPtr)();
// Tutorial: http://www.linuxjournal.com/article/3687
// Tutorial Code: shape *my_shape = static_cast<shape *()>(mkr)();
// Compiler error message: Application.cpp:22:56: error: invalid static_cast from type ‘void*’ to type ‘Library*()’
libInstance->Foo();
dlclose(libHandle);
} catch(std::exception& ex)
{
std::cerr << ex.what() << std::endl;
}
}
欢迎任何帮助 如果您需要更多信息,请告诉我。
【问题讨论】:
-
试试
reinterpret_cast-void*和其他演员不要混用。 -
@JohnDibling,它内嵌在代码中...
-
@Nim:不完全是。从
void*转换为任何其他对象指针类型(可能以前隐式转换为void*)正确的方法是static_cast。 -
@PlasmaHH,可能有一个对象指针,但也有一个函数指针?
-
对于想要创建动态类加载代码的人,请查看这个单头库:github.com/davidalbertonogueira/…@Disclaimer:我是上述库的维护者。
标签: c++ shared-libraries function-pointers