0

I've got a c program that has grown organically over a long period of time to support multiple different targets using #if #else #elif and #endif directives.

How would I use the gcc preprocessor (or the m4 preprocessor) to display to code that would be compiled as a result of a particular identifier being defined without displaying any of the conditional compilation directives or expanding any include files or macros.

gcc -E doesn't work as it expands both macros and include files and I'd prefer not to do this by hand!

For example on a linux system where the 'linux' identifier is defined then parsing the following code:

#include <stdio.h>
#if defined(linux)
#include <unistd.h>
#include <sys/types.h>
#elif defined(WIN32)
#include <windows.h>
#elif defined(VMS)
#include <timeb.h>
#include <lib$routines.h>
#else
#include <sys/types.h>
#include <sys/timeb.h>
#endif

#include "debug.h"

int i_wait(long l_delay) { /* wait for milliseconds */
#if defined(linux) /* Use usleep() function */
debug(fprintf(stderr, "Pausing using usleep() for  %ld ms.\n", l_delay));
return (usleep(l_delay * 1000));
#elif defined(WIN32) /* Use usleep() function */
debug(fprintf(stderr, "Pausing using sleep() for %d ms.\n", l_delay));
Sleep(l_delay);
return (0);
#elif defined(VMS) /* Use VMS LIB$WAIT */
float f_seconds;
debug(fprintf(stderr, "Pausing using LIB$WAIT for %ld ms.\n", l_delay));
f_seconds = l_delay / 1000.0;
return (lib$wait(&f_seconds));
#else /* Use a portable but very inefficent busy loop */
struct timeb o_start, o_end;
debug(fprintf(stderr, "Pausing for %ld ms.\n", l_delay));
ftime(&o_start);
ftime(&o_end);
while ((1000 * (o_end.time - o_start.time) + o_end.millitm - o_start.millitm) < l_delay) {
   ftime(&o_end);
}
return(0);
#endif
}

Should produce the output below:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

#include "debug.h"

int i_wait(long l_delay) { /* wait for milliseconds */
debug(fprintf(stderr, "Pausing using usleep() for  %ld ms.\n", l_delay));
return (usleep(l_delay * 1000));
}
Mike T.
  • 165
  • 1
  • 12
  • I do not understand. What would be displayed for what? Please give an example. How can you "display a result of a particular identifier without ... expanding ... macros"? To display the result, you _have to_ expand macros - I do not understand. How is m4 related to all this? `work as it expands both macros and include files and I'd prefer not to do this by hand!` what are you doing by hand? Either way, whatever the solution, you will have to do _something_ with your hands. I do not understand. – KamilCuk Feb 28 '22 at 23:40
  • Does https://stackoverflow.com/questions/1562074/how-do-i-show-the-value-of-a-define-at-compile-time answer your question? – KamilCuk Feb 28 '22 at 23:42
  • No I'm afraid it doesn't – Mike T. Mar 02 '22 at 00:08
  • I think your best bet is to use a program to comment out all the includes and defines, then run a normal preprocessor, then uncomment the lines. Either that, or find a simple open source C preprocessor (read: not GCC) and modify it to disable include and define processing. – markasoftware Mar 02 '22 at 05:15

0 Answers0