pybind / pybind/pybind11

Use #pragma push_macro to properly restore the _DEBUG macro

Open
#1,713 3 comments 0 reactions 1 assignee View on GitHub

@henryiii is already working on this.

Since Sep 16, 2020.

build system
Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

Issue description

The common.h header uses #undef _DEBUG before including the Python headers (I don't know why, but I guess it's important).
When it later restores the macro it uses #define _DEBUG, which may be different from the original definition of the macro that happens to be 1 for example.
As a result subsequent code that expects _DEBUG to have a value will not compile.
I ran into this when including tbb.h (Threading Building Blocks) after pybind11.h, the TBB headers do expect _DEBUG to have a value.
This can be fixed by using #pragma push_macro("_DEBUG") before the #undef and #pragma pop_macro("_DEBUG") in place of the #define.
The push_macro pragma is a VC++ feature, not sure if other compilers have it, but this is done just for _MSC_VER anyway.

Reproducible example code

#include "pybind11/pybind11.h"

#if _DEBUG
#pragma message( "debug" )
#endif

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.