【问题标题】:C++ Boost Python methods from *.pyd library does not work*.pyd 库中的 C++ Boost Python 方法不起作用
【发布时间】:2019-02-06 02:59:24
【问题描述】:

美好的一天,我对 boost::python 中的简单示例有一些问题 - 所有必须返回除 char const* 之外的任何类型或创建类的方法都不会返回任何内容,我看到它的 RESTART: Shell 是什么。

我正在使用:

  • CLion 2018.3.3,内部版本 #CL-183.5153.40,于 2019 年 1 月 10 日构建;
  • MinGW-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0;
  • CMake 3.13.0-rc1-;
  • Python 3.7 (x86_64);
  • 提升 1.68.0,

它是用下一个参数构建的:

bootstrap gcc 
b2 -j12 toolset=gcc runtime-link=shared architecture=x86 address-model=64 --with-python cxxflags="-D__int64=\"long long int\" -DBOOST_REGEX_MATCH_EXTRA -D__NO_INLINE__ -D_hypot=hypot"  --build-type=complete --layout=tagged stage

CMakeList.txt 包含:

cmake_minimum_required(VERSION 3.13)
project(pylibtest)

set(CMAKE_CXX_STANDARD 17)

set(GCC_COVERAGE_COMPILE_FLAGS "-march=native  -m64 -D_hypot=hypot -static -static-libstdc++ -static-libgcc -Wall")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}" )

message(STATUS "start running cmake...")

set(Boost_DEBUG                 OFF)
#set(Boost_USE_STATIC_LIBS       ON)
set(Boost_USE_MULTITHREADED     ON)
#set(Boost_USE_STATIC_RUNTIME    ON)

find_package(Boost 1.68.0 COMPONENTS python37 REQUIRED)
if(NOT Boost_FOUND)
    set(Boost_LIBRARY_DIR "${Boost_Path}/stage/lib")
    message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARY_DIR}")
    message(STATUS "Boost_VERSION: ${Boost_VERSION}")
    include_directories(${Boost_INCLUDE_DIRS})
    link_directories(${Boost_LIBRARIES})
endif()

find_package(PythonInterp 3.7 REQUIRED)
find_package(PythonLibs 3.7 REQUIRED)
if(PYTHONLIBS_FOUND)
    message(STATUS "PYTHON_LIBRARIES = ${PYTHON_LIBRARIES}")
    message(STATUS "PYTHON_EXECUTABLE = ${PYTHON_EXECUTABLE}")
    message(STATUS "PYTHON_INCLUDE_DIRS = ${PYTHON_INCLUDE_DIRS}")
    include_directories(${PYTHON_INCLUDE_DIRS})
    link_directories(${PYTHON_LIBRARIES})
endif()

if(Boost_FOUND AND PYTHONLIBS_FOUND)
    add_library(pylibtest SHARED library.cpp library.h wrap.cpp Some.cpp Some.h)
    target_include_directories(pylibtest SYSTEM PRIVATE  ${Boost_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS})
    target_link_libraries(pylibtest ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
endif()

wrap.cpp 包含所有方法及其在 python 模块中的定义:

#include <boost/python.hpp>
//#include "Some.h"

using namespace boost::python;

char const* SayHello()
{
    return "Hello, from c++ dll!";
}

int GetMeNumber(){
    int temp = 255;
    return temp;
}

BOOST_PYTHON_MODULE( example )
{
    def("SayHello", SayHello);
    def("GetMeNumber", GetMeNumber);
}

IDLE 3.7(64 位)中的 Python 脚本:

print("Procedure started:")
import example
print(example.SayHello())
print("Procudure ended")
print("-=-=-=-=-=-=-=-=-=-=-=-")
print("New procedure started")
count = example.GetMeNumber();
print(count)
print("New procudure ended")

Python 3.7.0 Shell 中的输出:

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 
===== RESTART: C:\..\pi.py =====
Procedure started:
Hello, from c++ dll!
Procudure ended
-=-=-=-=-=-=-=-=-=-=-=-
New procedure started

=============================== RESTART: Shell ===============================
>>> 

当我尝试在 PyCharm 中使用相同的脚本时,我得到了错误代码消息:

Procedure started:
Hello, from c++ dll!
Procudure ended
-=-=-=-=-=-=-=-=-=-=-=-
New procedure started

Process finished with exit code -1073741819 (0xC0000005)

有人知道我的 out lib 出了什么问题,还有什么更好的方法可以让它工作吗?

【问题讨论】:

    标签: python c++ boost pyd


    【解决方案1】:

    代码0xC0000005Access Violation 错误。您可以尝试在管理帐户下重复所有操作。

    【讨论】:

    • 感谢您的回复,我在 Win 10 下使用管理帐户工作,默认情况下在管理规则下启动了我所有的 IDE。我不使用空间来确认我是管理员。主要问题是为什么返回值作为 char const* 和 std::cout
    猜你喜欢
    • 2019-11-13
    • 1970-01-01
    • 2012-04-09
    • 1970-01-01
    • 2018-10-06
    • 2012-06-17
    • 2013-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多