473,394 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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



Nov 15 '05 #1
3 978
Andreas Morgenstern wrote:
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>
Nov 15 '05 #2
Andreas Morgenstern <mo*********@informatik.uni-kl.de> wrote:
# 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

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I have no idea what you just said.
I get that alot.
Nov 15 '05 #3
On Fri, 24 Jun 2005 16:34:27 -0000, SM Ryan
<wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> wrote in
comp.lang.c; comp.lang.awk added, fup there for the awk stuff:

<snip: OP wants to rename all FOO_fun<number> in a C source>
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
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: karpagam | last post by:
Since WebServices are a standardized way to invoke a method of another application over the internet, is there a way to integrate two different applications running on different platforms (Say a...
5
by: Juan T. Llibre | last post by:
OK, guys, usually I answer questions instead of asking them, but this thing has me scratching my head. Why is the default for AutoEventWireup different for C# and VB.NET ? In VS 2005, if I...
7
by: Nerrad | last post by:
Hi, i am currently working on a project which requires me to create a form to allow the user to change their password. I know there is a user-level security login which is easier and safer but...
2
by: delphine_fontaine | last post by:
Hello, I have a projet for smartphones which I can't build. In fact, I have this error : The type or namespace name 'ISupportInitialize' does not exist in the class or namespace...
2
by: phjones | last post by:
Need help programming mortagage calculator for 3 different loans 7 year, 15 year and 30 year. using java array I am a beginner with Java, This is what I have so far. Need to know if I am off the...
6
by: Mayur1234 | last post by:
Hi all, Can any body tell me how to configure Textpad for compiling and running java application
2
by: freddy windey | last post by:
Hi, this my 1st time i join discussion. im just a new programmer. i need help from your expert on this "Insert data using 1 form to 2 different table"- ASP VBscript. example- name: age: DOB:...
5
by: 2di | last post by:
Hi every one, I got this problem, i need to create classes in c++ by using macros. Thats what i want: #define createObject(type) type a; main(){ string className = "int"; ...
6
by: nicolenwn | last post by:
Hi everyone(: I'm having trouble creating pivot tables using macros. First i tried recording it then running the exact same thing. It worked fine the first time but a week later when i tried...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.