user2263786
user2263786

Reputation: 131

Hiding implementation and helper functions in C header file

Since the files i include are simply copied into the file I include from, I can call any function that the header file implements.

static declarations won't help either as far as I understand.

How can I publish some header file and prevent the user's main C file from calling some functions in my header?

(Of course not really hide because he can just look at the source, but help him not to use functions that are not intended to be called directly)

Upvotes: 1

Views: 2146

Answers (1)

Don't really hide them (BTW, making your library free software is usually worthwhile. You could use the LGPL license....). But put these internal functions in a separate header, e.g. foo-internals.h which is #included from the public foo.h and comment these functions as internal only!

Also, define some naming conventions, e.g. use some general foo_ prefix for public functions, and foointernal_ for "private" functions. Use once-only headers when needed.

You could use visibility pragmas and function attributes (set the visibility to hidden), at least on Linux or with GCC.

An API is not only defined by header files, but also by convention and by documentation. An undocumented function, or a function commented to be internal, should not be expected to be part of the API.

Take your inspiration by studying source code of real existing free software libraries (e.g. GTK, Posix libc such as MUSL libc or GNU libc, etc....).

Upvotes: 1

Related Questions