Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Building a Python extension with a local C++ library dependency using setuptools and build

I have a library written in C++ for which I would like to make a Python wrapper. The directory structure of my repository is similar to the following:

.
├── include # C++ library headers
│  └── ...
├── Makefile
├── python # Python package
│ ├── wrapper-module # C++ and Python source files
│ │ └── ...
│ ├── pyproject.toml
│ ├── setup.py
│  └── ...
├── src # C++ library source files
│  └── ...
└── tests # C++ library tests
  └── ...

I use GNU make for building the C++ library and use Python’s C API directly to implement the wrapper. (I.e. no Boost.Python, Cython etc.)

Building the Python package has been unsuccessful so far. Since I use the C API, I chose setuptools 80.9.0 for building the module. When running python -m build, the relevant files are copied to a separate location, even if I use --no-isolation. So far I have been unable to make the C++ library available in that location or determine their relative path with respect to that location. (I would like to avoid specifying its absolute path or installing it to e.g. /usr.) The source files for the wrapper module (or at least most of them) are copied as expected.

So far I have attempted to solve the issue as follows:

  • Checking the documentation. What I have found so far has had to do with Python dependencies, not "local" ones.
  • Copying the C++ library build products to the Python directory with a build phase in the Makefile. In this case the build products are not copied to the separate build location. I have not found a configuration option to specify the directory in question as a build dependency.
  • Adding a custom build phase in setup.py. In this case I have not found a function or property to retrieve the original path in order to copy the required files as part of the build process.
  • Writing a custom build backend based on setuptools.build_meta. In this case I can determine the original path using os.getcwd(). However, the module that contains the custom backend is not copied to the separate build location even though I have set build-backend and backend-path in pyproject.toml. (In any case I probably would need to store the original path somehow, to which I have not paid much attention.)

Any suggestions for making setuptools aware of the location of the built C++ library and the associated header files are appreciated.

Answer*

Draft saved
Draft discarded
Cancel
1
  • 1
    I tried to explain why this does not work in the question: the Python package files are copied to a separate build location and hence the relative path will not work. I would like to build the C++ library separately since it does not depend on the Python library. Commented Sep 24, 2025 at 12:18

lang-py

AltStyle によって変換されたページ (->オリジナル) /