preprocessor question

  • Thread starter Nathan Moinvaziri
  • Start date
N

Nathan Moinvaziri

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
 
K

Keith Thompson

Nathan Moinvaziri said:
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.)
 
K

Keith Thompson

Robbie Hatley said:
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.)
 

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

Similar Threads

Preprocessor question 3
preprocessor bug? 2
Preprocessor 3
Preprocessor commands usage. 11
preprocessor trick 4
Preprocessor limitation workarounds 32
Preprocessor 0
preprocessor 2

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top