Changing LD_LIBRARY_PATH at runtime for ctypes

后端 未结 5 1969
南笙
南笙 2020-11-27 15:14

How do you update this environment variable at runtime so that ctypes can load a library wherever? I\'ve tried the following and neither seem to work.

from c         


        
5条回答
  •  青春惊慌失措
    2020-11-27 15:27

    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

提交回复
热议问题