Macro expansion and the preprocessor

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux