2

I have a package I've created in C++ and, have already compiled it into a shared library.

When I link against it with my own main function, I can initialize the package by calling the initialization function directly, initfoo, and everything works fine.

How do I get python to recognize my shared library as a package, so I can just type:

import foo

running from the regular python interpreter?

I'm not interested in using distutils to compile the file, since the compilation has to be part of the regular cmake build system. I just need to create whatever package files necessary to load my shared library.

Update: I now have it working. The issue was with cmake defaulting to a lib prefix for shared library names. To fix this requires

SET_TARGET_PROPERTIES(foo PROPERTIES PREFIX "")

and for Mac OS X

SET_TARGET_PROPERTIES(foo PROPERTIES SUFFIX ".so")
Johan Råde
  • 20,480
  • 21
  • 73
  • 110
Juan
  • 3,667
  • 3
  • 28
  • 32
  • `distutils` exists specifically because it can a huge hassle to do by yourself (see http://python-history.blogspot.com/2009/03/dynamically-loaded-modules.html) - is building it with `distutils` (possibly twice, if it's also needed in the cmake build) completely out of question? –  Jun 23 '11 at 16:31
  • It is out of the question. My commands invoke a lot of C++ functions. In addition, on Mac OS X, the resulting shared library must link against all of my other shared libraries. – Juan Jun 23 '11 at 16:41

1 Answers1

2

If you have a shared library with initfoo symbol exported (note: since you've tagged the question — make sure it's an extern "C" symbol, otherwise name mangling will prevent the interpreter from finding it), then it's already a Python module, and can be loaded directly, with no further work. You only need to make sure it's on search path, just like any other module.

Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
  • Thank you. I set the PYTHONPATH variable to $PWD and verified that the "initfoo" function is unmangled, and in the symbol table. It turns out that the .so file name must be foo.so, and not libfoo.so. Thanks. – Juan Jun 23 '11 at 16:42