cmakefind_包Python

2024-05-15 23:13:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用不同版本的Python创建多个cMake目标。在我的cMakeLists中,我定义了Python_ROOT变量,并使用find_package(Python REQUIRED COMPONENTS Interpreter Development)获取python库路径、版本和链接选项

是否可以撤消find_包并再次调用find_package(..)?类似于代码剪下的吼声。现在,我看到了相同的python版本。 在最终版本中,用户应该在调用cMake时定义python路径列表

cmake_minimum_required (VERSION 3.10)

cmake_policy(SET CMP0048 NEW)   # set version string with project() command
cmake_policy(SET CMP0094 NEW)   # use LOCATION for Python lookup strategy
cmake_policy(SET CMP0074 NEW)

# Project 
project(Test
    VERSION 1.0.0 
)

set(Python_FIND_STRATEGY LOCATION)

# Python 3.5
set(Python_ROOT "<PATH to 3.5>" CACHE PATH "..." FORCE)

message("*****************************************")
message("Python_ROOT " ${Python_ROOT})
find_package(Python REQUIRED COMPONENTS Interpreter Development)

message("Python_FOUND " ${Python_FOUND})
message("Python_Interpreter_FOUND " ${Python_Interpreter_FOUND})
message("Python_Development_FOUND " ${Python_Development_FOUND})
   
message("Python_LIBRARIES " ${Python_LIBRARIES})
message("Python_LIBRARY_DIRS " ${Python_LIBRARY_DIRS})
message("Python_INCLUDE_DIRS " ${Python_INCLUDE_DIRS})
message("Python_LINK_OPTIONS " ${Python_LINK_OPTIONS})
message("Python_EXECUTABLE " ${Python_EXECUTABLE})
message("Python_INTERPRETER_ID " ${Python_INTERPRETER_ID})
  
message("Python_VERSION " ${Python_VERSION})
message("Python_VERSION_MAJOR " ${Python_VERSION_MAJOR})
message("Python_VERSION_MINOR " ${Python_VERSION_MINOR})
message("*****************************************")

# target A definition

# Python 2.7
set(Python_ROOT "<PATH to 2.7>" CACHE PATH "..." FORCE)

message("*****************************************")
message("Python_ROOT " ${Python_ROOT})
find_package(Python REQUIRED COMPONENTS Interpreter Development)

message("Python_FOUND " ${Python_FOUND})
message("Python_Interpreter_FOUND " ${Python_Interpreter_FOUND})
message("Python_Development_FOUND " ${Python_Development_FOUND})
   
message("Python_LIBRARIES " ${Python_LIBRARIES})
message("Python_LIBRARY_DIRS " ${Python_LIBRARY_DIRS})
message("Python_INCLUDE_DIRS " ${Python_INCLUDE_DIRS})
message("Python_LINK_OPTIONS " ${Python_LINK_OPTIONS})
message("Python_EXECUTABLE " ${Python_EXECUTABLE})
message("Python_INTERPRETER_ID " ${Python_INTERPRETER_ID})
  
message("Python_VERSION " ${Python_VERSION})
message("Python_VERSION_MAJOR " ${Python_VERSION_MAJOR})
message("Python_VERSION_MINOR " ${Python_VERSION_MINOR})
message("*****************************************")

# target B definition

编辑: 上述脚本的输出为:

-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.19042.
-- The C compiler identification is MSVC 19.28.29910.0
-- The CXX compiler identification is MSVC 19.28.29910.0
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
*****************************************
Python_ROOT C:/Users/schnudi/.edm/envs/py35
-- Found Python: C:/Users/schnudi/.edm/envs/py35/python.exe (found version "3.5.2") found components: Interpreter Development
Python_FOUND TRUE
Python_Interpreter_FOUND TRUE
Python_Development_FOUND TRUE
Python_LIBRARIES C:/Users/schnudi/.edm/envs/py35/libs/python35.lib
Python_LIBRARY_DIRS C:/Users/schnudi/.edm/envs/py35/libs
Python_INCLUDE_DIRS C:/Users/schnudi/.edm/envs/py35/include
Python_LINK_OPTIONS
Python_EXECUTABLE C:/Users/schnudi/.edm/envs/py35/python.exe
Python_INTERPRETER_ID Canopy
Python_VERSION 3.5.2
Python_VERSION_MAJOR 3
Python_VERSION_MINOR 5
*****************************************
*****************************************
Python_ROOT C:/Users/schnudi/.edm/envs/py27
CMake Error at C:/Program Files/CMake/share/cmake-3.16/Modules/FindPackageHandleStandardArgs.cmake:146 (message):
  Could NOT find Python (missing: Interpreter Development) (found version
  "3.5")
Call Stack (most recent call first):
  C:/Program Files/CMake/share/cmake-3.16/Modules/FindPackageHandleStandardArgs.cmake:393 (_FPHSA_FAILURE_MESSAGE)
  C:/Program Files/CMake/share/cmake-3.16/Modules/FindPython.cmake:347 (find_package_handle_standard_args)
  CMakeLists.txt:44 (find_package)


-- Configuring incomplete, errors occurred!
See also "C:/Users/schnudi/Desktop/temp/allinone/build/CMakeFiles/CMakeOutput.log".

Tags: cmakemessagecompilerversioncxxrootfindprogram