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'
答案 0 :(得分:0)
我正在使用macOS,最近我也遇到了这个TypeError
!可能是由于将构建的.so
文件链接到另一个Python解释程序的 lib/libpython2.7.dylib
文件引起的。
.so
命令检出otool -L
文件:$ otool -L libh264decoder.so
libh264decoder.so:
/somepath/build/libh264decoder.so (compatibility version 0.0.0, current version 0.0.0)
/usr/local/opt/ffmpeg/lib/libavcodec.58.dylib (compatibility version 58.0.0, current version 58.35.100)
/usr/local/opt/ffmpeg/lib/libswscale.5.dylib (compatibility version 5.0.0, current version 5.3.100)
/usr/local/opt/ffmpeg/lib/libavutil.56.dylib (compatibility version 56.0.0, current version 56.22.100)
/usr/local/opt/boost-python/lib/libboost_python27-mt.dylib (compatibility version 0.0.0, current version 0.0.0)
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/Python (compatibility version 2.7.0, current version 2.7.0)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 400.9.4)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.200.5)
请注意,此.so
文件已链接到MacPort安装的Python,该文件位于:/opt/local/Library/Frameworks/Python.framework/Versions/2.7/Python
它位于:/Users/name/anaconda3/envs/py27/
(显然是Anaconda安装的)。
cmake
变量,使DPYTHON_LIBRARY
链接到正确的Python库:$ cd build/
$ cmake -DPYTHON_LIBRARY="/Users/name/anaconda3/envs/py27/lib/libpython2.7.dylib" ..
$ make
$ otool -L libh264decoder.so
libh264decoder.so:
/somepath/build/libh264decoder.so (compatibility version 0.0.0, current version 0.0.0)
/usr/local/opt/ffmpeg/lib/libavcodec.58.dylib (compatibility version 58.0.0, current version 58.35.100)
/usr/local/opt/ffmpeg/lib/libswscale.5.dylib (compatibility version 5.0.0, current version 5.3.100)
/usr/local/opt/ffmpeg/lib/libavutil.56.dylib (compatibility version 56.0.0, current version 56.22.100)
/usr/local/opt/boost-python/lib/libboost_python27-mt.dylib (compatibility version 0.0.0, current version 0.0.0)
@rpath/libpython2.7.dylib (compatibility version 2.7.0, current version 2.7.0)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 400.9.4)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.200.5)
请注意,libpython2.7.dylib
的链接路径已更改。