使用 Code::Blocks 构建 OpenCV.cbp 时出现 Python.h 问题

Issue with Python.h while building OpenCV.cbp with Code::Blocks

我想安装 Opencv 并按照本教程进行操作:https://medium.com/@sourabhjigjinni/install-opencv-4-0-0-for-c-windows-7-10-code-blocks-tdm-gcc-64-dff65addf162

我一步一步跟着它,用代码块构建它,但是在大约 99% 构建时我得到了错误: 致命错误:Python.h:没有这样的文件或目录。

PC:Windows 10、64 位,python3 已安装。

我在编译器的 "search directories" 部分添加了 python 包含目录的完整路径。 在使用 Cmake 生成 Makefile 时,我检查了 python.exe 和包含目录的路径是否已填充。 我也查看了很多论坛,但从未找到令人满意的答案...

[ 99%] Building CXX object modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.obj
cd /d C:\opencv\build\modules\python3 && C:\TDM-GCC-64\bin\g++.exe  -DCVAPI_EXPORTS -D_USE_MATH_DEFINES -D__OPENCV_BUILD=1 -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS @CMakeFiles/opencv_python3.dir/includes_CXX.rsp -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -Wno-unused-function -Wno-deprecated-declarations -Wno-overloaded-virtual -Wno-undef -O3 -DNDEBUG  -DNDEBUG   -std=c++11 -o CMakeFiles\opencv_python3.dir\__\src2\cv2.cpp.obj -c C:\opencv\source\opencv-4.1.0\modules\python\src2\cv2.cpp

C:\opencv\source\opencv-4.1.0\modules\python\src2\cv2.cpp:9:20: fatal error: Python.h: No such file or directory

compilation terminated.
mingw32-make.exe[2]: *** [modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.obj] Error 1
mingw32-make.exe[1]: *** [modules/python3/CMakeFiles/opencv_python3.dir/all] Error 2
mingw32-make.exe: *** [all] Error 2
modules\python3\CMakeFiles\opencv_python3.dir\build.make:62: recipe for target 'modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.obj' failed
mingw32-make.exe[2]: Leaving directory 'C:/opencv/build'
CMakeFiles\Makefile2:3512: recipe for target 'modules/python3/CMakeFiles/opencv_python3.dir/all' failed
mingw32-make.exe[1]: Leaving directory 'C:/opencv/build'
C:/opencv/build/Makefile:161: recipe for target 'all' failed
Process terminated with status 2 (0 minute(s), 37 second(s))
4 error(s), 0 warning(s) (0 minute(s), 37 second(s))

好吧,我尽我所能让编译器知道 Python.h 在哪里,但它似乎没有用。如果您需要更多数据,请告诉我。

似乎Cmake在@Cmakefiles/opencv_python.dir.

目录下找不到Python.h头文件

要尝试的事情:

  • 您可能需要尝试找到如何在Cmake界面中设置@Cmakefiles/opencv_python.dir标志,或者在CMakeLists.txt中手动设置,并将其设置为正确的[=10] =] 位置.

  • 此外,确保 Cmakes Python 包含路径标志是正确的 (PYTHON_INCLUDE=/path/to/python/include/)。

  • 最后,请确保您的 Python 路径设置正确,方法是按照此 post here 和您的 Python 位置。

编辑: 我能够按照说明进行操作并获得网络摄像头演示来构建和 运行。这是在 Windows 10 和 Python 3.7.1 上测试的。不确定在完全重新安装之前要尝试什么。如果您有任何问题或想了解具体信息,请告诉我,如果我想到什么可以尝试,我会编辑此 post。祝你好运。

好的,我从头开始重建(删除了 opencv/build 文件夹的内容 + re-generated CMakeFiles with Cmake)并且它自己构建没有任何错误。 我的错 : - 我第一次构建 OpenCV.cbp 时,我的 python 包含目录路径未在 code::blocks 中的 "search directories" 中指定。 - 在几次不成功的构建之后,我更改了我的 python 版本,并在 Cmake 中实现了所有 python 路径,并将包含目录路径添加到 code::blocks

问题:OpenCV.cbp 没有考虑到所有的变化,即使重建它(我不知道为什么),并一直要求 Python.h

解决问题: 在构建 OpenCV.cbp 之前,确保 Cmake 中指定的 python 路径是正确的,并将 python 包含目录添加到 code::blocks "search directories",这不是我的情况. 然后,构建 !