博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python动态加载so文件
阅读量:6172 次
发布时间:2019-06-21

本文共 1361 字,大约阅读时间需要 4 分钟。

hot3.png

解决方案1:

#!/usr/bin/env pythonimport os,sys,time,refrom ctypes import *... ...def function1():    ... ...    sStdPath = os.path.dirname(sStcPath) + "/libstdc++.so.6"    stc_lib = CDLL(sStdPath)

解决方案二:

ctypes.cdll.LoadLibrary('/abs/path/to/a.so')ctypes.cdll.LoadLibrary('/abs/path/to/b.so')

方案三:

Compile your binary with a rpath relative to the current working directory like:

gcc -shared -o yourbinary.so yoursource.c otherbinary.so \    -Wl,-rpath='.',-rpath='./another/relative/rpath' -fpic

Then, you are able to change the working directory in python at runtime with:

import osos.chdir('/path/to/your/binaries')

Like this, the loader also finds other dynamic libraries like otherbinary.so

 

LD_LIBRARY_PATH不生效的原因:

31down voteaccepted

By the time a program such as Python is running, the dynamic loader (ld.so.1 or something similar) has already read LD_LIBRARY_PATH and won't notice any changes thereafter. So, unless the Python software itself evaluates LD_LIBRARY_PATH and uses it to build the possible path name of the library for dlopen() or an equivalent function to use, setting the variable in the script will have no effect.

Given that you say it doesn't work, it seems plausible to suppose that Python does not build and try all the possible library names; it probably relies on LD_LIBRARY_PATH alone.

转载于:https://my.oschina.net/activehealth/blog/888965

你可能感兴趣的文章
系统进程管理工具Process Explorer
查看>>
富士通仍执着SPARC架构芯片 将坚持推新
查看>>
易宪容:企业要利用大数据挖掘潜在需求
查看>>
微软声称Win10周年更新为Edge浏览器带来更好电池寿命
查看>>
混合云是企业IT的未来吗?
查看>>
LINE在日本取得成功 但全球化之路还很长
查看>>
红帽云套件新增QuickStart Cloud Installer,加快私有云部署
查看>>
MapXtreme 2005 学习心得 一些问题(八)
查看>>
流量精细化运营时代,营销SaaS之使命——流量掘金
查看>>
雅虎同意出售核心资产
查看>>
Win10大丰收的节奏 微软收编iOS全部150万应用
查看>>
智慧城市要除“城市病” 中兴通讯开辟新增长极
查看>>
Opera已确认解散iOS开发团队
查看>>
DevOps:新的业务浪潮
查看>>
CERT:启用EMET的Windows 7比Windows 10更加安全
查看>>
LINE上市:一场迟到、勇敢又无奈的IPO
查看>>
OA选型:OA系统工作流是核心
查看>>
如何发现“利用DNS放大攻击”的服务器
查看>>
《Arduino开发实战指南:LabVIEW卷》——第2章 Arduino软件
查看>>
京津冀大数据走廊起笔谋篇
查看>>