-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Description
Required prerequisites
- Make sure you've read the documentation. Your issue may be addressed there.
- Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
- Consider asking first in the Gitter chat room or in a Discussion.
Problem description
When installing python for windows, you have the option of installing the debug binaries as well.
If you then find_package(Python)
followed by find_package(pybind11)
it will cause the following linker error:
[build] LINK : fatal error LNK1104: cannot open file 'python310.lib'
If you then search the link command line, I cannot find any mention of python310.lib
, only "C:/Program Files/Python310/libs/python310_d.lib"
.
The find_package(Python)
finds C:/Program Files/Python310/libs/python310_d.lib
The find_package(pybind11)
finds C:/Program Files/Python310/libs/python310.lib
If I put the find_package(pybind11)
first and don't link against Python3::Python
, it seems to work correctly. This is contrary to the pybind11 documentation, however.
If I don't install the debug binaries then everything works correctly, and while I don't need the debug binaries, I would hope that their presence wouldn't lead to a linker error.
Something interesting I found is that pybind11 detects the python library by running PYTHON_EXECUTABLE
and parsing the output. However, when you run find_package(Python)
on a debug build, you get the release interpreter and the debug libraries, which I think is contributing to this issue.
Another interesting fact is that this bug only happens if you include one of pybind11's header files. If you just include Python.h, then it does not generate the linker error. (Which makes me feel like I'm going crazy)
Thank you for your time, I hope I've included enough information to be helpful!
Reproducible example code
* Download and install python from https://www.python.org/downloads/windows/
* Check `Download debug binaries`
CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 3.19)
PROJECT(Example)
FIND_PACKAGE(Python3 COMPONENTS Interpreter Development)
FIND_PACKAGE(pybind11 CONFIG)
ADD_EXECUTABLE(Example Main.cpp)
TARGET_LINK_LIBRARIES(Example PUBLIC Python3::Python pybind11::headers)
Main.cpp
#include <pybind11/embed.h>
namespace py = pybind11;
int main(int argc, char ** argv)
{
py::scoped_interpreter guard{};
py::print("Hello from pybind11");
return 0;
}