【发布时间】:2019-08-09 19:18:35
【问题描述】:
cmake 文件
cmake_minimum_required(VERSION 3.13)
project(p1)
set(CMAKE_CXX_STANDARD 11)
FIND_PACKAGE(PythonInterp)
if (PYTHONINTERP_FOUND)
if (UNIX AND NOT APPLE)
if (PYTHON_VERSION_MAJOR EQUAL 3)
FIND_PACKAGE(Boost COMPONENTS python${PYTHON_VERSION_SUFFIX})
FIND_PACKAGE(PythonInterp 3)
FIND_PACKAGE(PythonLibs 3 REQUIRED)
else()
FIND_PACKAGE(Boost COMPONENTS python)
FIND_PACKAGE(PythonInterp)
FIND_PACKAGE(PythonLibs REQUIRED)
endif()
else()
if (PYTHON_VERSION_MAJOR EQUAL 3)
FIND_PACKAGE(Boost COMPONENTS
python${PYTHON_VERSION_MAJOR}${PYTHON_VERSION_MINOR})
FIND_PACKAGE(PythonInterp 3)
FIND_PACKAGE(PythonLibs 3 REQUIRED)
else()
FIND_PACKAGE(Boost COMPONENTS
python${PYTHON_VERSION_MAJOR}${PYTHON_VERSION_MINOR})
FIND_PACKAGE(PythonInterp)
FIND_PACKAGE(PythonLibs REQUIRED)
endif()
endif()
else()
message("Python not found")
endif()
message(STATUS "PYTHON_LIBRARIES = ${PYTHON_LIBRARIES}")
message(STATUS "PYTHON_EXECUTABLE = ${PYTHON_EXECUTABLE}")
message(STATUS "PYTHON_INCLUDE_DIRS = ${PYTHON_INCLUDE_DIRS}")
message(STATUS "Boost_LIBRARIES = ${Boost_LIBRARIES}")
#ENABLE_TESTING()
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS})
add_library(pylib SHARED pylib.cpp)
target_link_libraries(pylib ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
#
# Tweaks the name of the library to match what Python expects
set_target_properties(pylib PROPERTIES SUFFIX .so)
set_target_properties(pylib PROPERTIES PREFIX "")
cmake 输出:
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -
DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - Unix Makefiles"
/Users/studentuser/CLionProjects/sbmlPythonAPI
-- Found PythonInterp: /usr/local/bin/python (found version "2.7.16")
-- Boost version: 1.68.0
-- Found the following Boost libraries:
-- python27
-- PYTHON_LIBRARIES = /usr/lib/libpython2.7.dylib
-- PYTHON_EXECUTABLE = /usr/local/bin/python
-- PYTHON_INCLUDE_DIRS =
/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr
/include/python2.7
-- Boost_LIBRARIES = /usr/local/lib/libboost_python27-mt.dylib
-- Configuring done
-- Generating done
-- Build files have been written to:
/Users/studentuser/CLionProjects/sbmlPythonAPI/cmake-build-debug
Bonjour.hpp
#include <iostream>
#include <string>
using namespace std;
class Bonjour
{
// Private attribute
string m_msg;
public:
// Constructor
Bonjour(string msg):m_msg(msg) { }
// Methods
void greet() { std::cout << m_msg << std::endl; }
void check_func() {cout<<"Hello! I am working"; }
// Getter/Setter functions for the attribute
void set_msg(std::string msg) { this->m_msg = msg; }
std::string get_msg() const { return m_msg; }
};
pylib.cpp
#include <boost/python.hpp>
#include "Bonjour.hpp"
using namespace boost::python;
BOOST_PYTHON_MODULE(pylib)
{
class_< Bonjour >("Bonjour", init<std::string>())
.def("greet", &Bonjour::greet)
.add_property("msg", &Bonjour::get_msg, &Bonjour::set_msg);
}
我在尝试运行时收到磁贴中所述的错误消息
from pylib import Bonjour
b = Bonjour("He")
错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-2-a019b42ef03f> in <module>()
----> 1 b = Bonjour("He")
TypeError: __init__() should return None, not 'NoneType'
【问题讨论】:
-
您能否edit 向我们展示产生此错误消息的源文件,以及您能否向我们展示确切的编译器错误(您的标题很有帮助,但不清楚是否“ with Python Boost”是否是错误消息的一部分)。
-
谢谢。以及错误消息的确切文本(包括文件名和行号)?
-
@MartinBonner 更新
标签: c++ python-3.x python-2.7 boost boost-python