Visual Studio + CMake製作Python Extension

版本資訊
Visual Studio 2019
CMake 3.24
Python3.8.10

設定Opencv

  • 編譯OpenCV

  • 設定windows的環境變數 OpenCV_ROOT 到OpenCVConfig.cmake所在的資料夾(CMake 3.12之後的功能,文件

  • 從編譯好的OpenCVConfig.cmake我們可以看OpenCV提供了那些CMake變數給我們使用,可以參考這裡

  • 讓CMake複製dll文件

  • 設定CMake尋找Python.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
find_package(Python 3 REQUIRED 
COMPONENTS Interpreter Development.Module NumPy) # New in cmake 3.19

include_directories(${Python_INCLUDE_DIRS})
link_directories(${Python_LIBRARY_DIRS})
message(STATUS "Python Location: ${Python_INCLUDE_DIRS}")

include_directories(${Python_NumPy_INCLUDE_DIRS})
message(STATUS "NumPy Location: ${Python_NumPy_INCLUDE_DIRS}")

find_package(OpenCV REQUIRED)
if (OpenCV_FOUND)
# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
include_directories(${OpenCV_INCLUDE_DIRS})
link_libraries(${OpenCV_LIBRARIES})
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
else ()
message(FATAL_ERROR "Could not locate OpenCV")
endif()

原碼參考:

參考:
Python.h位置

CMake尋找Python