0

I am working on a pybind11 project, and hope both c++ and setup.py on the python can use a commonly defined macro or variable.

for example, in a c++ include file defined.h

#define THIS_NAME
#define THIS_NAME1 this_name

in python, setup.py

from setuptools import setup, Extension
from distutils.command.bdist import bdist as _bdist
from distutils.command.sdist import sdist as _sdist
import pybind11

# The following is for GCC compiler only.
cpp_args = []

a_name = THIS_NAME
if THIS_NAME1 == 'this_name':
   ...

Is this doable, and how to implement?

xycs
  • 121
  • 1
  • 8
  • 1
    What makes you think using MACRO's is a good idea in the first place? See this [answer](https://stackoverflow.com/questions/14041453/why-are-preprocessor-macros-evil-and-what-are-the-alternatives). Instead of using a macro define a typed constant and have a C++ function that returns that value so python can pick it up. – Pepijn Kramer Nov 16 '22 at 15:19
  • I am not limiting myself to use MACRO. Let me make it clear. The python file I refer to actually is the setup.py. – xycs Nov 16 '22 at 20:19
  • 1
    You would have to do something like `re.findall('#define (\w+) (\w+)', )`. But normally you would pass the defined variable from python to C++ by adding `-DTHIS_NAME=this_name` to the `extra_compile_args` argument of `Extension`. – ChrisD Nov 23 '22 at 06:00

0 Answers0