How to configure setup.py to link libraries?


#1

Hi!
I am about to build my own C++ extension for Pytorch :slight_smile:
However I have now written a .cpp file which requires a dynamic library. How do I properly modify the setup.py file, so that it is being found (at runtime) when the extension is loaded?

(I use macOS if that information would be needed.)

My tree look like:

├── libraries
│   ├── cubic_spline.h
│   └── libcubic_spline.dylib
├── setup.py
├── splitFrames.cpp

My setup.py file looks like this:

from setuptools import setup
from torch.utils.cpp_extension import CppExtension, BuildExtension

setup(
    name='split_frames',
    ext_modules=[
        CppExtension(
            name='split_frames',
            sources=['splitFrames.cpp'],
            extra_compile_args=['-g', '-stdlib=libc++', '-std=c++11']),
    ],
    cmdclass = {
        'build_ext': BuildExtension
    })


# call: $ CC=clang CXX=clang++ NO_CUDA=1 python setup.py install

Cheers,
Lucas


#2

Okay in the meantime I got something working for static libraries (might also work for dynamic libraries as well). In case it is helpful for someone.

from setuptools import setup
import sys
from distutils.core import setup, Extension
from torch.utils.cpp_extension import CppExtension, BuildExtension

static_libraries = ['cubic_spline']
static_lib_dir = 'lib'
libraries = ['']
library_dirs = ['']

if sys.platform == 'win32':
    libraries.extend(static_libraries)
    library_dirs.append(static_lib_dir)
    extra_objects = []
else:  # POSIX
    extra_objects = ['{}/lib{}.a'.format(static_lib_dir, l) for l in static_libraries]

setup(
    name='torch_cpp',
    ext_modules=[
        CppExtension(
            name='torch_cpp',
            sources=['pybind_wrapper.cpp', 'torch_boost.cpp', 'torch_cubicspline.cpp'],
            extra_compile_args=['-g', '-stdlib=libc++', '-std=c++11'],
            extra_objects=extra_objects)

        ],
    cmdclass={
        'build_ext': BuildExtension
    })


# call: $ CC=clang CXX=clang++ NO_CUDA=1 python setup.py install