This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Macro expansion and the preprocessor
- From: Adam Mercer <ramercer at gmail dot com>
- To: GCC Help <gcc-help at gcc dot gnu dot org>
- Date: Fri, 14 Jan 2011 11:55:05 -0600
- Subject: Macro expansion and the preprocessor
Hi
In one of our projects we have several functions that are mostly
identical, all that varies is the datatypes on which they operate.
When this code was originally written, over 10 years ago, this was
achieved by using m4 to generate the source multiple times and making
the appropriate substitutions. Whilst this works I don't like it at
all and think that it would be cleaner to use the preprocessor. I've
been trying to code this up but am running into problems:
I have two source files, Grid.c:
#include <lal/LALStdlib.h>
#include <lal/AVFactories.h>
#include <lal/Grid.h>
#define TYPECODE Z
#define TYPE COMPLEX16
#include "Grid_source.c"
#undef TYPECODE
#undef TYPE
and Grid_source.c:
#define GTYPE TYPE##Grid
#define FUNC(f) LAL##TYPECODE##f
#define CFUNC FUNC(CreateGrid)
#define DFUNC FUNC(DestroyGrid)
#define CREATEFUNC FUNC(CreateArray)
#define DESTROYFUNC FUNC(DestroyArray)
void
CFUNC ( LALStatus *stat, GTYPE **grid, UINT4Vector *dimLength, UINT4 dimension )
{
printf("Hello, World!\n");
}
These are snippets but are enough to illustrate the problem. When I
build it fails with the following:
$ make
CC Grid.lo
In file included from Grid.c:106:
Grid_source.c:9: error: expected declaration specifiers or '...'
before 'TYPEGrid'
Grid_source.c:10: warning: no previous prototype for 'LALTYPECODECreateGrid'
Grid_source.c: In function 'LALTYPECODECreateGrid'
$
So it seems that the expansions aren't be done as I would expect them to be.
Can anyone see what I'm doing wrong?
I've been trying to find some documentation for using the preprocessor
in this fashion but have been unsuccessful, does anyone know of a good
resource?
Cheers
Adam