Source File Organization
Richard A. O'Keefe
ok at goanna.cs.rmit.oz.au
Wed Feb 27 18:49:29 AEST 1991
In article <1991Feb26.045242.23453 at rfengr.com>, rfarris at rfengr.com (Rick Farris) writes:
> I have a problem that I'm sure has been solved in the C
> language before; would someone point me in the right
> direction?
> typedef enum { A, B, C, D } CMD;
> char ltrs[] = { 'A', 'B', 'C', 'D' };
> My problem is: How do I keep the darn things in sync?
I've seen this one often enough that I think it belongs in the FAQ list.
The answer is that you *don't* do it by any special magic in the C source
code itself, but use some other tool to transform a "mini language" to
both files. For example, write a little file like
cmd.defs
-----------
A "Alfa"
B "Bravo"
...
D "Delta"
and two awk scripts:
cmd.awk
-------------
BEGIN { print "typedef enum {" }
{ print $1, "," }
END { print "} CMD;" }
and
ltrs.awk
-------------
BEGIN { print "char ltrs[] = {" }
{ print " '" substr($2,1,1) "'," }
END { print "};"}
and then put in your Makefile
cmd.h: cmd.defs cmd.awk
awk -f cmd.awk <cmd.defs >cmd.h
ltrs.c: cmd.defs ltrs.awk
awk -f ltrs.awk <cmd.defs >ltrs.c
(This is not a UNIX-specific solution: make and awk lookalikes are
available for other systems. If you haven't got them, it's trivial
to do this in C itself.)
Moral: C source code is plain text that can be generated by other
programs.
--
The purpose of advertising is to destroy the freedom of the market.
More information about the Comp.lang.c
mailing list