如何在c ++应用程序和python脚本之间共享对象

时间:2016-08-21 16:19:23

标签: python c++ python-2.7 boost

我想扩展一个c ++应用程序,以便:

  • C ++对象可用于python
  • C ++应用程序运行python脚本
  • python能够操作C ++对象

我一直在尝试使用Boost.Python并使用以下测试代码:

这是Dog.hh

// Dog.hh
class dog
{
    int legs;
    int age;
public:
    void wag();
    void bark();
};

这是Dog.cc

 // Dog.cc
#include <boost/python.hpp>
#include <iostream>
#include "Dog.hh"

void
Dog::wag()
{
    std::cout << "wag, wag, wag" << std::endl;
}

void
Dog::bark()
{
    std::cout << "bark, woof" << std::endl;
}

BOOST_PYTHON_MODULE(libpydog)
{
    boost::python::class_<Dog>("Dog")
        .def("wag", &Dog::wag)
        .def("bark", &Dog::bark)
        ;
};

这是python-integration.cc驱动程序

// python-integration.cc

#include <boost/python.hpp>
#include <iostream>
#include <exception>
#include "Dog.hh"

namespace python = boost::python;

void
run_it()
{
    std::string a_file = "py-test-dog.py";

    python::object main_module  = python::import("__main__");
    std::cout << "Imported main module" << std::endl;

    python::object global(main_module.attr("__dict__"));
    std::cout << "Created global environment" << std::endl;

    try
    {
        python::object ignored      = python::exec_file(
            a_file.c_str() , global, global );
        std::cout << "Executed script" << std::endl;
    }
    catch(std::exception& e)
    {
        std::cout << e.what() << std::endl;
    }

}



int main(int argc, char *argv[])
{
    std::cout << "Running the script"
              << " with python interpreter"
              << std::endl;

    Py_Initialize();
    std::cout << "Initialized the python interpreter"
              << std::endl;

    if ( python::handle_exception(run_it) )
    {
        if (PyErr_Occurred())
        {
            std::cout << "Python Error detected"
                      << std::endl;
            PyErr_Print();
        }
        else
        {
            std::cout << "A C++ exception was thrown  for which "
                      << "there was no exception translator registered."
                      << std::endl;
        }
    }

    return 0;
}

最后,这是我用来编译的cmake文件:

# CMakeLists.txt
#   -*- cmake -*-


cmake_minimum_required(VERSION 2.8)

project(python_test CXX)

set(CMAKE_BUILD_DIR "build")

option(ENABLE_CLANG "build application with clang" ON)

if(ENABLE_CLANG)
  set(CMAKE_CXX_COMPILER           "/usr/bin/clang++")
  set(CMAKE_AR                     "/usr/bin/llvm-ar")
  set(CMAKE_LINKER                 "/usr/bin/llvm-ld")
  set(CMAKE_NM                     "/usr/bin/llvm-nm")
  set(CMAKE_OBJDUMP                "/usr/bin/llvm-objdump")
  set(CMAKE_RANLIB                 "/usr/bin/llvm-ranlib")
else()
  set(CMAKE_CXX_COMPILER           "/usr/bin/g++")
endif()

set(CMAKE_CXX_FLAGS       "${CMAKE_CXX_FLAGS} -std=c++11 -Wall")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -g")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -Wextra")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -Wno-system-headers")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -Wold-style-cast")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -Woverloaded-virtual")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -Wsign-promo")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -Weffc++")



set(Boost_USE_STATIC_LIBS   OFF)
set(Boost_USE_MULTITHREADED ON)
find_package(Boost COMPONENTS
  python
  REQUIRED
  )


include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})

find_package(PythonLibs 2.7 EXACT REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
link_directories(${PYTHON_LIBRARIES})


add_library(pydog SHARED
  Dog.cc
  )

target_link_libraries( pydog
  ${PYTHON_LIBRARIES}
  ${Boost_LIBRARIES}
  )


add_executable(pytest
  Dog.cc
  python-integration.cc
  )

target_link_libraries(pytest
  ${PYTHON_LIBRARIES}
  ${Boost_LIBRARIES}
  pydog
  )
##############################################################################
## Build Summary
message(STATUS "Directories:")
message(STATUS "CMAKE_SOURCE_DIR          ${CMAKE_SOURCE_DIR}")
message(STATUS "CMAKE_BUILD_DIR           ${CMAKE_BUILD_DIR}")
message(STATUS "EXECUTABLE_OUTPUT_PATH    ${EXECUTABLE_OUTPUT_PATH}")

message(STATUS "Compiler Options")
message(STATUS "CMAKE_CXX_COMPILER   ${CMAKE_CXX_COMPILER}")
message(STATUS "CXX_COMPILER         ${CXX_COMPILER}")
message(STATUS "CMAKE_CXX_FLAGS      ${CMAKE_CXX_FLAGS}")
message(STATUS "INCLUDE_DIRECTORIES  ${INCLUDE_DIRECTORIES}")

这是我用来测试的python脚本:

#!/usr/bin/env python2.7

import libpydog

d = libpydog.Dog()
print("Wagging the dog")
d.wag()
print("Barking the dog")
d.bark()

编译完成且没有错误。如果我运行python脚本,我得到:

./py-test-dog.py
Wagging the dog
wag, wag, wag
Barking the dog
bark, woof

我知道这篇文章有很多代码,我道歉,我只想彻底。

我想在运行c ++应用程序时看到相同的输出,但是我得到了一个核心转储。我可以在这里发布它,如果它会有所帮助,但我希望这些线索在上面的代码中。

按建议添加输出:

~/repos/cpp-projects/python-integration-test/build $ ./pytest
Running the script with python interpreter
Initialized the python interpreter
Imported main module
Created global environment
*** Error in `/home/aldrichtr/repos/cpp-projects/python-integration-test/build/pytest': double free or corruption (!prev): 0x00000000014ffac0 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x77725)[0x7f3760a84725]
/lib/x86_64-linux-gnu/libc.so.6(+0x7ff4a)[0x7f3760a8cf4a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f3760a90abc]
/lib/x86_64-linux-gnu/libc.so.6(fclose+0x103)[0x7f3760a7a313]
/usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(+0x1579ad)[0x7f3761c349ad]
/usr/lib/x86_64-linux-gnu/libboost_python-py27.so.1.58.0(_ZN5boost6python9exec_fileENS0_3strENS0_3api6objectES3_+0x4cb)[0x7f37618c7bcb]
/home/aldrichtr/repos/cpp-projects/python-integration-test/build/pytest(_Z6run_itv+0x176)[0x412e46]
/home/aldrichtr/repos/cpp-projects/python-integration-test/build/pytest(_ZN5boost6detail8function26void_function_ref_invoker0IPFvvEvE6invokeERNS1_15function_bufferE+0x1d)[0x41365d]
/usr/lib/x86_64-linux-gnu/libboost_python-py27.so.1.58.0(_ZN5boost6python21handle_exception_implENS_9function0IvEE+0x73)[0x7f37618c2613]
/home/aldrichtr/repos/cpp-projects/python-integration-test/build/pytest(_ZN5boost6python16handle_exceptionIPFvvEEEbT_+0x88)[0x413348]
/home/aldrichtr/repos/cpp-projects/python-integration-test/build/pytest(main+0xa0)[0x413120]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f3760a2d830]
/home/aldrichtr/repos/cpp-projects/python-integration-test/build/pytest(_start+0x29)[0x40de29]
======= Memory map: ========
00400000-00418000 r-xp 00000000 fc:00 6163807                            /home/aldrichtr/repos/cpp-projects/python-integration-test/build/pytest
00617000-00618000 r--p 00017000 fc:00 6163807                            /home/aldrichtr/repos/cpp-projects/python-integration-test/build/pytest
00618000-00619000 rw-p 00018000 fc:00 6163807                            /home/aldrichtr/repos/cpp-projects/python-integration-test/build/pytest
01446000-0151c000 rw-p 00000000 00:00 0                                  [heap]
7f3758000000-7f3758021000 rw-p 00000000 00:00 0 
7f3758021000-7f375c000000 ---p 00000000 00:00 0 
7f375f810000-7f37601cf000 r--p 00000000 fc:00 27925285                   /usr/lib/locale/locale-archive
7f37601cf000-7f37601d1000 r-xp 00000000 fc:00 19272214                   /lib/x86_64-linux-gnu/libutil-2.23.so
7f37601d1000-7f37603d0000 ---p 00002000 fc:00 19272214                   /lib/x86_64-linux-gnu/libutil-2.23.so
7f37603d0000-7f37603d1000 r--p 00001000 fc:00 19272214                   /lib/x86_64-linux-gnu/libutil-2.23.so
7f37603d1000-7f37603d2000 rw-p 00002000 fc:00 19272214                   /lib/x86_64-linux-gnu/libutil-2.23.so
7f37603d2000-7f37603d5000 r-xp 00000000 fc:00 19272057                   /lib/x86_64-linux-gnu/libdl-2.23.so
7f37603d5000-7f37605d4000 ---p 00003000 fc:00 19272057                   /lib/x86_64-linux-gnu/libdl-2.23.so
7f37605d4000-7f37605d5000 r--p 00002000 fc:00 19272057                   /lib/x86_64-linux-gnu/libdl-2.23.so
7f37605d5000-7f37605d6000 rw-p 00003000 fc:00 19272057                   /lib/x86_64-linux-gnu/libdl-2.23.so
7f37605d6000-7f37605ef000 r-xp 00000000 fc:00 19272224                   /lib/x86_64-linux-gnu/libz.so.1.2.8
7f37605ef000-7f37607ee000 ---p 00019000 fc:00 19272224                   /lib/x86_64-linux-gnu/libz.so.1.2.8
7f37607ee000-7f37607ef000 r--p 00018000 fc:00 19272224                   /lib/x86_64-linux-gnu/libz.so.1.2.8
7f37607ef000-7f37607f0000 rw-p 00019000 fc:00 19272224                   /lib/x86_64-linux-gnu/libz.so.1.2.8
7f37607f0000-7f3760808000 r-xp 00000000 fc:00 19272179                   /lib/x86_64-linux-gnu/libpthread-2.23.so
7f3760808000-7f3760a07000 ---p 00018000 fc:00 19272179                   /lib/x86_64-linux-gnu/libpthread-2.23.so
7f3760a07000-7f3760a08000 r--p 00017000 fc:00 19272179                   /lib/x86_64-linux-gnu/libpthread-2.23.so
7f3760a08000-7f3760a09000 rw-p 00018000 fc:00 19272179                   /lib/x86_64-linux-gnu/libpthread-2.23.so
7f3760a09000-7f3760a0d000 rw-p 00000000 00:00 0 
7f3760a0d000-7f3760bcd000 r-xp 00000000 fc:00 19272033                   /lib/x86_64-linux-gnu/libc-2.23.so
7f3760bcd000-7f3760dcc000 ---p 001c0000 fc:00 19272033                   /lib/x86_64-linux-gnu/libc-2.23.so
7f3760dcc000-7f3760dd0000 r--p 001bf000 fc:00 19272033                   /lib/x86_64-linux-gnu/libc-2.23.so
7f3760dd0000-7f3760dd2000 rw-p 001c3000 fc:00 19272033                   /lib/x86_64-linux-gnu/libc-2.23.so
7f3760dd2000-7f3760dd6000 rw-p 00000000 00:00 0 
7f3760dd6000-7f3760dec000 r-xp 00000000 fc:00 19272071                   /lib/x86_64-linux-gnu/libgcc_s.so.1
7f3760dec000-7f3760feb000 ---p 00016000 fc:00 19272071                   /lib/x86_64-linux-gnu/libgcc_s.so.1
7f3760feb000-7f3760fec000 rw-p 00015000 fc:00 19272071                   /lib/x86_64-linux-gnu/libgcc_s.so.1
7f3760fec000-7f37610f4000 r-xp 00000000 fc:00 19272103                   /lib/x86_64-linux-gnu/libm-2.23.so
7f37610f4000-7f37612f3000 ---p 00108000 fc:00 19272103                   /lib/x86_64-linux-gnu/libm-2.23.so
7f37612f3000-7f37612f4000 r--p 00107000 fc:00 19272103                   /lib/x86_64-linux-gnu/libm-2.23.so
7f37612f4000-7f37612f5000 rw-p 00108000 fc:00 19272103                   /lib/x86_64-linux-gnu/libm-2.23.so
7f37612f5000-7f3761467000 r-xp 00000000 fc:00 27918734                   /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f3761467000-7f3761667000 ---p 00172000 fc:00 27918734                   /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f3761667000-7f3761671000 r--p 00172000 fc:00 27918734                   /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f3761671000-7f3761673000 rw-p 0017c000 fc:00 27918734                   /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f3761673000-7f3761677000 rw-p 00000000 00:00 0 
7f3761677000-7f376168f000 r-xp 00000000 fc:00 6163410                    /home/aldrichtr/repos/cpp-projects/python-integration-test/build/libpydog.so
7f376168f000-7f376188f000 ---p 00018000 fc:00 6163410                    /home/aldrichtr/repos/cpp-projects/python-integration-test/build/libpydog.so
7f376188f000-7f3761890000 r--p 00018000 fc:00 6163410                    /home/aldrichtr/repos/cpp-projects/python-integration-test/build/libpydog.so
7f3761890000-7f3761891000 rw-p 00019000 fc:00 6163410                    /home/aldrichtr/repos/cpp-projects/python-integration-test/build/libpydog.so
7f3761891000-7f37618da000 r-xp 00000000 fc:00 27921547                   /usr/lib/x86_64-linux-gnu/libboost_python-py27.so.1.58.0
7f37618da000-7f3761ada000 ---p 00049000 fc:00 27921547                   /usr/lib/x86_64-linux-gnu/libboost_python-py27.so.1.58.0
7f3761ada000-7f3761adb000 r--p 00049000 fc:00 27921547                   /usr/lib/x86_64-linux-gnu/libboost_python-py27.so.1.58.0
7f3761adb000-7f3761add000 rw-p 0004a000 fc:00 27921547                   /usr/lib/x86_64-linux-gnu/libboost_python-py27.so.1.58.0
7f3761add000-7f3761dcf000 r-xp 00000000 fc:00 27918842                   /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0
7f3761dcf000-7f3761fcf000 ---p 002f2000 fc:00 27918842                   /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0
7f3761fcf000-7f3761fd1000 r--p 002f2000 fc:00 27918842                   /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0
7f3761fd1000-7f3762048000 rw-p 002f4000 fc:00 27918842                   /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0
7f3762048000-7f376206b000 rw-p 00000000 00:00 0 
7f376206b000-7f3762091000 r-xp 00000000 fc:00 19272005                   /lib/x86_64-linux-gnu/ld-2.23.so
7f37620f5000-7f376226f000 rw-p 00000000 00:00 0 
7f376228d000-7f3762290000 rw-p 00000000 00:00 0 
7f3762290000-7f3762291000 r--p 00025000 fc:00 19272005                   /lib/x86_64-linux-gnu/ld-2.23.so
7f3762291000-7f3762292000 rw-p 00026000 fc:00 19272005                   /lib/x86_64-linux-gnu/ld-2.23.so
7f3762292000-7f3762293000 rw-p 00000000 00:00 0 
7fff29d8e000-7fff29daf000 rw-p 00000000 00:00 0                          [stack]
7fff29dd2000-7fff29dd4000 r--p 00000000 00:00 0                          [vvar]
7fff29dd4000-7fff29dd6000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
aborted (core dumped)

0 个答案:

没有答案