C++ boostpython-对`Py_NoneStruct';
尝试使用Boost Python时出现以下错误:C++ boostpython-对`Py_NoneStruct';,c++,boost,C++,Boost,尝试使用Boost Python时出现以下错误: /usr/include/boost/python/object_core.hpp:400: undefined reference to `_Py_NoneStruct' 安装在Ubuntu 18.04上,使用: sudo apt-get install libboost-all-dev CMakeLists.txt cmake_minimum_required(VERSION 3.10) project(boostpy) set(CMA
/usr/include/boost/python/object_core.hpp:400: undefined reference to `_Py_NoneStruct'
安装在Ubuntu 18.04上,使用:
sudo apt-get install libboost-all-dev
CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(boostpy)
set(CMAKE_CXX_STANDARD 14)
include_directories(/usr/include/python3.6)
find_package(Boost 1.65.1 COMPONENTS python3-py36 REQUIRED)
if(Boost_FOUND)
message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")
message(STATUS "Boost_VERSION: ${Boost_VERSION}")
include_directories(${Boost_INCLUDE_DIRS})
endif()
add_executable(boostpy main.cpp)
if(Boost_FOUND)
target_link_libraries(boostpy ${Boost_LIBRARIES})
endif()
main.cpp
#include <iostream>
#include <boost/python.hpp>
#include <Python.h>
int main()
{
std::cout << "Hello, World!" << std::endl;
return 0;
}
这个问题的解决方案是,除了boostpython库之外,我还必须导入Python库本身 在本例中,我的Python库位于/usr/lib/x86_64-linux-gnu/libpython3.6m.so 在此过程中,我学到了find_包功能的一些有用用法 在此过程中还存在一些其他问题:
- 我不得不使用共享库而不是可执行文件
- find_包默认为不同版本的Python。我必须指定正确的版本
- 我必须确保BOOST_PYTHON_模块(hello)中指定的名称与库名称匹配。图书馆被称为“libhello.so”。我不得不告诉cmake删除“lib”前缀
char const* greet()
{
return "hello, world";
}
#include <boost/python.hpp>
BOOST_PYTHON_MODULE(hello)
{
using namespace boost::python;
def("greet", greet);
}
另一个数据点:这似乎与boostpython库特别相关,而不是一些通用的boostlibrary链接问题。我使用Boost文件系统运行了一个测试,效果很好。
char const* greet()
{
return "hello, world";
}
#include <boost/python.hpp>
BOOST_PYTHON_MODULE(hello)
{
using namespace boost::python;
def("greet", greet);
}
cmake_minimum_required(VERSION 3.16)
project(hello)
set(CMAKE_CXX_STANDARD 14)
find_package(Python 3.6 COMPONENTS Interpreter Development REQUIRED)
find_package(Boost 1.65.1 COMPONENTS python3.6 REQUIRED)
if(Boost_FOUND)
message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")
message(STATUS "Boost_VERSION: ${Boost_VERSION}")
include_directories(${Boost_INCLUDE_DIRS})
endif()
if(Python_FOUND)
message(STATUS "Python Found: ${Python_EXECUTABLE}")
message(STATUS "Python Found: ${Python_INCLUDE_DIRS}")
message(STATUS "Python Found: ${Python_LIBRARIES}")
message(STATUS "Python Found: ${Python_LIBRARY_DIRS}")
include_directories(${Python_INCLUDE_DIRS})
endif()
add_library(hello SHARED hello.cpp)
SET_TARGET_PROPERTIES(hello PROPERTIES PREFIX "")
if(Boost_FOUND)
target_link_libraries(hello ${Boost_LIBRARIES} ${Python_LIBRARIES})
endif()