AttributeError:Windows中的Boost.Python和CMake模块没有属性

时间:2018-10-06 18:55:36

标签: python c++ windows boost cmake

简介

我目前正在通过 Windows x64 支持扩展我的Linux构建(可以正常工作2年),并且遇到 boost.python 模块的问题。我使用 CMake 构建代码(请参见我使用的cmake脚本的快照),这为我提供了无法加载 CppCudaPyModule.dll 共享库。带有消息AttributeError: module has no attribute boost.python 模块(请参见下面的完整错误)。

  

平台详细信息:   我在 Windows Server2016 上工作,使用 Anaconda 3.6 x64 ,并使用 msvc-14.0 重建boost和boost.python。

问题

在linux上,boost.python模块可以正常工作。我怀疑我在Windows中使用CMAKE 无法正确构建该DLL。有什么想法会出问题吗?

错误

Traceback (most recent call last):
  File "C:\Users\Administrator\Documents\code\apps\pyservice\__main__.py", line 31, in <module>
    main()
  File "C:\Users\Administrator\Documents\code\apps\pyservice\__main__.py", line 16, in main
    qb = CppCudaPyModule.LibraryFacade([
AttributeError: module 'CppCudaPyModule' has no attribute 'LibraryFacade'
Press any key to continue . . .

CMake

find_package(PythonInterp 3.6)
find_package(PythonLibs 3.6)

find_package(
  Boost 1.65.0
  REQUIRED
  COMPONENTS
  python3
)

(STATUS "Adding CppCudaPyModule")

file(GLOB_RECURSE SRC *.h *.cpp)

add_library(CppCudaPyModule SHARED ${SRC})

message (STATUS "PYTHON_LIBRARIES ${PYTHON_LIBRARIES}" )

target_link_libraries(
    CppCudaPyModule
    CppLib
    ${Boost_LIBRARIES}
    ${PYTHON_LIBRARIES}                 # This is required in Win
    C:\\Python27\\libs\\python27.lib    # This is required in Win (I do not know why)
)

set_property(TARGET CppCudaPyModule PROPERTY PREFIX "")
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY_PY     "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/CppCudaPyModule.so")

if (WIN32)
    set (CMAKE_LIBRARY_OUTPUT_DIRECTORY_PY     "${PROJECT_BINARY_DIR}/libs/CppCudaPyModule/Debug/CppCudaPyModule.dll")
endif()

message (STATUS "Boost.python output ${CMAKE_LIBRARY_OUTPUT_DIRECTORY_PY}")

增强C ++包装器

我用boost.python包装的代码示例如下:

//Required to address link errors in WIN32
#define BOOST_PYTHON_STATIC_LIB 

#include <libs/CppLib/LibraryFacade.h>
#include <libs/CppCudaPyModule/Converters/ListToVectorConverter.h>

#include <boost/python.hpp>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/operators.hpp>

#include <vector>
#include <string>
#include <memory>

using namespace boost::python;

int execute(std::vector<std::string> const& args)
{
    LibraryFacade calculationEngine(args);
    return calculationEngine.execute();
}


void exportQbCppObjectsToPython()
{
    #The converter is not very important here
    #but I include them to give you a better picture 
    #of how the code looks like
    initialize_vector_converters<std::string>();

    class_<LibraryFacade>("LibraryFacade",init< std::vector<std::string> const& >())
        .def("execute", &LibraryFacade::execute)
    ;

}

BOOST_PYTHON_MODULE(CppCudaPyModule)        // Module name
{
    using namespace boost::python;

    exportQbCppObjectsToPython();

    def("execute", execute);
}

0 个答案:

没有答案