6

I am trying to combine a few modules, created by pybind11 and unfortunately can't get it to work. Hopefully someone can help out. I have tried to simplify the problem as much as possible.

Am trying to create the following two modules:

  • point: can be called directly
  • line: can be called directly and also uses point module.

point.h:

#ifndef UNTITLED1_POINT_H
#define UNTITLED1_POINT_H

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

namespace py = pybind11;

class Point {

private:
    double m_x;
    double m_y;
    double m_z;

public:
    Point()= default;
    Point(double x, double y, double z);
};

PYBIND11_MODULE(point, m) {

    py::class_<Point>(m, "Point")
            .def(py::init<double, double, double>());

}

#endif //UNTITLED1_POINT_H

point.cpp:

#include "point.h"
Point::Point (double x, double y, double z){
    m_x = x;
    m_y = y;
    m_z = z;
}

line.h:

#ifndef UNTITLED1_LINE_H
#define UNTITLED1_LINE_H

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

namespace py = pybind11;

#include "point.h"

class Line {
private:
    Point m_p1;
    Point m_p2;
public:
    Line(Point p1, Point p2);
};

PYBIND11_MODULE(line, m) {

    py::class_<Line>(m, "line")
            .def(py::init<Point, Point>());

}

#endif //UNTITLED1_LINE_H

line.cpp:

#include "line.h"

Line::Line(Point p1, Point p2) {
    m_p1 = p1;
    m_p2 = p2;
}

CMakeLists.txt:

cmake_minimum_required(VERSION 3.0)
project(untitled1)

set(CMAKE_CXX_STANDARD 11)

add_subdirectory(pybind11)

pybind11_add_module(point point.cpp)
pybind11_add_module(line line.cpp)

Now the following python code is run:

from point import point
from line import line

p1 = point(1, 2, 3)
p2 = point(3, 4, 5)

l = line(p1, p2)

leading to a undefined symbol error: Symbol not found: __ZN5pointC1Eddd

Update:

I have also tried the following lines in the cmake file: pybind11_add_module(point SHARED point.cpp) pybind11_add_module(line line.cpp) target_link_libraries(line PRIVATE point)

Update: more precise error:

Matthijs
  • 439
  • 3
  • 16
  • You ask multiple questions at once. Part of the solution process (and of asking on StackOverflow) is to isolate one problem, investigate, if needed ask it here, and then continue. So I'd propose you edit, and if needed ask two different questions. – Tom de Geus May 14 '18 at 07:42
  • Problem one: `symbol not found: __ZN5pointC1Eddd`. It seems that a member function `add` is not linked. Well... What you present is not sufficient to judge how to solve this. But probably either include `point.cpp` in the compilation, or make it header only by making the function `inline`. – Tom de Geus May 14 '18 at 07:45
  • Problem two: It seems here that you are making two modules that are completely independent from the Python side. I think that you compile them using two different `CMakeLists.txt` – Tom de Geus May 14 '18 at 07:46
  • 2
    "You ask multiple questions at once. " I have tried to isolate the problem as much as possible and was not aware that i am dealing with two separate issues. when i had the modules line and point separately, everything works as expected, it only breaks when the dependency between the two modules is introduced (using point in the line constructor). – Matthijs May 14 '18 at 08:39
  • will gladly update/clarify the question, but i don't really know what is unclear. – Matthijs May 14 '18 at 08:46
  • "I think that you compile them using two different CMakeLists.txt" no, i use only the described CMake file – Matthijs May 14 '18 at 08:46
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/170999/discussion-between-tom-de-geus-and-matthijs). – Tom de Geus May 14 '18 at 11:35
  • was there ever a resolution – rchav9 Dec 05 '18 at 03:57
  • @rchav9, no i didn't have anymore time look into this issue. Instead i switched to Cython – Matthijs Dec 05 '18 at 11:59

1 Answers1

2

Add the PYBIND11_EXPORT macro to all types you want to use from other modules (see documentation).

e.g.

class PYBIND11_EXPORT Point {
  ...
}
thomas
  • 21
  • 2