CMake构建致命错误求助:macOS Sierra下Python3.6+OpenCV编译问题
Hey there, sorry you’ve been banging your head against this for two days—let’s get this sorted out. I’ve dealt with similar CMake/Python/OpenCV headaches on macOS Sierra before, so let’s walk through the most likely fixes for that fatal make error.
First, Let’s Rule Out Common CMake Detection Issues
macOS Sierra ships with a system Python 2.7 by default, and CMake loves to prioritize that over your manually installed Python 3.6. Let’s make sure your CMake config is explicitly targeting the right version:
Update Your CMakeLists.txt
Add these lines to ensure CMake picks up Python 3.6 specifically:
# Explicitly require Python 3.6 find_package(Python3 3.6 EXACT REQUIRED COMPONENTS Interpreter Development) # Link Python's headers and libraries include_directories(${Python3_INCLUDE_DIRS}) link_directories(${Python3_LIBRARY_DIRS}) # Add Python and OpenCV libs to your target target_link_libraries(your_project_name_here ${Python3_LIBRARIES} ${OpenCV_LIBS})
Force CMake to Use the Correct Python Path
If CMake still isn’t finding Python 3.6, pass the paths directly when running cmake:
First, get your Python 3.6 paths with these terminal commands:
which python3.6 # Gets the executable path python3.6 -c "import sysconfig; print(sysconfig.get_path('include'))" # Gets include dir python3.6 -c "import sysconfig; print(sysconfig.get_config_var('LIBDIR'))" # Gets library dir
Then plug those into your CMake command:
cmake \ -DPYTHON3_EXECUTABLE=/path/to/your/python3.6 \ -DPYTHON3_INCLUDE_DIR=/path/to/your/python3.6/include \ -DPYTHON3_LIBRARY=/path/to/your/python3.6/lib/libpython3.6m.dylib \ ..
Ensure OpenCV Was Built for Python 3.6
If you installed OpenCV via Homebrew, make sure you enabled Python 3 support:
brew install opencv3 --with-python3
If you built OpenCV from source, double-check you included Python 3.6 flags during the CMake step:
cmake \ -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules \ -DPYTHON3_EXECUTABLE=/path/to/python3.6 \ -DPYTHON3_INCLUDE_DIR=/path/to/python3.6/include \ -DPYTHON3_LIBRARY=/path/to/python3.6/lib/libpython3.6m.dylib \ -DPYTHON3_PACKAGES_PATH=/path/to/python3.6/site-packages \ ..
Fix macOS Sierra-Specific Linking Quirks
macOS Sierra has stricter dynamic library rules that often trip up OpenCV builds. Try these:
- Add macOS framework dependencies to your CMake target:
target_link_libraries(your_project_name_here ${Python3_LIBRARIES} ${OpenCV_LIBS} -framework CoreFoundation -framework Cocoa) - Ensure OpenCV’s libraries are in your dynamic library path when running
make:export DYLD_LIBRARY_PATH=/usr/local/lib:$DYLD_LIBRARY_PATH make -j4
Start Fresh with a Clean Build
Stale build files can cause weird, hard-to-debug errors. Wipe your build folder and start over:
rm -rf build mkdir build cd build # Run your corrected CMake command here make -j4
If you can share the exact error message from make, we can narrow this down even further—but these steps fix 90% of the fatal build errors I’ve seen with this setup.
内容的提问来源于stack exchange,提问作者Mertalp Tasdelen

