Xref: utzoo comp.lang.c:36573 comp.software-eng:4936 Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!uakari.primate.wisc.edu!zaphod.mps.ohio-state.edu!rpi!batcomputer!munnari.oz.au!goanna!ok From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) Newsgroups: comp.lang.c,comp.software-eng Subject: Re: Source File Organization Message-ID: <4836@goanna.cs.rmit.oz.au> Date: 27 Feb 91 07:49:29 GMT References: <1991Feb26.045242.23453@rfengr.com> Followup-To: comp.lang.c Organization: Comp Sci, RMIT, Melbourne, Australia Lines: 47 In article <1991Feb26.045242.23453@rfengr.com>, rfarris@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.h ltrs.c: cmd.defs ltrs.awk awk -f ltrs.awk 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.