Skip to content

Debug build does not set -DPy_DEBUG with cmake #2018

@SimonHeybrock

Description

@SimonHeybrock

TLDR

I think the cmake variable PYTHON_IS_DEBUG is not visible inside the scope of the function pybind11_add_module.

What

Trying to create a debug build, using the debug version of Python with cmake:

  • -DCMAKE_BUILD_TYPE=Debug
  • -DPYTHON_EXECUTABLE=/usr/lib/python3.6-dgb

Build goes fine, but importing the resulting module gives an Import Error about the undefined symbol PyModule_Create2.

From what I have read, this indicates that we are linking correctly against the debug version of Python, but the includes are wrong, since PyModule_Create2 has a different name in the debug version of Python.

Solution?

  • # Python debug libraries expose slightly different objects
    # https://docs.python.org/3.6/c-api/intro.html#debugging-builds
    # https://stackoverflow.com/questions/39161202/how-to-work-around-missing-pymodule-create2-in-amd64-win-python35-d-lib
    if(PYTHON_IS_DEBUG)
    target_compile_definitions(${target_name} PRIVATE Py_DEBUG)
    endif()
    is not working as it should.
  • I have verified that PYTHON_IS_DEBUG is set correctly in https://github.com/pybind/pybind11/blob/master/tools/FindPythonLibsNew.cmake#L115, and it is also correct in pybind11Tools.cmake (outside the function scope)...
  • ... but not when the function is called.

I am having trouble understanding cmake's scoping rules. If I set PYTHON_IS_DEBUG in the cmake CACHE it works. I cannot tell whether this is the correct solution though?

Versions

  • cmake 3.10.2
  • pybind11 2.4.3
  • python3.6-dbg
  • Ubuntu

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions