在Boost Python编译模块中缺失库
我在用Boost Python编译和运行一个简单的“你好,世界”示例时遇到了麻烦。在我之前的安装(在家里)中,我可以使用sudo命令把Boost安装到/usr/local/目录下,但在我现在的地方,我没有管理员权限,所以我只能把它安装到另一个目录(./boost_1_55_0)里。
代码看起来能编译,但当我运行一个示例的Python脚本时,出现了以下错误:
ImportError: libboost_python.so.1.55.0: cannot open shared object file: No such file or directory
这个共享对象确实存在于./boost_1_55_0/stage/lib目录下。因此,在寻找解决办法时,我尝试在我的Python脚本中添加以下内容:
import os
os.environ['LD_LIBRARY_PATH']='./boost_1_55_0/stage/lib/'
但结果还是一样。我在所有情况下也尝试过绝对路径。此外,我还查看了以下StackOverflow的问题:
并尝试实现它,但没有成功。我附上了makefile、.cpp文件和测试脚本,希望有人能提供帮助。作为额外的信息,我尝试使用distutils来编译,但也没有成功。我也附上了那个Python脚本。
谢谢你的帮助,
Nathan
Makefile:
# location of the Python header files
PYTHON_VERSION = 2.7
PYTHON_INCLUDE = /usr/local/python/canopy_1.1.0/appdata/canopy-1.1.0.1371.rh5-x86_64/include/python2.7
PYTHON_LIB = /usr/local/python/canopy_1.1.0/appdata/canopy-1.1.0.1371.rh5-x86_64/lib
# location of the Boost Python include files and library
BOOST_INC = ./boost_1_55_0
BOOST_LIB = ./boost_1_55_0/stage/lib/
# compile mesh classes
TARGET = evoalg_gen
$(TARGET).so: $(TARGET).o
LD_LIBRARY_PATH=$(BOOST_LIB) g++ -shared -Wl,--export-dynamic $(TARGET).o -L$(BOOST_LIB) -L$(PYTHON_LIB) -lpython$(PYTHON_VERSION) -lboost_python -o $(TARGET).so
$(TARGET).o: $(TARGET).cpp
LD_LIBRARY_PATH=$(BOOST_LIB) g++ -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -fPIC -c $(TARGET).cpp
evoalg_gen.cpp:
#include <Python.h>
#include <boost/python.hpp>
char const* greet()
{
return "hello, world";
}
BOOST_PYTHON_MODULE(hello_ext)
{
using namespace boost::python;
def("greet", greet);
}
test_helloworld.py:
import os
os.environ['LD_LIBRARY_PATH']='./boost_1_55_0/stage/lib/'
#import EvoAlgs
import evoalg_gen
print hello_ext.greet()
setup.py:
from distutils.core import setup, Extension
module1 = Extension('EvoAlgs',
include_dirs = ['/raid1/nathanm/9502_Model/c++/boost_1_55_0'], ### Boost Directory
library_dirs = [''#, ### Boost Library
#'/usr/local/python/epd-7.1-2-rh5-x86_64/lib/' ### Python Library
],
libraries = ['boost_python'],
sources = ['evoalg_gen.cpp'])
setup (name = 'EvoAlgs',
version = '0.1',
description = 'This is a collection of evolutionary algorithms',
ext_modules = [module1])
附加信息:
运行以下命令:
LD_LIBRARY_PATH=./boost_1_55_0/stage/lib python test_helloworld.py
会出现以下错误:
LD_LIBRARY_PATH=./boost_1_55_0/stage/lib: Command not found.
根据@kirbyfan64sos的建议,在我的makefile中添加,-rpath,./boost_1_55_0/stage/lib/后,运行时出现了以下错误:
ImportError: ./boost_1_55_0/stage/lib/libboost_python.so.1.55.0: undefined symbol: PyUnicodeUCS4_FromEncodedObject
1 个回答
0
要使用LD_LIBRARY_PATH这个环境变量,它必须在程序真正运行之前设置好,而不是在程序运行时(那样没用)或者在编译时(那样只会为编译器自己设置路径)。举个例子:
LD_LIBRARY_PATH=./boost_1_55_0/stage/lib python my_script.py
不过,你可以通过使用'-Wl,-rpath,'把库的路径写死在库里面。所以在这种情况下,链接时你可以使用'-Wl,-rpath,./boost_1_55_0/stage/lib/'。