我正在尝试组合由 pybind11 创建的几个模块,但遗憾的是无法使其正常工作。希望有人可以帮忙。我试图尽可能地简化问题。
我正在尝试创建以下两个模块:
point
: 可以直接调用line
: 可以直接调用,也可以使用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
点.cpp:
#include "point.h"
Point::Point (double x, double y, double z){
m_x = x;
m_y = y;
m_z = z;
}
线.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
线.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)
现在运行以下python代码:
from point import point
from line import line
p1 = point(1, 2, 3)
p2 = point(3, 4, 5)
l = line(p1, p2)
导致未定义的符号错误:
Symbol not found: __ZN5pointC1Eddd
更新:
我还在 cmake 文件中尝试了以下几行:
pybind11_add_module(point SHARED point.cpp)
pybind11_add_module(line line.cpp)
target_link_libraries(line PRIVATE point)
更新:更精确的错误: