Use PyBind11 with CMake and MSYS2 and MinGW

I want to use Pybind11 together with CMake and MSYS2.

I installed pybind11 with the command

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>pacman -S mingw-w64-x86_64-pybind11
</code>
<code>pacman -S mingw-w64-x86_64-pybind11 </code>
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#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");
}
</code>
<code>#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"); } </code>
#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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># 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 ()
</code>
<code># 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 () </code>
# 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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>mkdir mingwbuild
cd mingwbuild
cmake .. -G "MinGW Makefiles"
</code>
<code>mkdir mingwbuild cd mingwbuild cmake .. -G "MinGW Makefiles" </code>
mkdir mingwbuild
cd mingwbuild
cmake .. -G "MinGW Makefiles"

and I get the error:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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".
</code>
<code>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". </code>
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>pacman -S mingw-w64-x86_64-pybind11
</code>
<code>pacman -S mingw-w64-x86_64-pybind11 </code>
pacman -S mingw-w64-x86_64-pybind11

And I am not sure, whether an executable should be created.
Or whether I need to write

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>add_library(example ${SRCS})
</code>
<code>add_library(example ${SRCS}) </code>
add_library(example ${SRCS})

instead of

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>add_executable(example ${SRCS})
</code>
<code>add_executable(example ${SRCS}) </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import example
print( example.add(3,4))
</code>
<code>import example print( example.add(3,4)) </code>
import example
print( example.add(3,4))

Not sure whether I need to install anything on the Python side with Pip
like

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>pip install pybind11
</code>
<code>pip install pybind11 </code>
pip install pybind11

Any help appreciated!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật