Compiling a C shared library with setup.py distutils when the library depends on a second shared library

I am on OSX trying to compile a common C library with setup.py distutils (for use in python using ctypes). I'm new to distutils, but I'm having problems when the shared library I want to compile (libreboundx.so) depends on another shared library (librebound.so). Explicitly, in the edit_orbits_direct.c file, I have

#include "rebound.h" 

rebound.h is located in the / Users / dt / rebound / src / directory, and all functions in the rebound.h file are located in the librebound.so shared library, which is located in / Users / dt / rebound /.

Communication with cc will look.

 cc -fPIC -shared reboundx.o -L/Users/dt/rebound -lrebound -o libreboundx.so 

UPDATE . This situation looks exactly like the example at the end of the section. 3 at https://docs.python.org/2/extending/building.html . I updated my setup.py to simulate this:

 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'], ) 

This works great on startup.

 pip install -e ./ 

Create output:

 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 

but when i try

 import reboundx 

in Python, I get OSError: dlopen (libreboundx.so, 10): Symbol not found: _reb_boundary_particle_is_in_box, which is a function in another library (librebound.so) that is not even called in the code for libreboundx.so.

If I link the shared library to the cc command above, everything works, and I can use the libreboundx.so shared library perfectly in C. If I try to take the same libreboundx.so, I compile the cc command and paste it where setup will be installed .py and then try to import reboundx in python, instead I get

 OSError: dlopen(/Users/dtamayo/Documents/workspace/reboundx/reboundx/../libreboundx.so, 10): Library not loaded: librebound.so 

Link: /Users/dtamayo/Documents/workspace/reboundx/libreboundx.so Reason: image not found

Could this be a rpath problem where at runtime libreboundx.so doesn't know where to look for librebound.so?

+5
source share
1 answer

Thanks for all the suggestions. I had to indicate in the question that in the end I want a solution that I could pack for upload to PyPy so that users can install one command. It should also work on both OSX and Linux, so I preferred solutions not related to install_name_tool.

I was not able to verify it, but I think adding

  runtime_library_dirs=['/Users/dt/rebound'], 

next to library_dirs should fix the problem on Linux. This does not seem to work on Mac, but you can use extra_link_args instead. By adding this below the libreboundxmodule definition above,

 if platform.system() == 'Darwin': extra_link_args.append('-Wl,-rpath,'+'/Users/dtamayo/Documents/workspace/rebound') 

fixed my problem. I found the answer here: Python runtime_library_dirs not working on Mac

+1
source

All Articles