摘要
对于OpenCV环境,在CMake配置之后,我尝试了make
,但是发生了错误,说Python.h: No such file or directory
。
环境
我做了什么
首先,我尝试使用下面的一些选项进行CMake配置。$CONDA_HOME
等于/home/qcmp/anaconda3
。
(cv) (anaconda3-5.3.1/envs/cv) (base) qcmp@qcmp-Alienware-Aurora-R8:~/opencv/build$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules -D PYTHON3_EXECUTABLE=$CONDA_HOME/envs/cv/bin/python -D PYTHON3_PACKAGES_PATH=$CONDA_HOME/envs/cv/lib/python3.7/site-packages -D BUILD_opencv_python2=OFF -D BUILD_opencv_python3=ON -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF -D OPENCV_ENABLE_NONFREE=ON -D BUILD_EXAMPLES=ON -D ENABLE_PRECOMPILED_HEADERS=OFF -D BUILD_TESTS=OFF -D PYTHON3_LIBRARY=/home/qcmp/anaconda3/lib/libpython3.7m.so -D PYTHON3_INCLUDE_DIR=/home/qcmp/anaconda3/include/python3.7m -D WITH_CUBLAS=OFF -D OpenBLAS_FOUND=OFF ..
输出为here。有些警告类似Could not find OpenBLAS include
,但可能与make
问题(?)
此后,当make -j8
时,发生如下错误:
/home/qcmp/opencv/modules/python/src2/cv2.cpp:9:10: fatal error: Python.h: No such file or directory
#include <Python.h>
^~~~~~~~~~
compilation terminated.
modules/python3/CMakeFiles/opencv_python3.dir/build.make:62: recipe for target 'modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.o' failed
make[2]: *** [modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.o] Error 1
CMakeFiles/Makefile2:12152: recipe for target 'modules/python3/CMakeFiles/opencv_python3.dir/all' failed
make[1]: *** [modules/python3/CMakeFiles/opencv_python3.dir/all] Error 2
该错误表明找不到Python.h。根据{{3}},在运行cmake
时,我附加了用于添加路径的选项,例如-D PYTHON3_LIBRARY=/home/qcmp/anaconda3/lib/libpython3.7m.so -D PYTHON3_INCLUDE_DIR=/home/qcmp/anaconda3/include/python3.7m
。实际上,home/qcmp/anaconda3/include/python3.7m/Python.h
中确实存在一个Python.h文件。
根据this link,我已经尝试过sudo apt-get install python3.7-dev
和sudo apt-get install python3-dev
,但无法解决问题。
该如何解决?
已编辑
我将cmake选项从设置文件路径更改为搜索一个。
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=$(python -c "import sys; print(sys.prefix)") -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules -D PYTHON3_INCLUDE_DIR=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") -D PYTHON3_EXECUTABLE=$(which python) -D PYTHON3_PACKAGES_PATH=$(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") -D PYTHON3_LIBRARY=$(python -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR') + '/' + sysconfig.get_config_var('LDLIBRARY'))") -D BUILD_opencv_python2=OFF -D BUILD_opencv_python3=ON -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF -D OPENCV_ENABLE_NONFREE=ON -D BUILD_EXAMPLES=ON ..
搜索参考文献如下。
$ which python
/home/qcmp/.pyenv/shims/python
$ python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())"
/home/qcmp/.pyenv/versions/anaconda3-5.3.1/envs/cv/include/python3.7m
$ python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"
/home/qcmp/.pyenv/versions/anaconda3-5.3.1/envs/cv/lib/python3.7/site-packages
$ python -c "import sys; print(sys.prefix)"
/home/qcmp/.pyenv/versions/anaconda3-5.3.1/envs/cv
$ python -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR') + '/' + sysconfig.get_config_var('LDLIBRARY'))"
/home/qcmp/.pyenv/versions/anaconda3-5.3.1/envs/cv/lib/libpython3.7m.so
但是,我仍然遇到相同的错误。