How to apply a C preprocessor only to certain (#if/#endif) directives?

后端 未结 4 1872
攒了一身酷
攒了一身酷 2021-01-16 04:27

I was wondering if it is possible, and if yes how, can I run a C preprocessor, like cpp, on a C++ source file and only process the conditional directives #

相关标签:
4条回答
  • 2021-01-16 04:56

    You can use g++ -E option to stop after preprocessing stage

    -E -> stop after the preprocessing stage.The output is in the form of preprocessed source code, which is sent to the standard output

    0 讨论(0)
  • 2021-01-16 04:59

    You might be looking for a tool like coan:

    Coan is a software engineering tool for analysing preprocessor-based configurations of C or C++ source code. Its principal use is to simplify a body of source code by eliminating any parts that are redundant with respect to a specified configuration.

    It's precisely designed to process #if and #ifdef preprocessor lines, and remove code accordingly, but it has a lot of other possible uses.

    0 讨论(0)
  • 2021-01-16 05:00

    The linux unifdef command does what you want:

    http://linux.die.net/man/1/unifdef

    Even if you're not on linux, there is source available on the web.

    BTW, this is a duplicate of another question: Way to omit undefined preprocessor branches by default with unifdef?

    0 讨论(0)
  • 2021-01-16 05:00

    Oh, this is the same task as I had in the past. I've tried cpp unifdef and coan tools - all of them stumbled upon special C# preprocessor things like #region. In the end I've decided to make my own one: https://github.com/gaDZella/undefine.

    The tool has a pretty simple set of options compared to the mentioned cpp tools but it is fully compatible with C# preprocessor syntax.

    0 讨论(0)
提交回复
热议问题