conditional compile...

N

naunetr

hello all,

i wrote the small program below to practice conditional compiling. if i
define MYSYMBOL then everthing works fine, but when i comment MYSYMBOL
gcc gives the following message:
cnd_compile01.c:4:2: error: #error "Error"
cnd_compile01.c:1: warning: ISO C forbids an empty source file

i cant understand the warning and how to eliminate it? i tried placing
some dummy comment but still the same warning.

also why is compiler printing error twice. it looks ugly. is there a
way to make it print error once only?

thanks.

code:
/* #define MYSYMBOL 1 */

#if MYSYMBOL != 1
#error "Error"
#else

#include <stdio.h>
main() { puts("hello"); }

#endif
 
C

Chris Dollin

naunetr said:
hello all,

i wrote the small program below to practice conditional compiling. if i
define MYSYMBOL then everthing works fine, but when i comment MYSYMBOL
gcc gives the following message:
cnd_compile01.c:4:2: error: #error "Error"
cnd_compile01.c:1: warning: ISO C forbids an empty source file

i cant understand the warning and how to eliminate it? i tried placing
some dummy comment but still the same warning.

You need real code. A static declaration will do; comments won't.
also why is compiler printing error twice. it looks ugly. is there a
way to make it print error once only?

Pass [1]. But who cares? The way to stop it printing `error` twice is to
stop it printing `error` at all. If there's a real error error, the
repeated text is likely to be the least of your problems.

[1] If there is, it will be compiler specific. gcc has many many command-
line options, many of which control diagnostic reports.
 
P

pete

naunetr said:
hello all,

i wrote the small program below to practice conditional compiling.
if i
define MYSYMBOL then everthing works fine, but when i comment MYSYMBOL
gcc gives the following message:
cnd_compile01.c:4:2: error: #error "Error"
cnd_compile01.c:1: warning: ISO C forbids an empty source file

i cant understand the warning and how to eliminate it? i tried placing
some dummy comment but still the same warning.

It means that with MYSYMBOL commented out, this:
#error "Error"
becomes your whole program,
and that's not enough of a program to compile.
 
J

Jack Klein

hello all,

i wrote the small program below to practice conditional compiling. if i
define MYSYMBOL then everthing works fine, but when i comment MYSYMBOL
gcc gives the following message:
cnd_compile01.c:4:2: error: #error "Error"
cnd_compile01.c:1: warning: ISO C forbids an empty source file

i cant understand the warning and how to eliminate it? i tried placing
some dummy comment but still the same warning.

also why is compiler printing error twice. it looks ugly. is there a
way to make it print error once only?

The compiler is outputting two different diagnostic messages because
there are two different issues with your program.

thanks.

code:
/* #define MYSYMBOL 1 */

#if MYSYMBOL != 1
#error "Error"
#else

#include <stdio.h>
main() { puts("hello"); }

#endif

To add detail to Pete's explanation...

When MYSYMBOL is not defined to 1, the preprocessor eliminates
everything and the compiler only sees:

#error "Error"

....as the entire translation unit. C requires that every translation
unit requires at least one external definition, and yours does not
have one. That's the second diagnostic, the one referring to line 1,
although the text of the diagnostic is misleading.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 

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,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top