[英]Compiling C shared library with distutils' setup.py, when the library depends on a second shared library
我在OSX上,嘗試使用distutils的setup.py在C中編譯共享庫(在python中使用ctypes)。 我是distutils的新手,但是當我想編譯的共享庫(libreboundx.so)依賴於另一個共享庫(librebound.so)時,我遇到了問題。 顯然,在modify_orbits_direct.c中我有
#include "rebound.h"
rebound.h位於目錄/ Users / dt / rebound / src /中,並且rebound.h中的所有函數都位於共享庫librebound.so中,該庫位於/ Users / dt / rebound /中。
與cc的鏈接看起來像。
cc -fPIC -shared reboundx.o -L/Users/dt/rebound -lrebound -o libreboundx.so
更新 :這種情況看起來與Sec末尾的示例完全相同。 3在https://docs.python.org/2/extending/building.html 。 我已經更新了我的setup.py來模仿那個:
libreboundxmodule = Extension('libreboundx',
sources = [ 'src/reboundx.c',
'src/modify_orbits_direct.c'],
include_dirs = ['src', '/Users/dt/rebound/src'],
extra_compile_args=['-fstrict-aliasing', '-O3','-std=c99','-march=native', '-D_GNU_SOURCE', '-fPIC'],
library_dirs=['/Users/dt/rebound'],
libraries=['rebound'],
)
我跑的時安裝得很好
pip install -e ./
構建輸出:
You are using pip version 7.0.3, however version 7.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Obtaining file:///Users/dtamayo/Documents/workspace/reboundx
Installing collected packages: reboundx
Running setup.py develop for reboundx
Successfully installed reboundx-1.0
但是當我嘗試
import reboundx
在Python中,我得到一個OSError:dlopen(libreboundx.so,10):找不到符號:_reb_boundary_particle_is_in_box,這是另一個庫(librebound.so)中的一個函數,甚至在libreboundx的代碼中都沒有調用它。所以。
如果我使用上面的cc命令鏈接共享庫,一切正常,我可以在C中完全使用共享庫libreboundx.so。如果我嘗試使用相同的libreboundx.so我使用cc命令編譯並將其粘貼在哪里setup.py會把它,然后嘗試在python中導入reboundx,我反而得到
OSError: dlopen(/Users/dtamayo/Documents/workspace/reboundx/reboundx/../libreboundx.so, 10): Library not loaded: librebound.so
參考自:/Users/dtamayo/Documents/workspace/reboundx/libreboundx.so原因:圖片未找到
這可能就像一個rpath問題,在運行時libreboundx.so不知道在哪里尋找librebound.so?
感謝所有的建議。 我應該在問題中指出,我最終想要一個可以打包上傳到PyPy的解決方案,這樣用戶就可以使用單個命令進行安裝。 它也應該在OSX和Linux上運行,所以我更喜歡不涉及install_name_tool的解決方案。
我一直無法測試它,但我想補充一下
runtime_library_dirs=['/Users/dt/rebound'],
在library_dirs旁邊應該解決Linux上的問題。 顯然這不適用於Mac,但您可以使用extra_link_args。 在上面發布的libreboundxmodule定義下面添加這個,
if platform.system() == 'Darwin':
extra_link_args.append('-Wl,-rpath,'+'/Users/dtamayo/Documents/workspace/rebound')
解決了我的問題。 我在這里找到了答案: Python runtime_library_dirs在Mac上不起作用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.