Evaluate all macros in a C++ header file

后端 未结 3 1697
陌清茗
陌清茗 2021-01-23 11:36

I have a requirement to build an automated system to parse a C++ .h file with a lot of #define statements in it and do something with the value that each #def

3条回答
  •  攒了一身酷
    2021-01-23 12:19

    Can you use g++ or gcc with the -E option, and work with that output?

    -E Stop after the preprocessing stage; do not run the compiler proper. The output is in the form of preprocessed source code, which is sent to the standard output. Input files which don't require preprocessing are ignored.

    With this, I imagine:

    1. Create the list of all #define keys from the source
    2. Run the appropriate command below against the source file(s), and let the GNU preprocessor do its thing
    3. Grab the preprocessed result from stdout, filter to take only those in integer form, and output it to however you want to represent key/value pairs

    One of these two commands:

    gcc -E myFile.c
    g++ -E myFile.cpp
    

    https://gcc.gnu.org/onlinedocs/gcc-2.95.2/gcc_2.html https://gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html

提交回复
热议问题