1

I have some C sources for which I want to expand #define macros (and #ifdefs and so on) but NOT #includes. #includes should be left untouched.

Is it possible by using the cpp (C preprocessor)?

Mat
  • 202,337
  • 40
  • 393
  • 406

6 Answers6

2

No, it isn't, since #include is a preprocessor instruction.

The only possibility you have is to launch cpp on the files after having removed the #include instructions.

If you have a Unix shell with GNU grep, this can be done, for instance, with:

grep -Pv '^\s*#\s*include\b' thefile.c|cpp
fge
  • 119,121
  • 33
  • 254
  • 329
2

There is no direct way.. So I have tried the following program and it works for most of the .c files that I have. I have taken @fge grep and added some more code along with gcc -E.. Hope this helps!

#!/bin/bash

if [[ $# -ne 1 || ! -f $1 ]] ; then
    echo "Invalid args / Check file "
    exit 
fi

file_name=$1

grep '^\s*#\s*include' $file_name > /tmp/include.c
grep -Pv '^\s*#\s*include\b' $file_name > /tmp/code.c
gcc -E /tmp/code.c | grep -v ^# > /tmp/preprocessed.c
cat /tmp/include.c > $file_name.done
cat /tmp/preprocessed.c >> $file_name.done
Sangeeth Saravanaraj
  • 16,027
  • 21
  • 69
  • 98
  • This moves all the includes to the beginning of the file, though, which goes wrong if your code contains something like `#ifdef __GNUC__ \n #include \n #else \n #include \n #endif`. I'd prefer to replace the `#include` with `FOOBARinclude`, preprocess, then replace FOOBAR with `#`. For safety, FOOBAR could be a long random string / GUID / whatever. – Steve Jessop Jan 05 '12 at 15:11
  • Yes that was a very primitive script. But that can be updated to suit all different `c` files. Your replace idea seems good! Let me know how it goes. – Sangeeth Saravanaraj Jan 05 '12 at 15:26
  • Great solution! Thanks Sangeeth, i'm indebted! – Nikolaos Kavvadias Jan 05 '12 at 18:19
  • @NikolaosKavvadias If you find it useful, please feel free to upvote. It would be encouraging. If you find my answer satisfying, please feel free to confirm the same by clicking the tick mark next to my answer. I'm glad it was useful to you in some way. Thanks! :) – Sangeeth Saravanaraj Jan 06 '12 at 03:13
1

If you have Emacs handy: c-macro-expand bound to C-c C-e can expand only the marked the region. This will also take the includes into account but will limit the output of the command to the marked section. One of the best commands if you do macro programming.

pmr
  • 58,701
  • 10
  • 113
  • 156
1

If those included files looks like:

inc_file1.h

#if !defined(INC_FILE1_H)
#define INC_FILE1_H

/// some code

#endif

then you can define INC_FILE1_H yourself causing inc_file1.h inside declarations to not be processed

marcinj
  • 48,511
  • 9
  • 79
  • 100
1

If you have one source file (or only a few), what you can do is to comment the #include directives, for example:

#if 0
#include "include1.h" 
#include "include2.h" 
#endif

and then run the preprocessor on the source files.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • Better still, comment them out with `/* ... */` and use option `-C` then they won't be deleted from the preprocessor output. – ams Jan 05 '12 at 16:12
0

With GCC, this is not possible.

Manuel Selva
  • 18,554
  • 22
  • 89
  • 134