CMake不创建.pyd而不是.dll文件,为什么会这样?

ulmd4ohb  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(95)

我目前正在尝试使用pybind 11将我在Windows 10机器上的C++代码连接到我的Python代码。但是当我打开cmake-gui时,它成功编译了,但是没有创建预期的.pyd文件。
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)

在我的build目录中,它创建了以下文件:

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输出:

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)

在我的子项目目录中,我有以下文件:

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

我怀疑问题可能出在我的主.cpp文件中。这里:

PYBIND11_MODULE(Scoring, m) {
    m.def("montecarlo", &montecarlo);
}

VS Code将其标记为红色,当我将鼠标悬停在它上面时,它会显示以下内容:no instance of function template "pybind11::module_::def" matches the argument list
整个代码的其余部分没有任何错误。也许问题是因为我通过git clone(在我的项目目录中)和pip分别“安装”了pybind 11。这可能会引起一些愤怒。
我对C++真的很陌生,以前从未使用过pybind 11,所以我很感激任何帮助。提前谢谢你。

jei2mxaa

jei2mxaa1#

我用cppimport解决了这个问题。为了解决VS代码中的错误,我忘记更改Scoring. h文件中的一些行,其中涉及我重写的函数,现在它可以工作了。

相关问题