ctypes.cdll.LoadLibrary(None)如何工作? [英] How does ctypes.cdll.LoadLibrary(None) work?

查看:257
本文介绍了ctypes.cdll.LoadLibrary(None)如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ctypes.cdll.LoadLibrary()调用如何与作为参数传递的 None 一起工作?当我尝试下面的代码时, math 库似乎自动加载:

How does ctypes.cdll.LoadLibrary() call work with None passed in as an argument? When I try the code below, it seems that the math library gets loaded automatically:

>>> import ctypes
>>> lib = ctypes.cdll.LoadLibrary(None)
>>> lib.sin
<_FuncPtr object at 0x7f36dd65f430>
>>> lib.exp
<_FuncPtr object at 0x7f36dd65f4f8>
>>> 

math 库如何在不加载的情况下加载被明确指定?是否加载了标准库中的所有共享库?幕后发生了我不了解的事情。

How does the math library get loaded without being explicitly specified? Do all shared libraries in the standard library get loaded? There is something happening behind the scenes that I don't understand.

推荐答案

注意:您遇到了在 Nix 上(在 Win 上是不可复制的)。

Note: You encountered this on Nix (on Win it isn't reproducible).

看看[SO]:如何检查文件是否存在,没有例外? (@CristiFati的答案)
在那里,项目#4 的最后一部分(注释部分),我解释了这种确切的情况,引用了 [man7]的以下段落:DLOPEN (3)

Take a look at [SO]: How to check whether a file exists without exceptions? (@CristiFati's answer).
There, in the last part (the Notes section) of item #4, I explained this exact scenario, quoting the following passage from [man7]: DLOPEN(3):


如果 filename 为NULL,则返回的句柄用于main
程序。当赋予 dlsym ()时,此句柄将在主程序中搜索
符号,然后搜索在
程序启动时加载的所有共享库,然后搜索所有共享库由 dlopen ()加载并带有
标志 RTLD_GLOBAL

If filename is NULL, then the returned handle is for the main program. When given to dlsym(), this handle causes a search for a symbol in the main program, followed by all shared objects loaded at program startup, and then all shared objects loaded by dlopen() with the flag RTLD_GLOBAL.

根据 [Python 3]加载库时使用的:加载共享库


所有这些类都可以通过使用至少一个参数(即路径名)来实例化。共享库。如果您具有已加载的共享库的现有句柄,则可以将其作为 handle 命名参数进行传递,否则将作为基础平台 dlopen LoadLibrary 函数用于将库加载到进程中并获取其句柄。

All these classes can be instantiated by calling them with at least one argument, the pathname of the shared library. If you have an existing handle to an already loaded shared library, it can be passed as the handle named parameter, otherwise the underlying platforms dlopen or LoadLibrary function is used to load the library into the process, and to get a handle to it.

这篇关于ctypes.cdll.LoadLibrary(None)如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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