4

when I run pip install . in the directory where I have my setup.py. I'm using pybind11 to build a python module for my C++ project. (also, on windows 10)

I get this error: https://pastebin.com/xGyFQQk2

Here is the module.cpp code:

Environment pyInitEnvironment(const py::list& pyList, const unsigned int allowedLayers, const unsigned int padding, const unsigned int maxTracersPerGroup) {
    return Environment(listToContactVertexGroup(pyList), allowedLayers, padding, maxTracersPerGroup);
}

PYBIND11_MODULE(route_search_superfast, m) {
    py::class_<Environment>(m, "Environment")
        .def(py::init(&pyInitEnvironment));

    py::class_<RandomModel>(m, "RandomModel");
}

And here is my setup.py:

import os, sys

from distutils.core import setup, Extension
from distutils import sysconfig

cpp_args = ['-std=c++11', '-stdlib=libc++', '-mmacosx-version-min=10.7']

rss_module = Extension(
    'route_search_superfast', 
    sources=['module.cpp'],
    include_dirs=['pybind11/include'],
    language='c++',
    extra_compile_args = cpp_args,
)

setup(
    name = 'route_search_superfast',
    version = '1.0',
    description = 'Python package with superfastcode2 C++ extension (PyBind11)',
    ext_modules = [rss_module],
)

The only thing that doesn't want to build is my Environment.h & Environment.cpp files. They're quite long, but I can assure you that Visual Studio does build them. Everything works perfectly fine. I can even generate the output .pyd file and import that into python and it will run everything. However, when I try to have the setup script build the C++ project, it throws that console stack (again, found here: https://pastebin.com/xGyFQQk2)

Since the .pyd file works (again, I'm able to import this into python and everything works perfect) is there a way that I can use distutils to simply use the .pyd when installing the package locally?

Dyllan M
  • 301
  • 2
  • 15
  • 1
    When building module.cpp, it needs to link with the external lib that contains the Environment code. If you examine the command line arguments from the snipped you'll see that it's missing the library that contains Environment Try adding these two lines to the rss_module definition ` libraries = ['...'], library_dirs = ['...'] ` – Eyal D Jan 23 '20 at 10:31
  • @EyalD Even if the Environment.h file is within the same directory as the setup file? – Dyllan M Jan 25 '20 at 01:38
  • @EyalD That worked for compilation, however now I'm getting "Segmentation Fault (core dumped)" when including the module. – Dyllan M Jan 25 '20 at 01:44
  • C++ compilation involves three steps. In the first one preprocessing, #include macros are handled. In this step, the compiler will look for header files in the include_dirs. In the second part, the compilation, object files are created, and in the third part (linking) they are linked together – Eyal D Jan 26 '20 at 08:59
  • In case you are using external libraries, you will need to explicitly specify the file names (libraries in the setup.py) and the library dirs (library_dirs ) – Eyal D Jan 26 '20 at 09:05

1 Answers1

0

C++ compilation involves three steps. In the first one preprocessing, #include macros are handled. In this step, the compiler will look for header files in the include_dirs. In the second part, the compilation, object files are created, and in the third part (linking) they are linked together. See here for more details.

When building module.cpp, you have to explicitly specify the library files and their directories. If you examine the command line arguments from your snippet you'll see that it's missing the library that contains Environment.

Try adding these two lines to the rss_module definition

libraries = ['...'], library_dirs = ['...']

See this tutorial, which shows a similar example on how to link an external library to an application https://learn.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp?view=vs-2019

Eyal D
  • 169
  • 1
  • 15