1 year ago
#236295
Mike T.
Display preprocessor output just processing the conditional compilation directives
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));
}
c-preprocessor
m4
0 Answers
Your Answer