功能,从对dlsym返回值()? [英] function that returns value from dlsym()?

查看:157
本文介绍了功能,从对dlsym返回值()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我敢肯定,愚蠢的问题是语法的一些位那是不对的。我如何对dlsym与返回值的函数工作?我发现了错误无效的void *转换成LSError(*)()在以下code - 试图让编译Linux LightScribe的示例程序,希望我能对OSX dylib链接它(为什么地狱不会释放HP实际可可SDK LS才出现了什么6或7年了):???

 无效* LSHandle =的dlopen(liblightscribe.1.dylib,RTLD_LOCAL | RTLD_LAZY);
    如果(LSHandle){
        LSError(* LS_DiscPrinter_ReleaseExclusiveUse)()=的dlsym(LSHandleLS_DiscPrinter_ReleaseExclusiveUse);..
lsError = LS_DiscPrinter_ReleaseExclusiveUse(pDiscPrinter);


解决方案

C标准实际上并不转换和从函数指针定义行为。有所不同的解释,为什么;最常见的是,并不是所有的架构实现函数指针的简单数据指针。在某些体系结构,功能可以驻留在内存是不可寻址使用指针为void一个完全不同的领域。

&将ldquo;推荐”的使用方法则dlsym 是:

  LSError(* LS_DiscPrinter_ReleaseExclusiveUse)(LS_DiscPrinterHandle); *(无效**)及LS_DiscPrinter_ReleaseExclusiveUse = dlsym进行(LS_DiscPrinter_ReleaseExclusiveUse);

阅读POSIX页面上的基本原理和示例 对dlsym 了解详情。

Stupid question that I'm sure is some bit of syntax that's not right. How do I get dlsym to work with a function that returns a value? I'm getting the error 'invalid conversion of void* to LSError (*)()' in the following code - trying to get the compile the linux lightscribe sample program hoping that I can link it against the OSX dylib (why the hell won't HP release an actual Cocoa SDK? LS has only been around for what? 6 or 7 years now?):

void* LSHandle = dlopen("liblightscribe.1.dylib", RTLD_LOCAL|RTLD_LAZY);
    if (LSHandle) {
        LSError (*LS_DiscPrinter_ReleaseExclusiveUse)() = dlsym(LSHandle, "LS_DiscPrinter_ReleaseExclusiveUse");

..
lsError = LS_DiscPrinter_ReleaseExclusiveUse( pDiscPrinter);

解决方案

The C standard does not actually define behaviour for converting to and from function pointers. Explanations vary as to why; the most common being that not all architectures implement function pointers as simple pointers to data. On some architectures, functions may reside in an entirely different segment of memory that is unaddressable using a pointer to void.

The “recommended” way to use dlsym is:

 LSError (*LS_DiscPrinter_ReleaseExclusiveUse)(LS_DiscPrinterHandle);

 *(void **)&LS_DiscPrinter_ReleaseExclusiveUse = dlsym("LS_DiscPrinter_ReleaseExclusiveUse");

Read the rationale and example on the POSIX page for dlsym for more information.

这篇关于功能,从对dlsym返回值()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆