6

I got my hands on a opensource project coded in C. It uses #ifdef's for cross-compiling. There are a lot of ifdef's all over the source code. I want just to modify it for one platform. I was thinking to run it through compiler's pre-processor (Visual C++) but it will write the preprocessed result to a single file, which I don't need. Anybody knows a way to pre-process a project leaving it's structure intact (all files intact)? No grep, please.

edit:

I found a potential solution (it's amazing what you can find on the internet these days). It's boost.wave - a C++ preprocessor library which can do some interesting stuff. I don't know how it will turn out, but I will give it a try. Still, it's not the final answer, so if you have a solution then I will be glad to hear it.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ilcredo
  • 785
  • 2
  • 7
  • 17
  • 7
    why would you want to do this? readability? why not just let the #ifdefs remain so that if you ever want to share your modification, it will still crosscompile. – j-a Dec 30 '11 at 12:22
  • 1
    @j-a i counted 250 ifdef's in the main.c file. It's really a problem to me understanding all what's going on. Plus i want to add only platform specific code to the project. – ilcredo Dec 30 '11 at 12:24
  • Why are we not allowed to use regexp? – David Heffernan Dec 30 '11 at 12:27
  • @DavidHeffernan where's the challenge with regexen? – Daniel Fischer Dec 30 '11 at 12:29
  • 3
    I had the perfect solution in mind, until I read your last sentence. – Philip Dec 30 '11 at 12:29
  • @DavidHeffernan No grep on windows, and i have a lot of software installed so going for Cygwin or mingw will be a pain. Plus, it will be nice to use some compiler wisdom. – ilcredo Dec 30 '11 at 12:30
  • If the project is not too big, commenting out the `#include`s and preprocessing the individual files could be an option. – Daniel Fischer Dec 30 '11 at 12:32
  • Does visual-c++ have an option similar to gcc's -fdirectives-only? – Richard Pennington Dec 30 '11 at 12:38
  • @RichardPennington i think it doesn't. – ilcredo Dec 30 '11 at 12:44
  • 2
    There's grep on my system. And Perl. And Python. How does anyone get anything done without these basic tools? – David Heffernan Dec 30 '11 at 12:59
  • @DavidHeffernan I have python too, but in the project are also imbricated ifdef's and elseif's. I don't want to write a c compiler in python. I'll see, if nobody knows a way then i'll have to use grep, or even manually go over source. – ilcredo Dec 30 '11 at 13:07
  • Would this possibly be helpful? http://stackoverflow.com/questions/998913/visual-studio-hiding-ifdef-blocks – Dan Fego Dec 30 '11 at 13:47
  • @DanFego Yes, it is. I'll try to see if it works in VS2010. – ilcredo Dec 30 '11 at 14:10

2 Answers2

6

There are two tools I know of that you could use to do this semi-automatically.

One is sunifdef (son of unifdef). AFAIK, this is no longer being maintained, though (and neither is unifdef on which it is based).

The other is coan, which is being actively maintained, and is a development of sunifdef.

See also: Is there a C pre-processor which eliminates #ifdef blocks based on values defined/undefined?.

As it happens, I'm still using sunifdef on the main project at work where I'm eliminating archaic code (for example, machines not supported since about 1996) from the code base. The only gotcha I have with it is that if a line goes in with parentheses like this:

#if (defined(MACH_A) && defined(PROP_P)) || (defined(MACH_B) && defined(PROP_Q)) || \
    (defined(MACH_C) && defined(PROP_R))

and we have -UMACH_C (so machine C is no longer supported), the output line is:

#if defined(MACH_A) && defined(PROP_P) || defined(MACH_B) && defined(PROP_Q)

Technically, that's fine; it is correct. It is just preferable to keep the extra, technically redundant parentheses in the expression.

One caveat: although I can answer for these compiling on Unix-based systems, I've not personally checked them out on Windows.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
3

Comment out/remove the #include directives, preprocess it, reinstate the includes.

You'll need to make sure any macros used by the #ifdef are available, of course.

ams
  • 24,923
  • 4
  • 54
  • 75