I want to use Pybind11 together with CMake and MSYS2.
I installed pybind11 with the command
pacman -S mingw-w64-x86_64-pybind11
on MSYS2.
Then I created the two files example.cpp and CMakeLists.txt in the same directory (my working directory).
example.cpp is copied from this tutoriel
#include <pybind11/pybind11.h>
int add(int i, int j) {
return i + j;
}
PYBIND11_MODULE(example, m) {
m.doc() = "pybind11 example plugin"; // optional module docstring
m.def("add", &add, "A function that adds two numbers");
}
My CMakeLists.txt file looks like this:
# Nous voulons un cmake "récent" pour utiliser les dernières fonctionnalités
cmake_minimum_required(VERSION 3.0)
# Notre projet est étiqueté hello
project(example)
# Crée des variables avec les fichiers à compiler
set(SRCS
example.cpp
)
# Notre exécutable
add_executable(example ${SRCS})
# Recherche la dépendance externe
find_package(pybind11 REQUIRED)
if (pybind11_FOUND)
# Une fois la dépendance trouvée, nous l'incluons au projet
target_include_directories(example PUBLIC ${pybind11_INCLUDE_DIR})
target_link_libraries (example ${pybind11_LIBRARY})
else ()
# Sinon, nous affichons un message
message(FATAL_ERROR "pybind11 not found")
endif ()
I have created this CMakeLists.txt file with the help of this tutorial
Then I ran the commands:
mkdir mingwbuild
cd mingwbuild
cmake .. -G "MinGW Makefiles"
and I get the error:
cmake .. -G "MinGW Makefiles"
-- The C compiler identification is GNU 11.3.0
-- The CXX compiler identification is GNU 11.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: D:/Software/MSYS2/usr/bin/cc.exe
-- Check for working C compiler: D:/Software/MSYS2/usr/bin/cc.exe - works
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - failed
-- Check for working CXX compiler: D:/Software/MSYS2/usr/bin/c++.exe
-- Check for working CXX compiler: D:/Software/MSYS2/usr/bin/c++.exe - works
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PythonInterp: D:/Software/MSYS2/mingw64/bin/python3.exe (found suitable version "3.10.9", minimum required is "3.6")
CMake Error at D:/Software/MSYS2/mingw64/share/cmake/pybind11/FindPythonLibsNew.cmake:242 (message):
Python libraries not found
Call Stack (most recent call first):
D:/Software/MSYS2/mingw64/share/cmake/pybind11/pybind11Tools.cmake:50 (find_package)
D:/Software/MSYS2/mingw64/share/cmake/pybind11/pybind11Common.cmake:188 (include)
D:/Software/MSYS2/mingw64/share/cmake/pybind11/pybind11Config.cmake:250 (include)
CMakeLists.txt:16 (find_package)
-- Configuring incomplete, errors occurred!
See also "D:/Informatik/NachhilfeInfoUni/KadalaSchmittC++/PythonBindC++/buildmingw/CMakeFiles/CMakeOutput.log".
See also "D:/Informatik/NachhilfeInfoUni/KadalaSchmittC++/PythonBindC++/buildmingw/CMakeFiles/CMakeError.log".
So why is the pybind11 library not found, although I have installed it with
pacman -S mingw-w64-x86_64-pybind11
And I am not sure, whether an executable should be created.
Or whether I need to write
add_library(example ${SRCS})
instead of
add_executable(example ${SRCS})
in CMakeLists.txt
I am expecting some file like example.exe should be created, and then I just want to go to the directory of example.exe and run a python script like this one:
import example
print( example.add(3,4))
Not sure whether I need to install anything on the Python side with Pip
like
pip install pybind11
Any help appreciated!