在网上找了一些配置方法,最简单直接的是在cmake中加入如下语句:
set(PYTHON_EXECUTABLE /Python/Python311/python.exe)
include_directories("/PythonPython311/include")
link_directories("/PythonPython311/libs")
link_libraries(python3.lib)
link_libraries(python311.lib)
直接将电脑上安装的python路径加入进去,这样的话是可以编译运行Qt程序,但是当需要打包Qt程序到其他电脑上时,就会报错,找不到python.h。因此,再用另一种方法,将cmake中的语句修改为:
find_package(PythonLibs 3.11.0 EXACT REQUIRED)
set(PYTHON_EXECUTABLE /Python/Python311/python.exe)
include_directories(${PYTHON_INCLUDE_DIRS})
link_directories(${PYTHON_LIBRARIES})
link_libraries(python3.lib)
link_libraries(python311.lib)
通过find_package查找python路径,将对应路径保存到变量PYTHON_INCLUDE_DIRS和PYTHON_LIBRARIES中,然后通过取这两个变量的值来实现python路径的添加,但是这样有的时候也会出现find_package查找的路径不对或者查不到的问题,因此最终通过如下方法来实现:将python库文件拷贝到Qt项目的与exe文件同级目录下,修改cmake代码如下:
set(PYTHON_EXECUTABLE /Python/Python311/python.exe)
include_directories(${CMAKE_BINARY_DIR}/Python/Python311/include)
link_directories(${CMAKE_BINARY_DIR}/Python/Python311/libs)
link_libraries(python3.lib)
link_libraries(python311.lib)
其中变量CMAKE_BINARY_DIR代表可执行文件的同级目录,此时在打包程序时,将python库文件拷贝到安装包的与exe文件同级目录(即与Qt6Core.dll等Qt的dll文件的同级目录)下即可。
补充:cmake中,CMAKE_BINARY_DIR代表可执行文件的同级目录,CMAKE_SOURCE_DIR代表源码路径(即与main.cpp的同级目录)。
标签:Cmake,Qt,lib,python,directories,libraries,PYTHON,link From: https://www.cnblogs.com/Gregg/p/18330314