pybind / pybind/pybind11

connected pybind11 modules

Open
#1,391 5 comments 9 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

I think my problem is not necessarily a bug, but asking before on SO, gitter and an old related issue, didn't lead to solutions which solved the issue. Futhermore i think this use case might come up more frequently and can thus be of use for others if documented.

Issue description

Am trying to create and bind the following two modules with pybind11:

  • point: can be called directly
  • line: can be called directly and also uses point module.
    Trying to use use the line module leads to an undefined symbol error:
    Symbol not found: __ZN5pointC1Eddd

Reproducible example code

The original setup is as follows:

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

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)

Also tried adding different linking operations:
py::module::import("point");
py::object point = (py::object) py::module::import("point");
py::module point = py::module::import("point");

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with issue #1391 and reproduce the two-module example using point.h, point.cpp, line.h, line.cpp, and CMakeLists.txt. Review the existing pybind11 documentation for module builds and linking, then document the supported setup and verify it with the provided Python import sequence and undefined-symbol case.

Written by the indexing model from the issue text.

Assessment

Tech stack
cmake, cpp, python
Domain
build-system, documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.