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

MSYS2+MinGW环境下CMake配置Pybind11遇编译报错求助

MSYS2环境下CMake结合Pybind11的编译错误解决与疑问解答

问题背景

已在MSYS2中通过pacman -S mingw-w64-x86_64-pybind11完成pybind11安装,工作目录包含官方教程的example.cpp及如下CMakeLists.txt:

# Nous voulons un cmake "récent" pour utiliser les dernières fonctionnalités
cmake_minimum_required(VERSION 3.3)

# Notre projet est étiqueté hello
project(example)

find_package(Python COMPONENTS Interpreter Development)
find_package(pybind11 CONFIG)

# pybind11 method:
pybind11_add_module(MyModule1 example.cpp)

# Python method:
Python_add_library(MyModule2 example.cpp)
target_link_libraries(MyModule2 pybind11::headers)
set_target_properties(MyModule2 PROPERTIES
                                INTERPROCEDURAL_OPTIMIZATION ON
                                CXX_VISIBILITY_PRESET ON
                                VISIBILITY_INLINES_HIDDEN ON)

执行mkdir mingwbuild && cd mingwbuild && cmake .. -G "MinGW Makefiles"时触发错误。

错误详情

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 Python: D:/Software/Python/python.exe (found version "3.11.1") found components: Interpreter Development Development.Module Development.Embed
-- pybind11::lto disabled (problems with undefined symbols for MinGW for now)
-- pybind11::thin_lto disabled (problems with undefined symbols for MinGW for now)
-- Found pybind11: D:/Software/MSYS2/mingw64/include (found version "2.11.1")
CMake Error at CMakeLists.txt:15 (target_link_libraries):
  The keyword signature for target_link_libraries has already been used with
  the target "MyModule2".  All uses of target_link_libraries with a target
  must be either all-keyword or all-plain.

  The uses of the keyword signature are here:

   * D:/Software/MSYS2/mingw64/share/cmake/Modules/FindPython/Support.cmake:3400 (target_link_libraries)

-- Configuring incomplete, errors occurred!
See also "D:/Informatik/NachhilfeInfoUni/KadalaSchmittC++/PythonBindC++/buildmingw3/CMakeFiles/CMakeOutput.log".
See also "D:/Informatik/NachhilfeInfoUni/KadalaSchmittC++/PythonBindC++/buildmingw3/CMakeFiles/CMakeError.log".

解决方案

错误原因:Python_add_library内部已使用带关键字(如PRIVATE/PUBLIC)的target_link_libraries调用,而我们的代码中用了无关键字的形式,CMake要求同一个目标的所有target_link_libraries调用必须统一格式。

修改CMakeLists.txt中对应行,添加关键字PRIVATE(因为pybind11::headers仅为编译时头文件依赖):

target_link_libraries(MyModule2 PRIVATE pybind11::headers)

2. 简化方案(推荐)

如果仅需生成可被Python调用的模块,直接使用pybind11提供的pybind11_add_module即可,这是最简便且不易出错的方式。可删除CMakeLists中第二种方法的代码,保留如下内容:

cmake_minimum_required(VERSION 3.3)
project(example)

find_package(Python COMPONENTS Interpreter Development)
find_package(pybind11 CONFIG)

pybind11_add_module(MyModule1 example.cpp)

关于Python侧安装pybind11的疑问

不需要通过pip install pybind11在Python环境安装。pybind11是编译时依赖,仅在编译C++模块时需要;运行时Python只需加载编译生成的.pyd(Windows)或类.so(MSYS2下可能为.dll)文件即可调用模块中的函数,无需pybind11包。

内容的提问来源于stack exchange,提问作者flori10

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 18:21:11