preprocessor question

Discussion in 'C Programming' started by Nathan Moinvaziri, Apr 4, 2008.

  1. I am wonder if there is a way to use preprocessor definitions to
    expose code only if a particular file is included in a project. I am
    targeting the msvc.

    I am thinking of something like

    #if defined(filecabinet.h) // if filecabinet.h is part of the project

    ....

    #endif
    Nathan Moinvaziri, Apr 4, 2008
    #1
    1. Advertising

  2. Nathan Moinvaziri <> writes:
    > I am wonder if there is a way to use preprocessor definitions to
    > expose code only if a particular file is included in a project. I am
    > targeting the msvc.
    >
    > I am thinking of something like
    >
    > #if defined(filecabinet.h) // if filecabinet.h is part of the project
    >
    > ...
    >
    > #endif


    There's no general method. But if each header defines a particular
    symbol, you can use #ifdef on that symbol.

    Many headers are wrapped in header guards, something like:

    #ifndef H_NAME
    #define H_NAME
    ...
    #endif

    but I wouldn't necessarily recommend referring to that symbol outside
    the header, since it's in some sense "local" to the header itself and
    could be changed later.

    If it's a header that you don't control, you could have a problem.
    You might be able to find some macro that happens to be defined in a
    header; for example, if you're using standard headers, then INT_MAX
    will be defined only if <limits.h> has been included.

    (The language *could* have defined such a feature, but I suspect the
    need for it was so rare that it didn't occur to anyone.)

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Apr 4, 2008
    #2
    1. Advertising

  3. "Robbie Hatley" <> writes:
    > Nathan Moinvaziri queried:

    [...]
    >> I am thinking of something like
    >>
    >> #if defined(filecabinet.h) // if filecabinet.h is part of the project

    >
    > Firstly, lose the parentheses. Secondly, use the include guard
    > rather than the file name. Thirdly, "//" isn't standard C.
    > Fourthly, comments on end of #defines SHOULD be ignored by
    > the preprocessor, but don't trust that; put them on the previous
    > line instead.


    First, the parentheses are optional; both
    #if defined SOMETHING
    and
    #if defined(SOMETHING)
    are perfectly legal.

    Second, the include guard is generally intended to be referred to only
    within the header that it guards, and it could be changed during later
    maintenance.

    Third (before jacob points it out), "//" comments are standard C as of
    the 1999 ISO standard. But they aren't supported in C90, and few
    compilers yet fully support C99 (though "//" comments are perhaps the
    most widely supported C99-specific feature).

    Fourth, I wouldn't worry about putting comments on the same line as a
    preprocessor directive. Conforming compilers have been required to
    allow this since the 1989 ANSI standard. (If you have a specific
    requirement to support pre-ANSI compilers, you'll have a lot more to
    worry about than that.)

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Apr 6, 2008
    #3
    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. Dan W.
    Replies:
    9
    Views:
    325
    Paul Mensonides
    Dec 4, 2003
  2. xuatla
    Replies:
    1
    Views:
    354
    Denis Remezov
    Jul 10, 2004
  3. Cronus
    Replies:
    1
    Views:
    643
    Paul Mensonides
    Jul 15, 2004
  4. ³á³á³á³á

    Preprocessor's question

    ³á³á³á³á, Sep 12, 2003, in forum: C Programming
    Replies:
    1
    Views:
    323
    Martin Ambuhl
    Sep 12, 2003
  5. Neil Zanella

    preprocessor question

    Neil Zanella, Nov 6, 2003, in forum: C Programming
    Replies:
    4
    Views:
    314
    Eric Sosman
    Nov 6, 2003
Loading...

Share This Page