Xref: news2.mv.net comp.os.msdos.djgpp:8456 From: Elliott Oti Newsgroups: comp.os.msdos.djgpp Subject: Re: Problem with #define's under DJGPP Date: Mon, 09 Sep 1996 10:34:50 -0700 Organization: Academic Computer Centre Utrecht, (ACCU) Lines: 73 Message-ID: <3234553A.6C@stud.warande.ruu.nl> References: <511bnh$nne AT tornews DOT torolab DOT ibm DOT com> NNTP-Posting-Host: warande1078.warande.ruu.nl Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: wallaker AT vnet DOT ibm DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Aron Wallaker wrote: > > I'm having a problem trying to use #define's in a header file to define > constants for use in my C code. Here's an example: > > --File:STAR.H--------------------- > > #define STARPOPULATION 0x200 > > --FILE:STAR.C--------------------- > #include"star.h" > > void main(void) > { > int LocalStarPop = STARPOPULATION; // This works. > > for(x=0;x < STARPOPULATION ; x++) // This doesn't, see below. > { > //whatever > } > } > > When I try to set a local variable to the value I have #define'd in the > header file, it seems to work fine. However, if I try to use the #define > value in a function call, or a structure such as the for(... above, I > get a "parsing error in line...." error. I checked the FAQ's I could find, > and I saw no mention of this behavior. I have DJGPP V2.0 which I dl'd off > simtel about 2 weeks back. Does this look familiar to anyone ? I use this > type of #define all the time in Watcom, so I'd be pretty dissapointed if > I couldn't do something similar under DJGPP. I tried searching for any > command-line parameters which might affect the preprocessor and how it > handles #define's but I didn't find any. I would appreciate any feedback > anyone could give me. > > Thanks, > I tried compiling the fragment above and it compiled fine. I don't know if your code is literally equivalent to the fragment above, but you didn't declare x first. The only thing I added was "int x; " and "puts("*") " in the inner loop. It worked perfectly. I suspect the problem lies elsewhere in your code : in my experience the warning "parsing error" usually means you forgot a "{" or ")" or ";" or ' " ' -- or have one too many. So check that you didn't declare #define SHIT 0x34; <--- watch that ";" or #define PISS ((a)/(b))) <---- one ")" too many. Run lint or a braces-checker over your code; if you don't have lint replace the #defines with global variables and recompile a sample. If it works as expected then recheck the form of the #defines; if your program crashes then you know you left a loose brace somewhere in your code. The preprocessor in such cases doesn't stop where the mistake was made, but where it got into trouble. For instance: 1 void ShitFunction(void) 2 { puts("Oh shit!"); <--- oops, no closing brace 3 main() 4 { puts("Oh heck"); 5 ShitFunction(); 6 } doesn't give an error report at line 2 but at line 5 ("function ShitFunction already declared") and line 6 ("Parsing error in line 6"), because it thinks lines 3-6 are part of the definition of ShitFunction and expects closing braces at line 6. In this respect DJGPP's warnings are less helpfull than lint's would be. Try compiling with -Wall enabled; it is also helpful. Happy debugging, Elliott PS C++ comment-style *IS* allowed in C programs with DJGPP V2. Feel free to use //.