Computing > Programming

header file exposure

(1/8) > >>

Simon:
I want to write a header file that will be used by only one C file and one other header file. Is there a way to limit the visibility of the file? Basically I don't want to write overly long defines to guarantee that they do not clash with something else, but if this file is included in the main header file with function prototypes I will expose it to the whole program.

So I think something like:

name.c - cotains the code
name.h - contains the publicly available function types
name_internal.h - contains the private stuff and is only included in the c file.

T3sl4co1l:
It's traditional to put private stuff at the top of the .c file itself; or if you wish to avoid clutter, you can dump that into a "_private.h" or whatever, yes indeed. :)

There's no mechanism to prevent another file including that header, this is all just convention and hackery in C to emulate what would be explicit OOP in a higher level language.

Tim

Psi:

--- Quote from: T3sl4co1l on October 06, 2021, 09:50:48 am ---There's no mechanism to prevent another file including that header, this is all just convention and hackery in C to emulate what would be explicit OOP in a higher level language.

--- End quote ---

I've not actually tried this but could you surround your private header with an #ifdef.
Then, when you need to include it you do #define, #include, #undefine ?

If that works it would protect that file in case it is ever included by accident somewhere else in the project.

T3sl4co1l:
I mean, whatever song and dance you put into it, can just be copy and pasted from another file into the target file.  Preprocessor isn't quite Turing complete, nor stateful, so you can't implement just anything, like, locking it behind a crypto challenge-response or something.  There's no semantic protection, it's simply the honor system whether something #includes it or not.

Surrounding it with an #ifdef is a good idea anyway, but usually for purposes of including it just once -- this way if several headers include it in turn, there's no confusion.

Tim

DiTBho:

--- Quote from: Simon on October 06, 2021, 09:35:38 am ---name.c - cotains the code
name.h - contains the publicly available function types
name_internal.h - contains the private stuff and is only included in the c file.

--- End quote ---

rename internals as ".inc"

name.c:

--- Code: ---#include "name.h"
...
#define _name_internal_
#include "name_internal.inc"

--- End code ---

name.h:

--- Code: ---#ifndef _name_h_
#define _name_h_

...

#endif

--- End code ---

this can be used *only* if _name_internal_ is defined, and only name.c should define it
name_internal.inc:

--- Code: ---#ifdef _name_internal_

...

#endif

--- End code ---

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version