2

I am import from c++ header file into python .py file.can i access the c++ header file #define into python

In python code,

import sys

sys.path.import

I am expected #define's value in running python code It get name error

1 Answer 1

4

You can't just import a C++ header into a Python program - they're different languages so it won't work. It's much more complicated than that, see Extending Python with C or C++.

If you just want to pick out #define's from a C++ header file could could simply open that file in your Python program and search for lines that begin with #define and parse the variable and value. Something like:

import re
defines = {}
with open("header_file.h") as header_file:
    for line in header_file.readlines():
        if line.startswith("#define"):
            line.rstrip()
            m = re.search('#define\s+([A-Za-z]\w+)\s+(.*)', line)
            if m:
                defines[m.group(1)] = m.group(2)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.