0

I'm searching for a preprocessor which allows for partial preprocessing of C source files. What I want to do is to skip certain or all #include and to define some macros on the command line (or a separate file), the preprocessor then should only process what I specified and ignore the rest.

Here is an example of what I want to do:

#define FOO  
#ifdef FOO  
/* FOO */  
#endif  
#ifdef BAR  
/* BAR */  
#endif  

should be translated into

/* FOO */  
#ifdef BAR  
/* BAR */  
#endif

Some time ago at my previous job I needed a similar preprocessor and I think that I found a link to a standalone preprocessor here on stackoverflow, however after an hour of searching the web I gave up.

  • C != C++. Usually it's right to tag with only one. – tambre Jul 21 '17 at 07:44
  • Have not noticed any wars. I am C & C++. But the object programming is the future. – 0___________ Jul 21 '17 at 07:45
  • You can do this already. You need the `#if`, `#ifdef`, `#else` and `#endif` preprocessor directives. Google "conditional compilation c". You can define symbols on the command line when you compile: `gcc -DSomeSymbol ...`. – Jabberwocky Jul 21 '17 at 07:46
  • 1
    @Ron actually this question applies for both c and c++. – Jabberwocky Jul 21 '17 at 07:48
  • 2
    @MichaelWalz If he only hadn't titled "...C Files"... – Scheff's Cat Jul 21 '17 at 07:49
  • @Scheff you've got me.... – Jabberwocky Jul 21 '17 at 07:50
  • May be, it's worth to mention that any C (and C++) compiler has an option to do pre-processing only. For gcc it is `-E` (but it may differ for other "vendors"). On one hand, pre-processor can be applied for non-C (and non-C++) files. (I once used it to patch HTML files.) On the other hand, it allows you to see sources after pre-processing. (Sometimes this is surprising because an API may provide macros you didn't recognize as such. I once had horrific behavior in my application until I found out that a lot Win32 functions are hidden in macros to switch between ANSI and wide char versions.) – Scheff's Cat Jul 21 '17 at 07:57
  • 2
    @PeterJ "*But the object programming is the future.*" -- what's *the object programming*? If you mean OOP, this can be applied (and **is** applied) in C as well, whenever it makes sense. It was "*the future*" around 30 years ago. –  Jul 21 '17 at 07:59
  • @ron Why not? See [glib](https://developer.gnome.org/glib/stable/) for an example... – Scheff's Cat Jul 21 '17 at 08:02
  • @Ron [one of my own examples](https://github.com/Zirias/pocas/blob/master/include/pocas/core/list.h) (this is the header for a generic `List`) –  Jul 21 '17 at 08:04
  • @Felix Palmen C does not have language mechanisms for OOP. Of course at the end everything is translated to the machine code. Using your logic assembler (understand as a human readable version of the machine code - for all terminology fanatics) is the language where (paraphrasing you): _'OOP, this can be applied (and is applied) in assembler as well, whenever it makes sense'_ – 0___________ Jul 21 '17 at 08:07
  • @FelixPalmen I was under the impression that OOP requires a language to have facilities like classes and objects. – Ron Jul 21 '17 at 08:10
  • @PeterJ you could certainly do it for *some* CPUs using a horrible mess of macros. Nobody in his right mind would attempt that. C on the other hand gives you a notion of *functions*, *structured data* and pointers to both. That's all that's really needed for OOP. BTW, this discussion is off-topic here, so please just use Google for some resources on OOP in C. –  Jul 21 '17 at 08:10
  • 1
    @Ron I remember roughly a statement of B. Stroustrup where he distinguishs languages which _support_ a certain paradigm/feature in opposition to other languages which _allow_ it. – Scheff's Cat Jul 21 '17 at 08:12
  • @Scheff very good point. That is the reason why people if they prefer OOP choose C++, and those who do not - C. – 0___________ Jul 21 '17 at 08:16
  • Ha! Found it: "A language _supports_ a programming style if it provides facilities that make it convenient (reasonable easy, safe, efficient) to use that style. A language does not support a technique if it takes exceptional effort or skill to write such programs; in that case the language merely _enables_ programmers to use the technique." (B. Stroustrup) – Scheff's Cat Jul 21 '17 at 08:18
  • @PeterJ wrong again. The people developing glib and gtk clearly prefer OOP (which is the only reasonable style for a GUI lib like gtk anyways) and still pick C. There's no need to argue about the languages, everyone has his personal reasons for his choice and often the choice depends on the project. –  Jul 21 '17 at 08:24
  • 1
    @Felix Palmen - you have to have the last word. I am not going to argue. OOP means nowadays much more than only object creation, lists and functions pointers in it. Virtualisation polymorphism, templates and much much more is not easy to archive in Cc – 0___________ Jul 21 '17 at 08:34
  • 1
    This is a nice discussion about OOP, C, and C++. To sad, that it has rather nothing to do with the OP... ;-) – Scheff's Cat Jul 21 '17 at 08:36
  • @PeterJ you contradict yourself, you **are** arguing. Templates are not part of OOP, nowadays inheritance is often considered a bad idea (to the extent of some newer OOP languages not even allowing it), etc. –  Jul 21 '17 at 08:39
  • @Felix Palmen I am not arguing. But discussion with you unfortunately is not possible for the reason explained in one of my previous comments. For me it is EOD. – 0___________ Jul 21 '17 at 09:00
  • @PeterJ discussion doesn't mean people agreeing with you. –  Jul 21 '17 at 09:02
  • EOD - I agree - everything you said is right. there is no need of any OOP language as everything can be coded other way. And all the language features which are in the current standard are bad & obsolete. **Your word is the last** Happy? – 0___________ Jul 21 '17 at 09:21
  • @PeterJ this is ridiculous and trying to attribute some nonsense to me won't help you. You just don't want to accept your comment "*But the object programming is the future.*" doesn't make any sense, especially not when comparing C to C++. But that's only *your* problem. –  Jul 21 '17 at 13:50
  • And what should this tool do should it encounter `#if defined(FOO)`? How about `#if defined(BAR)&&defined(FOO)`? Or, how about `#if (!defined(BAR) && defined(FOO))||(__VERSION__>40403 && !defined(FOO))`? – H Walters Jul 21 '17 at 15:29
  • @HWalters, It should leave it unchanged, I think _coan_ is doing the trick. – Christian Herget Jul 23 '17 at 15:53

1 Answers1

2

You might be looking for coan, which has the ability to interpret #if and #ifdef directives given a set of definition (or undefinitions) supplied on the command-line.

rici
  • 234,347
  • 28
  • 237
  • 341