validating problem

F

foodic

Hi all,
I have got a problem where i have to execute
a portion of code only when a header file is
included somewhere in the source file. Can some
body explain how to do it.
for example
if included placebo.h{
execute this part of code
}


Thanks
Divya
 
A

Antonio Contreras

foodic said:
Hi all,
I have got a problem where i have to execute
a portion of code only when a header file is
included somewhere in the source file. Can some
body explain how to do it.
for example
if included placebo.h{
execute this part of code
}


Thanks
Divya

You could make this:

File placebo.h:

#define PLACEBO_H
.....

File whatever.c

....
#ifdef PLACEBO_H

code to execute

#endif
....

Note that this will only work if the header file is included _before_
this construct. This should be no great problem since all your includes
should be at the very start of the file...
 
C

Cong Wang

foodic said:
Hi all,
I have got a problem where i have to execute
a portion of code only when a header file is
included somewhere in the source file. Can some
body explain how to do it.
for example
if included placebo.h{
execute this part of code
}


Thanks
Divya
No,you can't do this directly.However,most of the head files define a
macro to identify itself.For example:
#ifndef _BACKWARD_ALLOC_H
#define _BACKWARD_ALLOC_H 1

Others are like this.
So you can use:
#ifdef _HEAD_FILE_NAME_H
/*do something*/
#endif
 
E

Emmanuel Delahaye

Cong Wang wrote on 29/07/05 :
No,you can't do this directly.However,most of the head files define a
macro to identify itself.For example:
#ifndef _BACKWARD_ALLOC_H

Please keep in mind that identifiers beginning with _[_A-Z] belong to
the implementation name space.

#ifndef H_BACKWARD_ALLOC

is fine for a user.


--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.
 
G

Grumble

Cong said:
For example:
#ifndef _BACKWARD_ALLOC_H
#define _BACKWARD_ALLOC_H 1

<quote>
The C standard reserves quite a few identifiers, meaning that you must
not create variables, functions, or macros with those names. If you
don't obey the restriction, you can have nasty subtle bugs in your
program. This page lists the reserved identifiers and discusses the
pitfalls.
</quote>

http://web.archive.org/web/20031203230123/oakroadsystems.com/tech/c-predef.htm
 
M

Martin Ambuhl

foodic said:
Hi all,
I have got a problem where i have to execute
a portion of code only when a header file is
included somewhere in the source file. Can some
body explain how to do it.
for example
if included placebo.h{
execute this part of code
}

If placebo.h is written well, it will have guards against multiple
inclusion. The header will be wrapped in something like
#if !defined(PLACEBO_H_GUARD)
#define PLACEBO_H_GUARD

/* the header contents go here */
#endif

Look in the header and find the name of the guarding macro. Use it in
your own code:

#if defined(PLACEBO_H_GUARD)
/* code to be used when placebo.h is included */
#else
/* code to be used otherwise */
#endif
 
M

Martin Ambuhl

Cong said:
No,you can't do this directly.However,most of the head files define a
macro to identify itself.For example:
#ifndef _BACKWARD_ALLOC_H
^^
This underscore should not be here unless the header is part of the
implementation.
 
K

Keith Thompson

Martin Ambuhl said:
If placebo.h is written well, it will have guards against multiple
inclusion. The header will be wrapped in something like
#if !defined(PLACEBO_H_GUARD)
#define PLACEBO_H_GUARD

/* the header contents go here */
#endif

Look in the header and find the name of the guarding macro. Use it in
your own code:

#if defined(PLACEBO_H_GUARD)
/* code to be used when placebo.h is included */
#else
/* code to be used otherwise */
#endif

Yes, that can work, but personally I'd hesitate to use a guarding
macro outside the header that defines it. If I were maintaining the
header itself, I'd feel free to change the name of the guarding macro;
it probably wouldn't occur to me that someone else might be using it.
I don't think of the name of the macro as part of the interface.

It's usually better to use something that *is* part of the interface.
For example, <stdbool.h> provides "__bool_true_false_are_defined".

If the header doesn't provide a testable macro, perhaps you can
prevail on the author to add one (which should be easy if you're the
author). If that's not feasible, you can replace each occurrence of
#include "placebo.h"
with
#include "placebo.h"
#define PLACEBO_INCLUDED

The OP's original question seemed to imply that he wants to do the
test at run time. Since the question of whether the header is
included is determined during compilation, it makes more sense to use
a preprocessor test (#if or #ifdef rather than if(...)).
 
B

Barry Schwarz

Hi all,
I have got a problem where i have to execute
a portion of code only when a header file is
included somewhere in the source file. Can some
body explain how to do it.
for example
if included placebo.h{
execute this part of code
}

You do realize that a header is included when your program is
compiled, not when it is executed?

If you want the code to be compiled only if the header is included,
you can define a macro in the header and use #ifdef to control whether
it is compiled or not.

If you want the code to be present but only executed if the header was
included, I'm not sure. Most of the regulars here recommend against
having a header file define an object or contain executable code.


<<Remove the del for email>>
 
P

Peter Nilsson

Martin said:
If placebo.h is written well, it will have guards against multiple
inclusion. The header will be wrapped in something like
#if !defined(PLACEBO_H_GUARD)
#define PLACEBO_H_GUARD

/* the header contents go here */
#endif

This is fine for placebo.h, but if there's a header called effect.h,
then a guard name like EFFECT_H_GUARD would clash with reserved E*
from <errno.h>. A better convention would be H_XXXXX.
 
M

Martin Ambuhl

Peter said:
This is fine for placebo.h, but if there's a header called effect.h,
then a guard name like EFFECT_H_GUARD would clash with reserved E*
from <errno.h>. A better convention would be H_XXXXX.

I did not propose a "convention" to use in naming a header guard. I
proposed using the header guard that already exists in a well-written
header. If you had read my post, you would know that. You would also
know that the effect.h header guard you don't like could not be in a
well-written header, so is already excluded from consideration. Schmuck.
 
P

Peter Nilsson

Martin said:
I did not propose a "convention" to use in naming a header guard.

But you did give a sample consistent with the popular HEADER_H[_...]
convention.
I proposed using the header guard that already exists in a well-
written header.

Yes, and that answered the OP's question. What I was commenting on
was a side issue, an issue that you yourself picked up on in the
OP's original post, namely that certain identifiers are not
appropriate as header guard names.
If you had read my post, you would know that.

Did you read my post and what it said?
You would also know that the effect.h header guard you don't like
could not be in a well-written header,

I know that and you know that, but not all newbies to C reading clc
will know that. Hence my post (and yours.)
so is already excluded from consideration.

By you, not by me.

<sigh> All I did was to provide further examples of reserved
identifiers which are inapropriate for use as ordinary header
guards. If there was anything wrong with my post from an ISO C
perspective, please supply a chapter and verse correction
thereof.
 

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

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,915
Members
47,456
Latest member
JavierWalp

Latest Threads

Top