automake
[Top][All Lists]
Advanced

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

Re: build the same source twice with different macros


From: Steffen Dettmer
Subject: Re: build the same source twice with different macros
Date: Mon, 15 Nov 2010 21:24:16 +0100

On Mon, Nov 15, 2010 at 5:11 PM, Nicolas Bock <address@hidden> wrote:
> I have some functions written in C that take a floating point argument, e.g.
>
> void foos (float x);
> void food (double x);
>
> The function bodies are basically identical except of course for the
> different floating point types. In order to avoid having to write
> redundant code, I see 2 options:
>
> (1) I can use C++ [...] template.

If I could use C++, I would do it :)

> (2) I can define a macro for the preprocessor that is either defined
> as "float" or "double" and then compile the function source twice, the
> first time with $CC -DFLOAT=float and the second time with $CC
> -DFLOAT=double.

I think this looks complicated.
If development rules allow it, I think someone could try for
small/short implementations:

--[foo.c]------------------------------------------------------>8=======
#define gen_FOO(FUNCNAME, TYPENAME) \
void FUNCNAME (TYPENAME x) \
{ \
   code; \
   code; \
}

gen_FOO(foos, float);
gen_FOO(food, double);
=======8<-------------------------------------------------------------------

or when gen_FOO would be too big (e.g. when needing compilers
just accepting 10 lines macros or so) maybe:

--[foo.inc]------------------------------------------------------>8=======
void FOONAME(FOOTYPENAME x)
{
  ...
}
=======8<-------------------------------------------------------------------

--[foo.c]------------------------------------------------------>8=======
#define FOONAME foos
#define FOOTYPENAME float
#include "foo.inc"
#undef FOONAME
#undef FOOTYPENAME

#define FOONAME food
#define FOOTYPENAME double
#include "foo.inc"
=======8<-------------------------------------------------------------------

or some source code generation:

--[Makefile.am]---------------------------------------------------->8=======
# just to illustrate the idea, rule surely is wrong:
foos.c: Makefile foox.in
        perl -np -e 's/FOONAME/foos/ ...' < foox.in > $@
food.c: Makefile foox.in
        perl -np -e 's/FOONAME/food/ ...' < foox.in > $@
=======8<-------------------------------------------------------------------

(here we typically use source code generation; often with a
dedicated perl generator script creating all related functions
inside a single .c file).

oki,

Steffen



reply via email to

[Prev in Thread] Current Thread [Next in Thread]