Reputation: 9
I am currently trying use pybind11 to connect my C++ code on my Windows 10 machine to my Python code. But when I open the cmake-gui it comiles successfully, but doesn't create the expected .pyd file.
CMakeLists.txt
cmake_minimum_required (VERSION 3.27.1)
project (equity_calculator)
enable_language(C)
enable_language(CXX)
set(pybind11_DIR C:/Python39/Lib/site-packages/pybind11/share/cmake/pybind11)
set(PYTHON_EXECUTABLE C:/Python39/python.exe)
set(pybind11_LIBRARIES C:/Python39/libs/python39.lib)
find_package(pybind11 CONFIG REQUIRED)
include_directories(${pybind11_INCLUDE_DIRS})
message([MAIN] "Found pybind11 v${pybind11_VERSION}: ${pybind11_INCLUDE_DIRS}")
MESSAGE( [Main] " pybind11_INCLUDE_DIRS = ${pybind11_INCLUDE_DIRS}")
MESSAGE( [Main] " pybind11_LIBRARIES = ${pybind11_LIBRARIES}")
#
# # Create an extension module
# add_library(mylib MODULE main.cpp)
# target_link_libraries(mylib pybind11::module)
#
# # Or embed the Python interpreter into an executable
# add_executable(myexe main.cpp)
# target_link_libraries(myexe pybind11::embed)
# method (1): generate `examplelib.pyd`
#pybind11_add_module(Scoring Scoring.cpp)
# method (2): generate `examplelib.dll` rename to `examplelib.pyd`
add_library(equity MODULE Scoring.cpp)
target_link_libraries(equity pybind11::module)
MESSAGE( [Main] " pybind11_INCLUDE_DIRS = ${pybind11_INCLUDE_DIRS}")
MESSAGE( [Main] " pybind11_LIBRARIES = ${pybind11_LIBRARIES}")
#add_executable(cpp_use_python cpp_use_python.cpp)
#target_link_libraries(cpp_use_python PRIVATE pybind11::embed)
In my build directory it creates the following files:
CMakeFiles
ALL_BUILD.vcxproj
ALL_BUILD.vcxproj.filters
cmake_install
CMakeCache
equity.vcxproj
equity.vcxproj.filters
equity_calculator.sln
ZERO_CHECK.vcxproj
ZERO_CHECK.vcxproj.filters
cmake-gui outputs that:
The C compiler identification is MSVC 19.35.32216.1
The CXX compiler identification is MSVC 19.35.32216.1
Detecting C compiler ABI info
Detecting C compiler ABI info - done
Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.35.32215/bin/Hostx64/x64/cl.exe - skipped
Detecting C compile features
Detecting C compile features - done
Detecting CXX compiler ABI info
Detecting CXX compiler ABI info - done
Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.35.32215/bin/Hostx64/x64/cl.exe - skipped
Detecting CXX compile features
Detecting CXX compile features - done
Found pybind11: C:/Python39/Lib/site-packages/pybind11/include (found version "2.11.1")
[MAIN]Found pybind11 v2.11.1: C:/Python39/Lib/site-packages/pybind11/include;C:/Python39/include
[Main] pybind11_INCLUDE_DIRS = C:/Python39/Lib/site-packages/pybind11/include;C:/Python39/include
[Main] pybind11_LIBRARIES = C:/Python39/libs/python39.lib
[Main] pybind11_INCLUDE_DIRS = C:/Python39/Lib/site-packages/pybind11/include;C:/Python39/include
[Main] pybind11_LIBRARIES = C:/Python39/libs/python39.lib
Configuring done (16.3s)
Generating done (0.1s)
In my sub-project directory I have the following files:
rendered.Scoring.cpp
.Scoring.cpp.swp
build
CMakeLists.txt
pybind11
readme.rst
Scoring.cpp
Scoring.h
setup.py (I have commented everything out, becuase I cloned this repo from someone and he was using a Mac)
test_montecarlo.py
test_monte_on_range.py
wrap.py
I suspect that the problem might be in my main .cpp file. Here:
PYBIND11_MODULE(Scoring, m) {
m.def("montecarlo", &montecarlo);
}
VS Code marks it red and when I hover my mouse over it, it says the follwin thing:
no instance of function template "pybind11::module_::def" matches the argument list
The entire rest of the code though doesn't have any errors though. Maybe the problem is because I have pybind11 "installed" through git clone (in my project directory) and through pip seperatly. Maybe this causes some iritation.
I am really new to C++ and have never used pybind11 before, so I would appreciate any help. Thank you in Advance.
Upvotes: -1
Views: 378
Reputation: 9
I have solved my issue by using cppimport. To solve the error from VS Code I had forgotten to change some line in the Scoring.h file which involved my rewritten function and now it works.
Upvotes: 0