I'm a newbie to distutils and I have a problem that really has me stuck. I am compiling a package that requires an extension, so I make the extension thus:
a_module = Extension(
"amodule",
["initmodule.cpp"],
library_dirs=libdirs,
extra_objects = [
"unix/x86_64/lib/liba.so"
"unix/x86_64/lib/lib.so",
"unix/x86_64/lib/libc.so"],
)
I then run the setup method:
setup(name="apackage", version="7.2",
package_dir = {'':instdir+'/a/b/python'},
packages=['apackage','package.tests'],
ext_modules=[hoc_module]
)
The package distribution is made properly and I can "python setup.py install" fine but when I try and import my package I get an error
ImportError: liba.so.0: cannot open shared object file: No such file or directory
I realise that when I add the location of liba.so.0 to my LD_LIBRARY_PATH the program runs fine. Unfortunately I haven't written these modules and don't have a good understanding of compilation. I've been trying to figure this out for several days to no avail.
UPDATE:I tried passing the liba.a, libb.a etc files to extra_objects but this didn't work, generating the following errror: liba.a: could not read symbols: Bad value collect2: ld returned 1 exit status. What I'm trying to do is package a python module which requires a library to be compiled which itself depends on other libraries which I need to somehow include in the package .I suspect that my problem is very similar to this one: http://mail.python.org/pipermail/distutils-sig/2009-February/010960.html but that one was not resolved, I thought perhaps since it's two years old a resolution has been found?
UPDATE 2: For now I have solved this by doing:
data_files=[('/usr/local/lib', glob.glob('unix/x86_64/lib/*'))]
That is to say, I am copying the libraries I need into /usr/local/lib. I'm not hugely happy with this solution however, not least because it requires my users to have root privileges and also because this may still not work Redhat distros. So if anyone can suggest something better than this fix do please let me know.
You can have the linker store paths to search in the output binary so LD_LIBRARY_PATH isn't necessary. Some examples:
# Will link fine but at run-time LD_LIBRARY_PATH would be required
gcc -o blah blah.o -lpcap -L/opt/csw/lib
# Without LD_LIBRARY_PATH=/opt/csw/lib it will fail to link, but
# it wouldn't be needed at run-time
gcc -o blah blah.o -lpcap -Wl,-R/opt/csw/lib
# LD_LIBRARY_PATH not needed at link or run-time
gcc -o blah blah.o -lpcap -Wl,-{L,R}/opt/csw/lib
# This makes it possible to use relative paths; run `readelf -d binary_name`
# and you'll see '$ORIGIN/../lib/' in RPATH. This plus `-zorigin` make it look
# relative to the binary for libraries at run-time
gcc -o blah blah.o -lsomelib -L/whatever/path/floats/your/boat -Wl,-R'$ORIGIN/../lib/' -Wl,-zorigin
.. where:
-L
are used at link-time
-R
are used at run-time
The extra_objects
argument to the Extension
class is not so much a list of libraries to link into your extension, but a list of object files that will be passed to the linker (and the filenames shouldn't include extensions, since distutils will add those.) It doesn't do what you seem to want.
If you want to link against specific shared libraries, as the names of those files suggest you want, you have to do two things: tell distutils to tell the compiler to link against those shared libraries, and tell the dynamic linker (usually ld.so
) where to find those shared libraries. You can tell distutils to tell the compiler to link against the libraries by using the libraries
argument to Extension
, which should be a list of library names (without the lib
prefix and .so
suffix.) In your example that seems to be ['a', 'b', 'c']
(although it looks like the 'b'
fell off of 'lib.so
', and 'c'
would actually clash with the system libc.)
Telling the linker where to find these shared libraries can be done by setting the LD_LIBRARY_PATH
environment variable, as you did, or by changing a system-wide configuration setting (with ldconfig
or by editing /etc/ld.so.conf
), or by hardcoding the search path in the extension module; you can do the latter by passing the runtime_library_dirs
argument to Extension
. Hardcoding the path does have its own issues, though -- you have to keep those libraries in the same place, and accessible to all users of the extension module.
(Alternatively, you can use static instead of dynamic linking, for example by only providing the libraries in static form, liba.a
archives (in which case distutils will automatically link to them statically.) That basically means the whole library is included in the extension module, which has various downsides and upsides.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With