Kagamin
Kagamin

Reputation: 143

What happens if I don't declare all functions in header?

I'm currently practicing a unit test with GTest, and noticed that I didn't declare all functions written on the target source code (target.c) to its header (target.h). Since I didn't do the test for those undeclared functions, I couldn't notice until now.

Now, it seems that those header-undeclared functions work as 'private' functions since they are not callable from the test code (which includes header of the target source code).

Can I consider this as a way to declare a private function or should I be aware of something else for safety?

Upvotes: 1

Views: 148

Answers (1)

engineer14
engineer14

Reputation: 617

No. that does not make your function private. It just then requires the caller to extern that function themselves. Using the static key word is the appropriate way to create a private function. Eg:

static void myfunc ()
{
 ...
}

Not including it in the header doesn't make it a private function, since any other C file could add an extern void myfunc() in either their header or C code and gain access to that function. At compile time, all of that is going to be linked (assuming you are compiling all the files).

BUT all static objects will only have module level (or file scope) visibility

The same goes for variables you only want in the filescope.

Upvotes: 2

Related Questions