使用Boost Python时CMake安装后出现Python ImportError求助
make install Hey there! Let's break down why your "hello world" Boost Python module works in the build directory but fails after make install—and how to fix it.
The error ImportError: libboost_python.so.1.64.0: cannot open shared object file happens because your system's dynamic linker can't find the Boost Python shared library once your module is moved to the install directory. When you run the module in the build directory, CMake sets up temporary runtime paths (RPATH) that point to the Boost libraries you used during compilation, but those paths don't carry over automatically when you install the module.
Here are the most reliable fixes:
1. Update the Dynamic Linker's Search Path (Quick Fix)
If your Boost libraries are installed in a non-system directory (like a custom path or your project's source directory), the linker doesn't know to look there by default.
Temporary Fix (Per Session)
Before running Python, add the Boost library path to the LD_LIBRARY_PATH environment variable:
export LD_LIBRARY_PATH=/path/to/your/boost/lib:$LD_LIBRARY_PATH
Replace /path/to/your/boost/lib with the actual directory containing libboost_python.so.1.64.0.
Permanent Fix
Make the linker remember this path permanently:
- Create a config file in
/etc/ld.so.conf.d/:echo "/path/to/your/boost/lib" | sudo tee /etc/ld.so.conf.d/boost-python.conf - Update the linker cache:
sudo ldconfig
2. Configure CMake to Set Correct RPATH (Recommended)
The best long-term fix is to tell CMake to embed the correct runtime paths into your installed module so it always knows where to find Boost. Add these lines to your CMakeLists.txt:
# Use the link paths from compilation for the installed module set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) # Add your install directory to the RPATH (if it's not a system default) set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
CMAKE_INSTALL_RPATH_USE_LINK_PATHtells CMake to include all the library paths used during linking (like your Boost library path) in the installed module's RPATH.CMAKE_INSTALL_RPATHadds your module's install directory to the runtime path, ensuring Python can find your module itself.
Also, double-check your CMakeLists.txt properly finds and links Boost/Python:
find_package(Boost REQUIRED COMPONENTS python) find_package(PythonInterp REQUIRED) find_package(PythonLibs REQUIRED) include_directories(${Boost_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS}) add_library(hello SHARED hello.cpp) target_link_libraries(hello ${Boost_LIBRARIES} ${PYTHON_LIBRARIES}) # Set install prefix to your source directory (as you mentioned) set(CMAKE_INSTALL_PREFIX ${PROJECT_SOURCE_DIR}) install(TARGETS hello DESTINATION lib)
3. Verify Python Can Find Your Module
Even if the linker finds Boost, Python might not know where your installed module is. Add your install lib directory to PYTHONPATH:
export PYTHONPATH=${PROJECT_SOURCE_DIR}/lib:$PYTHONPATH
Or, when running Python, make sure you're in a directory where your module is accessible, or add the path programmatically in Python:
import sys sys.path.append("/path/to/your/installed/lib") import hello
Quick Debug Check
To confirm the issue is path-related, run ldd on your installed module:
ldd /path/to/your/installed/lib/libhello.so
If libboost_python.so.1.64.0 shows "not found", you know the linker path is the problem.
内容的提问来源于stack exchange,提问作者Bociek

