boost-python

Passing a Python class instance into a C++ function

假如想象 提交于 2019-12-09 18:22:12
问题 I have an in-house library written in C++ that I'm currently working on extending into Python. I started this task with Boost.Python in mind, but I'm open to alternatives. Currently I have a C++ function that needs to accept a Python class instance, and then use the methods of this object to perform certain tasks. The idea is for the Python user to never need to deal with C++. They're expected to create this Python object from a Python template/example class that I will provide, with preset

How does import work with Boost.Python from inside python files

点点圈 提交于 2019-12-09 16:43:06
问题 I am using Boost.Python to embed an interpreter in my C++ executable and execute some prewritten scripts. I have got it working so that I can call functions in the python file but the python code I want to use imports external files and these imports fail because 'no module named '. If I run the script directly from python everything works as expected however. So my question is what is the correct way of importing modules in python scripts that are being run via C++ bindings? C++ Code:

boost::python: howto call a function that expects a pointer?

梦想的初衷 提交于 2019-12-09 12:43:46
问题 I have a function that takes an int-pointer and exposed it via boost::python. How can I call this function from python? in C++ with boost::python: void foo(int* i); ... def("foo", foo); in python: import foo_ext i = 12 foo_ext.foo(i) results in Traceback (most recent call last): File "<stdin>", line 1, in <module> Boost.Python.ArgumentError: Python argument types in foo(int) did not match C++ signature: foo(int* i) So how to pass a pointer? 回答1: Short answer is : You can't. Python does not

Is wrapping C++ library with ctypes a bad idea?

馋奶兔 提交于 2019-12-08 23:42:24
问题 I read through the following two threads on wrapping C library and C++ library, I am not sure I get it yet. The C++ library I am working with does use class and template, but not in any over-sophisticated way. What are issues or caveats of wrapping it with ctypes (besides the point that you can do so in pure python etc)? PyCXX , Cython and boost::python are three other choices people mentioned, is there any consensus which one is more suitable for C++? Thanks Oliver 回答1: For C++ a library to

How to link with Python3 Libs with cmake?

≯℡__Kan透↙ 提交于 2019-12-08 19:09:02
问题 I have Python3 installed via brew install python3 . However, cmake cannot find PythonLibs 3. Here's the header of my CMakeLists.txt . cmake_minimum_required(VERSION 3.0) find_package(PythonLibs 3 REQUIRED) When I ran cmake, I got this error message Could NOT find PythonLibs: Found unsuitable version "2.7.6", but required is at least "3" (found /usr/lib/libpython2.7.dylib) Not sure what I did wrong. 回答1: In my experience, this happened because I was using an older version of cmake (2.8 instead

Boost.Python and Qt inheritance

允我心安 提交于 2019-12-08 18:24:36
I'm want to wrap my C++ code wrote with Qt framework into python with boost.python. But I really can't understand one issue : when I wrapping class, I should also wrap it's base class. So should I wrap QObject, for example, if class which I need to wrap inherits from it? e.g. : class TRIKCONTROL_EXPORT BatteryInterface : public QObject, public DeviceInterface { Q_OBJECT public slots: /// Returns current battery voltage in volts. virtual float readVoltage() = 0; /// Returns current raw reading of battery. virtual float readRawDataVoltage() = 0; }; UPD 1 : Now I'm pretty sure that I don't need

Running 2 Python scripts concurrently with Boost Python embedded in C++

倖福魔咒の 提交于 2019-12-08 09:10:09
问题 I embedded Python in C++ using Boost Python. I wanted to run 2 Python scripts concurrently. The scripts should also have the opportunity to access C++ member functions. Well, when I use just 1 thread in the main interpreter it can use the member functions. But to run 2 scripts concurrently I create a new interpreter for each thread (http://docs.python.org/2/c-api/init.html#sub-interpreter-support). So scripts can be executed concurrently but they are not able to use the member functions. This

How to expose aligned class with boost.python

一曲冷凌霜 提交于 2019-12-08 08:57:37
问题 When trying to expose aligned class like this: class __declspec(align(16)) foo { public: void foo_method() {} }; BOOST_PYTHON_MODULE(foo_module) { class_<foo>("foo") .def("foo_method", &foo::foo_method); } You end up with error (msvs 2010): error C2719: 'unnamed-parameter': formal parameter with __declspec(align('16')) won't be aligned, see reference to class template instantiation 'boost::python::converter::as_to_python_function<T,ToPython>' being compiled The solution I found so far, is to

cannot import module using boost.python

风格不统一 提交于 2019-12-08 08:04:05
问题 I'm trying to build a simple program using boost.python. I have the following code: //greet.cpp #include <iostream> #include <boost/python.hpp> void greet() { std::cout << "hello world!" << std::endl; } BOOST_PYTHON_MODULE(greet) { using namespace boost::python; def("greet", greet); } and the follwing makefile : PYTHON_VERSION := 2.7 PYTHON_INC := /usr/include/python$(PYTHON_VERSION) PYTHON_LIB_LOCATION := /usr/lib/python${PYTHON_VERSION} PYTHON_LIB_FILE := python${PYTHON_VERSION} BOOST_INC :

Boost.Python and Qt inheritance

情到浓时终转凉″ 提交于 2019-12-08 06:35:55
问题 I'm want to wrap my C++ code wrote with Qt framework into python with boost.python. But I really can't understand one issue : when I wrapping class, I should also wrap it's base class. So should I wrap QObject, for example, if class which I need to wrap inherits from it? e.g. : class TRIKCONTROL_EXPORT BatteryInterface : public QObject, public DeviceInterface { Q_OBJECT public slots: /// Returns current battery voltage in volts. virtual float readVoltage() = 0; /// Returns current raw reading