Using macros as function_names (for compiling different function_names with one C-Code via a compile

  • Thread starter Andreas Morgenstern
  • Start date
A

Andreas Morgenstern

Hi,
Does anybody know how one can use a macro (or something similar) as a
function name ?
I would like to declare the following functions:

FOO_fun1(int)
{
....
}

FOO_fun2(int)
{
....
}

with a compile option (I use gcc) FOO should be substituted by any string.

Thank for all suggestions

Andreas

PS: if anybody wonders, what I'm doing:


I'm currently implementing something in Esterel and Esterel will
automatically generate me a C-Code-File (for example foo.c).
In this file, there will be function calls to some extern declared functions

foo_fun1(int), foo_fun2(char* h)....

I have to implement those functions foo. But the C-Code of another esterel
programm foo2 will assume the same syntax. I'm trying to avoid that I must
replace foo by foo2 by hand, therefore a compile option would be nice
 
G

Grumble

Andreas said:
Does anybody know how one can use a macro (or something similar) as a
function name ?
I would like to declare the following functions:

FOO_fun1(int)
{
...
}

FOO_fun2(int)
{
...
}

with a compile option (I use gcc) FOO should be substituted by any string.

I don't think (??) the ## preprocessor operator can help here.

<OT>
Would 'sed "s/FOO/foo2/g" foo2.c' be an acceptable solution?
(You might want to choose a more unique prefix.)
</OT>
 
S

SM Ryan

# Hi,
# Does anybody know how one can use a macro (or something similar) as a
# function name ?
# I would like to declare the following functions:
#
# FOO_fun1(int)
# {
# ...
# }
#
# FOO_fun2(int)
# {
# ...
# }
#
# with a compile option (I use gcc) FOO should be substituted by any string.

Why not just
#define FOO_fun1 othername
#define FOO_fun2 anothername
...
#define FOO_funN yet_anothername

If you don't what N will will be, and your build system allows, you can scan
the file and create the necessary #defines on the fly. For example, on Unix
you can do something like

grep -o -h 'FOO_fun[9-9]*' * <sourcefile \
| sort -u \
| awk '{N = substr($0,match($0,/[0-9]*$/)); print "#define " $0 " other" N}' \
> defines.h

and include defines.h
 
D

Dave Thompson

Why not just
#define FOO_fun1 othername
#define FOO_fun2 anothername
...
#define FOO_funN yet_anothername

If you don't what N will will be, and your build system allows, you can scan
the file and create the necessary #defines on the fly. For example, on Unix
you can do something like

grep -o -h 'FOO_fun[9-9]*' * <sourcefile \
| sort -u \
| awk '{N = substr($0,match($0,/[0-9]*$/)); print "#define " $0 " other" N}' \
> defines.h

and include defines.h

#if OFFTOPIC == mostly for clc

Presumably you meant [0-9], probably + instead of *, and not a
superfluous filename argument of *. -h is GNU-only, but not needed for
input from stdin (it sees no filename!) and -o doesn't exist on any
system/implementation I know about. awk can already handle what you
have in grep and sort, though your awk unnecessarily limits itself to
an identifier occuring at end of line which was not the OP's case; and
for nonUnix systems it is sometimes easier to get and install just awk
instead of a more extensive toolkit, and often easier to port an awk
script in a file rather than an xsh/ysh/CMD/somethingelse script.

awk <sourcefile -f blech.awk # containing, or quote appropriately

match($0,/FOO_fun[0-9]+/) && !done[$0=substr($0,RSTART,RLENGTH)]++ {
chg=$0; sub(/FOO/," BAR",chg); print "#define " $0 chg }

If FOO_bletch might occur within other names, add something like a
leading (^| |\t) or GNU-only \< or \y and/or similar trailing.

If you care about the order of the #define's can also do (reasonable)
sorting within awk, but OP's case doesn't need this.

The !done[]++ part, corresponding to your sort -u, isn't needed if the
input contains each function/name only once, which wasn't clear from
the OP; and isn't really needed even if it doesn't, since standard C
allows identical re-#define-itions, but that is sufficiently obscure
and rarely used to be confusing to readers and maintainers.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top