pybind11 icon indicating copy to clipboard operation
pybind11 copied to clipboard

[BUG]: 'Unknown CMake command "python3_add_library" in pybind11NewTools.cmake

Open stridera opened this issue 3 years ago • 6 comments

Required prerequisites

  • [X] Make sure you've read the documentation. Your issue may be addressed there.
  • [X] Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
  • [X] Consider asking first in the Gitter chat room or in a Discussion.

Problem description

Attempting to build throws an error in tools/pybind11NewTools.cmake throws the following error: Unknown CMake command "python3_add_library".

Adding find_package (Python3 COMPONENTS Interpreter Development) fixes it. image

Reproducible example code

src/cmakelists.txt

configure_file(${CMAKE_SOURCE_DIR}/third_party/pybind11/CMakeLists.txt ${CMAKE_BINARY_DIR}/external-projects/pybind11/CMakeLists.txt)
execute_process(COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" .
    WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/external-projects/pybind11"
)
execute_process(COMMAND "${CMAKE_COMMAND}" --build .
    WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/external-projects/pybind11"
)

# Add pybind11 makefile
add_subdirectory("${CMAKE_BINARY_DIR}/third_party/pybind11"
                 "${CMAKE_BINARY_DIR}/third_party/pybind11"
                 EXCLUDE_FROM_ALL
)

set(PYBIND11_CPP_STANDARD -std=c++11)
# Force Pybind11 not to share resources with other pybind modules.
# With this definition we force the ABI version to be unique and not risk crashes on different modules.
# (workaround for RS5-10582; see also https://github.com/pybind/pybind11/issues/2898)
add_definitions(-DPYBIND11_COMPILER_TYPE="_librs_abi")
include_directories(${CMAKE_BINARY_DIR}/third_party/pybind11/include)

pybind11_add_module(${PROJECT_NAME} ${SOURCE_FILES} ${HEADER_FILES} ${PYBIND_FILES})

/third_party/pybind11/

#  minimum required cmake version: 3.1.0
cmake_minimum_required(VERSION 3.1.0)

project(pybind11-download NONE)

include(ExternalProject)
ExternalProject_Add(
        pybind11
        PREFIX .
        GIT_REPOSITORY "https://github.com/pybind/pybind11.git"
        GIT_TAG "914c06fb252b6cc3727d0eedab6736e88a3fcb01" # 2.9.2
        SOURCE_DIR "${CMAKE_BINARY_DIR}/third_party/pybind11"
        # Override default steps with no action, we just want the clone step.
        CONFIGURE_COMMAND ""
        BUILD_COMMAND ""
        INSTALL_COMMAND ""
)

stridera avatar Jun 06 '22 20:06 stridera

But this is already at https://github.com/pybind/pybind11/blob/0964a9093a728dbf67b9b98c6371752b1a346f25/tools/pybind11NewTools.cmake#L35 ?

henryiii avatar Jun 20 '22 14:06 henryiii

Yeah, I noticed. I"m not a master at cmake files so I didn't just do a pull request, but I know that I'm getting the Unknown CMake command "python3_add_library". in my project until I add that line in my own branch. My guess is because it's inside the function, it's not calling any of the lines outside that function.

stridera avatar Jun 20 '22 20:06 stridera

Using windows and python 3.11 i seem to have a similar issue. python3_add_library is not found. The proposed solution here gets me a little farther, does not solve it yet though (different error).

BeatWolf avatar Nov 19 '22 18:11 BeatWolf

Are there any new insights on this? I just ran into the same error after upgrading my build environment from Ubuntu 20.04 to 22.04. So I guess it is related to the Python and/or CMake version.

luator avatar Aug 23 '23 08:08 luator

I found some workaround: In my case the error was triggered because I did find_package(pybind11) after an other package that apparently also searches for Python (Python3_FOUND was true) but probably in an incompatible way. Simply moving pybind11 to the top of the find_package-list fixed it for me.

luator avatar Aug 23 '23 11:08 luator

If you are searching for Python multiple times, you might want to set Python_ARTIFACTS_INTERACTIVE. Otherwise, FindPython will search every single time, and can even find different Python versions.

henryiii avatar Aug 23 '23 14:08 henryiii