python动态加载so文件
python动态加载so文件
active_health 发表于8个月前
python动态加载so文件
  • 发表于 8个月前
  • 阅读 32
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

解决方案1:

#!/usr/bin/env python

import os,sys,time,re
from 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 os
os.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.

共有 人打赏支持
粉丝 0
博文 100
码字总数 68865
×
active_health
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: