如何使用pybind11和cmake链接2个模块

时间:2018-05-09 16:03:15

标签: c++ cmake shared-libraries pybind11

我正在尝试组合一些由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

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)

现在运行以下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)

更新: 更准确的错误:

1 个答案:

答案 0 :(得分:0)

public class MainActivity extends AppCompatActivity { IncomingHandler msgHandler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); msgHandler = new IncomingHandler(MainActivity.this); FloatingActionButton fab = findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Handler Started", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); initHandler(); } }); } private void initHandler() { new Thread(new Runnable() { @Override public void run() { try{ Message msg = msgHandler.obtainMessage(); Bundle bundle = new Bundle(); bundle.putString("MSG_KEY", "Executed in background thread"); msg.setData(bundle); msgHandler.sendMessage(msg); }catch(Exception ex){ ex.printStackTrace(); Toast.makeText(MainActivity.this,ex.getMessage(),Toast.LENGTH_LONG).show(); } } }).start(); } private static class IncomingHandler extends Handler{ Handler mHandler = new Handler(); Context context; public IncomingHandler(Context context) { this.context = context; } @Override public void handleMessage(@NonNull final Message msg) { super.handleMessage(msg); Bundle bundle = msg.getData(); final String displayMessage = bundle.getString("MSG_KEY"); mHandler.post(new Runnable() { @Override public void run() { Toast.makeText(context ,displayMessage,Toast.LENGTH_LONG).show(); } }); } } } 宏添加到要从其他模块(RandomAccessFile docs)使用的所有类型中。

例如

PYBIND11_EXPORT