Find Python by Specific Version using CMake

Find Python by Specific Version using CMake

CMake, a cross-platform build system, plays an important role in managing the build process of various projects. When it comes to projects involving Python integration, specifying a particular Python version is often essential. This tutorial demonstrates how to find the Python by specific version using CMake.

CMake allows finding the Python using the find_package command. To target a specific Python version, use the EXACT keyword along with the desired version number.

For example, the following CMakeLists.txt file tries to find Python version 3.10 and locate the necessary development components. The REQUIRED keyword ensures that the build process stops if Python of the specified version is not found.

cmake_minimum_required(VERSION 3.27)
project(myapp)

set(CMAKE_CXX_STANDARD 17)

find_package(Python3 3.10 EXACT COMPONENTS Development REQUIRED)

add_executable(${PROJECT_NAME} main.cpp)

target_link_libraries(${PROJECT_NAME} Python3::Python)

Leave a Comment

Cancel reply

Your email address will not be published.