You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows10下使用pybind11+CMake编译未生成.pyd文件的问题求助

Windows10下使用pybind11+CMake编译未生成.pyd文件的问题求助

各位大佬好,我刚接触C和pybind11,现在在Windows10上尝试用pybind11把我的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(通过git克隆到项目里的)
  • readme.rst
  • Scoring.cpp
  • Scoring.h
  • setup.py(已经全部注释掉了,因为原项目是Mac环境的)
  • test_montecarlo.py
  • test_monte_on_range.py
  • wrap.py

我怀疑的问题点

  1. 可能是我的C++绑定代码有问题:在Scoring.cpp里的绑定代码VS Code标红了,鼠标悬停提示no instance of function template "pybind11::module_::def" matches the argument list,但代码其他部分都没有错误提示。绑定代码如下:
PYBIND11_MODULE(Scoring, m) {
    m.def("montecarlo", &montecarlo);
}
  1. 另外我同时通过git克隆了pybind11到项目目录,又用pip安装了pybind11,会不会是这两个版本冲突导致的问题?

希望有经验的大佬能给我指点一下,谢谢大家!

备注:内容来源于stack exchange,提问作者Alex Konopatski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.21 15:48:12