Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Platform independent way to find path to libpython (e.g., for use in cmake)

Tags:

python

cmake

Is there any platform independent way to get the path to a python installation's libpython, for use as a cmake argument? sysconfig.get_config_var gives some pieces but there's no consistent way I can get this working.

On OSX:

  • No variable contains the actual basename of the library (libpython2.7.dylib)
  • sysconfig.get_config_var('LIBDIR') returns the directory libpython2.7.dylib is in (/usr/local/opt/python/Frameworks/Python.framework/Versions/2.7/lib)
  • edit: There seems to be a bit of a discrepancy in when sysconfig is reporting a symlink and when it's reporting a real path. LIBDIR does in fact contain libpython.2.7.dylib, but what I just noticed is that this is a symlink to /usr/local/Cellar/python/2.7.13_1/Frameworks/Python.framework/Versions/2.7/Python.

    INSTSONAME points to Python.framework/Versions/2.7/Python, the basename of that path, but no variable I can find points to the parent part of that path.

On Ubuntu:

  • sysconfig.get_config_var('INSTSONAME') gives me the name of the library (libpython2.7.so.1.0).
  • No variable contains the directory it's in (/usr/lib/x86_64-linux-gnu/).
    • LIBDIR returns only /usr/lib
like image 671
alecbz Avatar asked Sep 23 '17 22:09

alecbz


1 Answers

CMake python modules: FindPythonLibs and FindPythonInterp.

If those don't work for you just set the next vars when calling cmake -DPYTHON_EXECUTABLE:FILEPATH, -DPYTHON_LIBRARY:FILEPATH and -DPYTHON_INCLUDE_DIR:FILEPAT, for more info look here.

Example:

cmake .. -G "Sublime Text 2 - Ninja"
-DCMAKE_BUILD_TYPE=Release
-DPYTHON_EXECUTABLE:FILEPATH=d:\virtualenvs\python362_32
-DPYTHON_LIBRARY:FILEPATH=d:\virtualenvs\python362_32\libs\python36.lib
-DPYTHON_INCLUDE_DIR:FILEPAT=d:\virtualenvs\python362_32\include
like image 198
BPL Avatar answered Sep 30 '22 21:09

BPL