gpt4 book ai didi

c++ - 在 OS X 上将 Boost Python 与 CMake 结合使用

转载 作者:搜寻专家 更新时间:2023-10-31 02:13:57 34 4
gpt4 key购买 nike

我正在尝试这个 boost python with cmake OS X 上的示例。该帖子有点旧,但我找不到更新的东西。我的目标是使用 CMake(因为我使用的是 CLion)来构建 C++ 和 Python 库的集成项目。我在 OS X 上使用 Python 2.7

我的.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 的其他方法。

最佳答案

关键似乎是我的 Python 版本无法加载由 make 生成的 .dylib 文件。我不知道为什么,所以我在 CMakeLists.txt 文件中使用了这个技巧并包含了 if(APPLE) 命令

cmake_minimum_required( VERSION 3.3 )
project( BoostPythonHelloWorld )

# Make a .so output!
if(APPLE)
set(CMAKE_SHARED_LIBRARY_SUFFIX ".so")
endif(APPLE)

# Find necessary packages
find_package( PythonLibs 2.7 REQUIRED )
include_directories( ${PYTHON_INCLUDE_DIRS} )

find_package( Boost COMPONENTS python REQUIRED )
include_directories( ${Boost_INCLUDE_DIR} )

# Build our library
add_library( greet SHARED greet.cpp )

# Define the wrapper library that wraps our library
add_library( greet_ext SHARED greet_ext.cpp )
target_link_libraries( greet_ext ${Boost_LIBRARIES} ${PYTHON_LIBRARIES} greet )
# don't prepend wrapper library name with lib
set_target_properties( greet_ext PROPERTIES PREFIX "" )

在完成 cmakemake 之后,我能够打开 Python shell 并运行

import greet_ext
greet_ext.greet()

我已经在 github 上发布了一个完整的示例.我还要感谢 Feral Chicken a useful post

关于c++ - 在 OS X 上将 Boost Python 与 CMake 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40554607/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com