已编译的 boost_python 扩展无法在 python 2.7 中导入
Compiled boost_python extension fails to import in python 2.7
我正在尝试使用 boost 导入用 c++ 编码的 python 扩展。虽然我在使用 cmake 编译扩展时遇到了一些问题,但我设法将其链接到 boost_python27 库。然后我使用 pythons distutils 将扩展安装到 python 框架中。
但是,当我尝试导入模块时出现以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: dlopen(./tools.so, 2): Library not loaded: @rpath/libboost_python.dylib
Referenced from: /Users/DaniBook/CLionProjects/Uebung1/cmake-build-debug/tools.so
Reason: image not found
我尝试了我在 Internet 上找到的所有方法,包括重新安装 boost 和使用 distutils 扩展重新编译等,以使其正常工作。任何人都可以就此提供一些帮助,也可以回答这个丢失的不祥图像是什么吗?
tools.h
#ifndef UEBUNG1_TOOLS_H
#define UEBUNG1_TOOLS_H
#include <string>
#include <vector>
#include <map>
#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
using namespace std;
vector<string> *product(string alphabet, int repeats);
vector<string> *product(vector<string> pools);
vector<string>* hammdist(string &pattern, int distance);
#endif //UEBUNG1_TOOLS_H
tools.cpp
#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include "tools.h"
#include <vector>
#include <string>
#include <map>
using namespace std;
vector<string> *product(string alphabet, int repeats) {
//initializing vector
auto *results = new vector<string>();
for(auto character : alphabet) {
string tmpstr;
tmpstr = character;
results->push_back(tmpstr);
}
//cartesian product generation
for(int i = 1; i < repeats; i++) {
vector<string> tmp = *results;
results->clear();
//iterating over temporary list adding elements from pool to each contained string
for(auto &it : tmp) {
for(auto &character : alphabet) {
results->push_back(it + character);
}
}
}
return results;
}
vector<string> product(vector<string> pools) {
//initializing vector
auto *results = new vector<string>();
for(auto character : pools[0]) {
string tmpstr;
tmpstr = character;
results->push_back(tmpstr);
}
//removing the first pool container
pools.erase(pools.begin());
//cartesian product generation
for(const auto &pool : pools) {
vector<string> tmp = *results;
results->clear();
//iterating over temporary list adding elements from pool to each contained string
for(auto &it : tmp) {
for(auto character : pool) {
results->push_back(it + character);
}
}
}
return results;
}
vector<string>* hammdist(string &pattern, int distance) {
map<char, string> possibles = {
{'A', "CGT"},
{'C', "AGT"},
{'G', "ACT"},
{'T', "ACG"}
};
auto *results = new vector<string>();
vector<string> *masks = product("01", pattern.size());
for(auto &mask : *masks) {
auto *permute = new vector<string>();
auto *tmp = new vector<string>();
if(count(mask.begin(), mask.end(), '1') == distance) {
for(int i = 0; i < pattern.size(); i++) {
if(mask[i] != '1') {
string tmpstr;
tmpstr = pattern[i];
tmp->push_back(tmpstr);
}
else {
tmp->push_back(possibles[pattern[i]]);
}
}
permute = product(*tmp);
results->insert(results->end(), permute->begin(), permute->end());
}
delete permute;
delete tmp;
}
return results;
}
/*
initializing function pointers in order to tell boost that we have
overloaded functions to expose to python
*/
vector<string> *(*product1)(string, int) = &product;
vector<string> *(*product2)(vector<string> pools) = &product;
//include all functions that are used by the function to expose to the BOOST_PYTHON_MODULE call
using namespace boost::python;
BOOST_PYTHON_MODULE(tools) {
//telling boost_python that we have overloaded functions which need to be called in the respective situations
//return_value_policy<manage_new_objects> is required in order for the interface to handle the new invocation and the returned pointer
def("product", product1, return_value_policy<manage_new_object>());
def("product", product2, return_value_policy<manage_new_object>());
def("hammdist", hammdist, return_value_policy<manage_new_object>());
//vector_indexing_suite handles the wrapping of vector member functions
//enables handling vector in a pythonic way when using in python
class_<std::vector<string>>("string_vector")
.def(vector_indexing_suite<std::vector<string>>());
}
CMakeLists.txt
make_minimum_required(VERSION 3.12)
project(tools)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
if(APPLE)
set(CMAKE_SHARED_LIBRARY_SUFFIX ".so")
endif(APPLE)
find_package(PythonLibs 2.7 REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
set(PROJECT_SOURCE_DIR src/)
include_directories(${PROJECT_SOURCE_DIR})
set(BOOST_ROOT "/Users/DaniBook/miniconda3/pkgs/boost-1.66.0-py27_1")
set(BOOST_LIBRARYDIR "/Users/DaniBook/miniconda3/pkgs/boost-1.66.0-27_1/lib")
find_package(Boost COMPONENTS python REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
add_library(tools SHARED src/tools.cpp src/tools.h)
target_link_libraries(tools ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
set_target_properties(tools PROPERTIES PREFIX "")
setup.py
from distutils.core import setup
setup(
name = 'tools',
version = '0.1',
py_modules = ['tools'])
好的,经过一些额外的研究,我得出了一个非常有趣的结论,当您查看错误消息时,这个结论非常明显。
问题不是缺少共享库本身,而是编译时使用的对 boost 库的引用的未定义相对路径(linker 阶段)。这可能是由于使用了驻留在 miniconda 中而不是 /usr/local/lib 中的 boost 安装,后者可能位于 pythonpath 或其他东西中。但是,这可以使用 otool
和 install_name_tool
手动解决(可以通过安装 XCode 开发工具在 mac 上获得)。
因此您在命令行中执行以下操作:
otools -L ${PATH_TO_PYTHONEXTENSION}/someextension.so
像这样在终端上列出所有库及其系统路径
./build/lib.macosx-10.13-x86_64-2.7/tools.so:
@rpath/libboost_python.dylib (compatibility version 0.0.0, current version 0.0.0)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 400.9.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.50.4)
可以很容易地看到我们在编译时 link 编辑的 boost 库缺少路径,我们使用 install_name_tool
:
解决了这个问题
install_name_tool -change @rpath/libboost_python.dylib ${ACTUAL_PATH_TO_libbost_python.dylib} ${PATH_TO_PYTHONEXTENSION}/someextension.so
重新运行 setup.py install
现在可以解决问题并且可以成功导入扩展:
import tools
dir(tools)
给出以下输出:
['__doc__', '__file__', '__name__', '__package__', 'hammdist', 'product', 'string_vector']
link 到外部资源:
我假设它对其他基于 Unix 的操作系统(如 Linux)也同样有效。
希望这可以帮助。
我正在尝试使用 boost 导入用 c++ 编码的 python 扩展。虽然我在使用 cmake 编译扩展时遇到了一些问题,但我设法将其链接到 boost_python27 库。然后我使用 pythons distutils 将扩展安装到 python 框架中。
但是,当我尝试导入模块时出现以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: dlopen(./tools.so, 2): Library not loaded: @rpath/libboost_python.dylib
Referenced from: /Users/DaniBook/CLionProjects/Uebung1/cmake-build-debug/tools.so
Reason: image not found
我尝试了我在 Internet 上找到的所有方法,包括重新安装 boost 和使用 distutils 扩展重新编译等,以使其正常工作。任何人都可以就此提供一些帮助,也可以回答这个丢失的不祥图像是什么吗?
tools.h
#ifndef UEBUNG1_TOOLS_H
#define UEBUNG1_TOOLS_H
#include <string>
#include <vector>
#include <map>
#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
using namespace std;
vector<string> *product(string alphabet, int repeats);
vector<string> *product(vector<string> pools);
vector<string>* hammdist(string &pattern, int distance);
#endif //UEBUNG1_TOOLS_H
tools.cpp
#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include "tools.h"
#include <vector>
#include <string>
#include <map>
using namespace std;
vector<string> *product(string alphabet, int repeats) {
//initializing vector
auto *results = new vector<string>();
for(auto character : alphabet) {
string tmpstr;
tmpstr = character;
results->push_back(tmpstr);
}
//cartesian product generation
for(int i = 1; i < repeats; i++) {
vector<string> tmp = *results;
results->clear();
//iterating over temporary list adding elements from pool to each contained string
for(auto &it : tmp) {
for(auto &character : alphabet) {
results->push_back(it + character);
}
}
}
return results;
}
vector<string> product(vector<string> pools) {
//initializing vector
auto *results = new vector<string>();
for(auto character : pools[0]) {
string tmpstr;
tmpstr = character;
results->push_back(tmpstr);
}
//removing the first pool container
pools.erase(pools.begin());
//cartesian product generation
for(const auto &pool : pools) {
vector<string> tmp = *results;
results->clear();
//iterating over temporary list adding elements from pool to each contained string
for(auto &it : tmp) {
for(auto character : pool) {
results->push_back(it + character);
}
}
}
return results;
}
vector<string>* hammdist(string &pattern, int distance) {
map<char, string> possibles = {
{'A', "CGT"},
{'C', "AGT"},
{'G', "ACT"},
{'T', "ACG"}
};
auto *results = new vector<string>();
vector<string> *masks = product("01", pattern.size());
for(auto &mask : *masks) {
auto *permute = new vector<string>();
auto *tmp = new vector<string>();
if(count(mask.begin(), mask.end(), '1') == distance) {
for(int i = 0; i < pattern.size(); i++) {
if(mask[i] != '1') {
string tmpstr;
tmpstr = pattern[i];
tmp->push_back(tmpstr);
}
else {
tmp->push_back(possibles[pattern[i]]);
}
}
permute = product(*tmp);
results->insert(results->end(), permute->begin(), permute->end());
}
delete permute;
delete tmp;
}
return results;
}
/*
initializing function pointers in order to tell boost that we have
overloaded functions to expose to python
*/
vector<string> *(*product1)(string, int) = &product;
vector<string> *(*product2)(vector<string> pools) = &product;
//include all functions that are used by the function to expose to the BOOST_PYTHON_MODULE call
using namespace boost::python;
BOOST_PYTHON_MODULE(tools) {
//telling boost_python that we have overloaded functions which need to be called in the respective situations
//return_value_policy<manage_new_objects> is required in order for the interface to handle the new invocation and the returned pointer
def("product", product1, return_value_policy<manage_new_object>());
def("product", product2, return_value_policy<manage_new_object>());
def("hammdist", hammdist, return_value_policy<manage_new_object>());
//vector_indexing_suite handles the wrapping of vector member functions
//enables handling vector in a pythonic way when using in python
class_<std::vector<string>>("string_vector")
.def(vector_indexing_suite<std::vector<string>>());
}
CMakeLists.txt
make_minimum_required(VERSION 3.12)
project(tools)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
if(APPLE)
set(CMAKE_SHARED_LIBRARY_SUFFIX ".so")
endif(APPLE)
find_package(PythonLibs 2.7 REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
set(PROJECT_SOURCE_DIR src/)
include_directories(${PROJECT_SOURCE_DIR})
set(BOOST_ROOT "/Users/DaniBook/miniconda3/pkgs/boost-1.66.0-py27_1")
set(BOOST_LIBRARYDIR "/Users/DaniBook/miniconda3/pkgs/boost-1.66.0-27_1/lib")
find_package(Boost COMPONENTS python REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
add_library(tools SHARED src/tools.cpp src/tools.h)
target_link_libraries(tools ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
set_target_properties(tools PROPERTIES PREFIX "")
setup.py
from distutils.core import setup
setup(
name = 'tools',
version = '0.1',
py_modules = ['tools'])
好的,经过一些额外的研究,我得出了一个非常有趣的结论,当您查看错误消息时,这个结论非常明显。
问题不是缺少共享库本身,而是编译时使用的对 boost 库的引用的未定义相对路径(linker 阶段)。这可能是由于使用了驻留在 miniconda 中而不是 /usr/local/lib 中的 boost 安装,后者可能位于 pythonpath 或其他东西中。但是,这可以使用 otool
和 install_name_tool
手动解决(可以通过安装 XCode 开发工具在 mac 上获得)。
因此您在命令行中执行以下操作:
otools -L ${PATH_TO_PYTHONEXTENSION}/someextension.so
像这样在终端上列出所有库及其系统路径
./build/lib.macosx-10.13-x86_64-2.7/tools.so:
@rpath/libboost_python.dylib (compatibility version 0.0.0, current version 0.0.0)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 400.9.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.50.4)
可以很容易地看到我们在编译时 link 编辑的 boost 库缺少路径,我们使用 install_name_tool
:
install_name_tool -change @rpath/libboost_python.dylib ${ACTUAL_PATH_TO_libbost_python.dylib} ${PATH_TO_PYTHONEXTENSION}/someextension.so
重新运行 setup.py install
现在可以解决问题并且可以成功导入扩展:
import tools
dir(tools)
给出以下输出:
['__doc__', '__file__', '__name__', '__package__', 'hammdist', 'product', 'string_vector']
link 到外部资源:
我假设它对其他基于 Unix 的操作系统(如 Linux)也同样有效。 希望这可以帮助。