conditional compile...

Discussion in 'C Programming' started by naunetr, Nov 30, 2007.

  1. naunetr

    naunetr Guest

    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
     
    naunetr, Nov 30, 2007
    #1
    1. Advertising

  2. naunetr

    Chris Dollin Guest

    naunetr wrote:

    > 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.

    --
    Chris "it was deliberate" Dollin

    Hewlett-Packard Limited Cain Road, Bracknell, registered no:
    registered office: Berks RG12 1HN 690597 England
     
    Chris Dollin, Nov 30, 2007
    #2
    1. Advertising

  3. naunetr

    pete Guest

    naunetr wrote:
    >
    > 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.


    > code:
    > /* #define MYSYMBOL 1 */
    >
    > #if MYSYMBOL != 1
    > #error "Error"
    > #else
    >
    > #include <stdio.h>
    > main() { puts("hello"); }
    >
    > #endif


    --
    pete
     
    pete, Nov 30, 2007
    #3
  4. naunetr

    Jack Klein Guest

    On Fri, 30 Nov 2007 19:57:40 +0530, naunetr <> wrote
    in comp.lang.c:

    > 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
     
    Jack Klein, Dec 1, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Miller

    Conditional compile in VHDL

    Dave Miller, Jan 30, 2005, in forum: VHDL
    Replies:
    5
    Views:
    8,447
    Thomas Entner
    Feb 9, 2005
  2. Brian Edginton

    Conditional Compile

    Brian Edginton, Jul 8, 2003, in forum: Java
    Replies:
    35
    Views:
    4,120
    Jim Sculley
    Jul 13, 2003
  3. Alec S.
    Replies:
    10
    Views:
    10,224
    Alec S.
    Apr 16, 2005
  4. Nagaraj
    Replies:
    1
    Views:
    888
    Lionel B
    Mar 1, 2007
  5. timjowers
    Replies:
    7
    Views:
    4,371
    heyjude
    Feb 2, 2011
Loading...

Share This Page