2
我在OS X上嘗試這個boost python with cmake的例子。這篇文章有點舊,但是我找不到更新的東西。我的目標是使用CMake(因爲我使用CLion)來構建C++和Python庫的集成項目。我使用Python 2.7在OS X上在OS X上使用CMake的Boost Python
我.cpp文件是
#include <boost/python.hpp>
char const* yay()
{
return "Yay!";
}
BOOST_PYTHON_MODULE(libyay)
{
using namespace boost::python;
def("yay", yay);
}
我CMakesLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 3.3)
IF(NOT CMAKE_BUILD_TYPE)
SET(CMAKE_BUILD_TYPE "DEBUG")
#SET(CMAKE_BUILD_TYPE "RELEASE")
#SET(CMAKE_BUILD_TYPE "RELWITHDEBINFO")
#SET(CMAKE_BUILD_TYPE "MINSIZEREL")
ENDIF()
FIND_PACKAGE(PythonLibs 2.7 REQUIRED)
FIND_PACKAGE(Boost)
IF(Boost_FOUND)
INCLUDE_DIRECTORIES("${Boost_INCLUDE_DIRS}" "/usr/include/python2.7")
SET(Boost_USE_STATIC_LIBS OFF)
SET(Boost_USE_MULTITHREADED ON)
SET(Boost_USE_STATIC_RUNTIME OFF)
FIND_PACKAGE(Boost COMPONENTS python)
ADD_LIBRARY(yay SHARED yay.cpp)
TARGET_LINK_LIBRARIES(yay ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
ELSEIF(NOT Boost_FOUND)
MESSAGE(FATAL_ERROR "Unable to find correct Boost version. Did you set BOOST_ROOT?")
ENDIF()
IF(CMAKE_COMPILER_IS_GNUCXX)
ADD_DEFINITIONS("-Wall")
ELSE()
SET(CMAKE_CXX_FLAGS "-Wall")
MESSAGE("You have compiler " ${CMAKE_CXX_COMPILER_ID})
#MESSAGE(FATAL_ERROR "CMakeLists.txt has not been tested/written for your compiler.")
MESSAGE("CMakeLists.txt has not been tested/written for your compiler.")
ENDIF()
最後,我打開一個Python控制檯,並嘗試此
from ctypes import *
ly = cdll.LoadLibrary("libyay.dylib")
print ly.yay()
產生此錯誤:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 375, in __getattr__
func = self.__getitem__(name)
File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 380, in __getitem__
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: dlsym(0x7fdb5344aec0, yay): symbol not found
關於(a)整合C++和Python的整個方法是否過時,(b)這個錯誤應該告訴我什麼以及(c)使用CMake的其他方法,我將不勝感激。