- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 ctypes 并两次加载相同的编译 Fortran 库,这样我就有了它的两个独立实例,这样库包含的任何模块变量都不会存储在相同的内存位置。描述的一般解决方案(例如,此处:https://mail.python.org/pipermail/python-list/2010-May/575368.html)是提供库的完整路径,而不仅仅是其名称。但是,我无法让它像这样工作。这是一个演示问题的最小工作示例:
测试.f90:
module test
use iso_c_binding, only: c_int
implicit none
integer :: n
contains
integer(c_int) function get() bind(c, name='get')
get = n
end function get
subroutine set(new_n) bind(c, name='set')
integer(c_int), intent(in) :: new_n
n = new_n
end subroutine set
end module test
测试.py:
import os
from ctypes import cdll, c_int, byref
if __name__ == '__main__':
lib1 = cdll.LoadLibrary(os.path.abspath('test.so'))
lib2 = cdll.LoadLibrary(os.path.abspath('test.so'))
lib1.set(byref(c_int(0)))
lib2.set(byref(c_int(1)))
print(lib1.get())
Fortran 库使用以下命令编译:
gfortran -shared -fPIC -o test.so test.f90
当我运行 python test.py
时,我得到 1
作为输出,而我想得到 0
。有谁知道如何进行这项工作?
最佳答案
[Python.Docs]: ctypes - A foreign function library for Python使用 DlOpen 加载库(在 Nix 上)。根据[Man7]: DLOPEN(3) :
If the same shared object is loaded again with dlopen(), the same object handle is returned. The dynamic linker maintains reference counts for object handles, so a dynamically loaded shared object is not deallocated until dlclose() has been called on it as many times as dlopen() has succeeded on it.
我准备了一个小例子。
在继续之前,检查[SO]: C function called from Python via ctypes returns incorrect value (@CristiFati's answer)有关使用 CTypes 时经常遇到的错误(也在问题中)的详细信息。
dll00.c:
#if defined(_WIN32)
# define DLL00_EXPORT_API __declspec(dllexport)
#else
# define DLL00_EXPORT_API
#endif
static int val = -1;
DLL00_EXPORT_API int get()
{
return val;
}
DLL00_EXPORT_API void set(int i)
{
val = i;
}
code00.py:
#!/usr/bin/env python
import ctypes as cts
import os
import shutil
import sys
def get_dll_funcs(dll):
get_func = dll.get
get_func.argtypes = ()
get_func.restype = cts.c_int
set_func = dll.set
set_func.argtypes = (cts.c_int,)
set_func.restype = None
return get_func, set_func
def main(*argv):
dll00 = "./dll00.so"
dll01 = "./dll01.so"
dir00 = "dir00"
os.makedirs(dir00, exist_ok=True)
shutil.copy(dll00, dir00)
shutil.copy(dll00, dll01)
dll_names = [dll00, os.path.abspath(dll00), os.path.join(dir00, dll00), dll01]
dlls = [cts.CDLL(item) for item in dll_names]
for idx, dll in enumerate(dlls):
print("Item {:d} ({:s}) was loaded at {:08X}".format(idx, dll_names[idx], dll._handle))
set_func = get_dll_funcs(dll)[1]
set_func(idx * 10)
for idx, dll in enumerate(dlls):
get_func = get_dll_funcs(dll)[0]
print("Item {:d} get() returned {: d}".format(idx, get_func()))
if __name__ == "__main__":
print("Python {:s} {:03d}bit on {:s}\n".format(" ".join(elem.strip() for elem in sys.version.split("\n")),
64 if sys.maxsize > 0x100000000 else 32, sys.platform))
rc = main(*sys.argv[1:])
print("\nDone.\n")
sys.exit(rc)
输出:
[cfati@cfati-5510-0:/mnt/e/Work/Dev/StackOverflow/q054243176]> ~/sopr.sh
### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###
[064bit prompt]>ls
code00.py dll00.c
[064bit prompt]> gcc -fPIC -shared -o dll00.so dll00.c
[064bit prompt]> ls
code00.py dll00.c dll00.so
[064bit prompt]> python ./code.py
Python 3.5.2 (default, Nov 12 2018, 13:43:14) [GCC 5.4.0 20160609] 064bit on linux
Item 0 (./dll00.so) was loaded at 02437A80
Item 1 (/mnt/e/Work/Dev/StackOverflow/q054243176/dll00.so) was loaded at 02437A80
Item 2 (dir00/./dll00.so) was loaded at 02438690
Item 3 (./dll01.so) was loaded at 02438EF0
Item 0 get() returned 10
Item 1 get() returned 10
Item 2 get() returned 20
Item 3 get() returned 30
从输出中可以看出(还要注意 _handle 属性),尝试多次加载相同的 .dll(通过其路径)(相同的行为在 Win 上):
如果位于同一路径(即使指定不同),实际上不会再次加载它,它只会增加其RefCount
如果名称或位置不同,则重新加载
简而言之,回答您的问题:只需将其复制到不同的名称下并加载即可。
关于python - 具有 Ctypes 的独立 CDLL 库实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54243176/
Java 库和 android 库有什么区别,各自有什么优点/缺点? 最佳答案 您可以在 Android 应用程序中包含标准 Java .jar 文件库。它们在 .apk 构建时被翻译成 Dalvik
所以,我现在的代码就像从 Java 层加载库(比如 liba.so),并在内部 liba.so 加载 libb.so。因此,如果我必须将所有库打包到 APK 中并将其安装在没有 root 访问权限的设
我想在我的系统中设置 LEDA 库。 我已经从以下链接下载了 LEDA 库 http://www.algorithmic-solutions.info/free/d5.php Instruct
我想用 autoconf 创建一个共享库。但是,我希望共享库具有“.so”扩展名,而不是以“lib”开头。基本上,我想制作一个加载 dlopen 的插件。 .是否有捷径可寻? 当我尝试使用 autoc
我需要在 Apps 脚本应用程序上修改 PDF。为此,我想使用 JS 库:PDF-LIB 我的代码: eval(UrlFetchApp.fetch("https://unpkg.com/pdf-lib
我正在构建一个使用以下 Boost header 的程序(我使用的是 Microsoft Visual C++ 10), #include #include #include #include
当我通过 cygwin 在 hadoop 上运行此命令时: $bin/hadoop jar hadoop-examples-*.jar grep input output 'dfs[a-z.]+' 我
我已经通过 vcpgk 成功安装了一个 C++ 库,名为:lmdb:x64-windows 我还安装了lmdb通过 Cabal 安装的 Haskell 绑定(bind)包 在尝试测试 lmdb 包时:
我该如何解决这个问题? 我刚刚将 javacv jar 文件复制到我的项目 Lib 文件夹下,但出现了这个错误! 我可以找到这个thread来自谷歌,但不幸的是,由于我国的谷歌限制政策,该页面无法打开
我有一个 Android 库项目 FooLib。 FooLib 引用 Android Context 之类的东西,但不需要任何资源文件(res/ 中的东西)所以我目前将其打包为供我的应用使用的 JAR
我正在开发一个 Android 应用程序(使用 Android Studio),它能够通过手势识别算法了解您正在进行的 Activity 。对于我使用 nickgillian ithub 帐户上可用的
关于从 .NET Framework 项目中引用 .NET Standard 类库的问题有很多类似的问题,其中 netstandard 库中的 NuGet 包依赖项不会流向 netframework
我已经从互联网上下载了 jna-4.2.2.jar,现在想将这个 jar 导入到我的项目中。但是当我试图将这个 jar 导入我的项目时,出现以下错误。 [2016-06-20 09:35:01 - F
我正在尝试通过编译在 Mac 上安装 rsync 3.2.3。但是,我想安装所有功能。为此,它需要一些库,此处 ( https://download.samba.org/pub/rsync/INSTA
进入 Web 开发有点困难。过去 5 年我一直致力于 winforms 工作。所以我正在努力从一种切换到另一种。前段时间,我使用过 JavaScript,但现在还没有大量的 JavaScript 库
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我正在寻找一个用Python编写的与logstash(ruby + java)类似的工具/库。 我的目标是: 从 syslog 中解析所有系统日志 解析应用程序特定日志(apache、django、m
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
我花了几天时间试图寻找用于 JavaPOS 实现的 .jar 库,但我找不到任何可以工作的东西。我找到了很多像这样的文档:http://jpos.1045706.n5.nabble.com/file/
这个问题在这里已经有了答案: Merge multiple .so shared libraries (2 个答案) 关闭 9 年前。 我有我在代码中使用的第三方库的源代码和对象。该库附带有关如何使
我是一名优秀的程序员,十分优秀!