C: File-scope visibility of identifiers

Programming, for all ages and all languages.
Post Reply
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

C: File-scope visibility of identifiers

Post by Solar »

Help out a C++ vet who's out of his fishing grounds...

Let's say I write a "C" header file, X.h. This header in turn includes another, Y.h.

I want to avoid the stuff from Y.h "leaking out" of X.h - if someone includes X.h, he should only see the identifiers from X.h, not those from Y.h.

I know the answer is probably trivial, but my brain stops dead after spitting out "anonymous namespace" - which, of course, is C++, not C...
Every good solution is obvious once you've found it.
Tim

Re:C: File-scope visibility of identifiers

Post by Tim »

You can't.

How you deal with this depends on what X.h needs from Y.h. As a C++ programmer, I assume you know that this works:

Code: Select all

/* X.h */

struct astruct
{
    /* anotherstruct defined in Y.h */
    struct anotherstruct *fred;
};
However, this won't:

Code: Select all

/* X.h */

struct astruct
{
    /* anotherstruct defined in Y.h */
    struct anotherstruct fred;
};
In the second example, X.h really does need the definition of anotherstruct in order to make the definition of astruct work.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:C: File-scope visibility of identifiers

Post by Solar »

Thanks.
Every good solution is obvious once you've found it.
Post Reply