cshared-librariesstatic-libraries

Expose only required functions in C


I am writing a small API library sort of module in C. I will compile this module and give it to my fellow developers and I will expose some required functions in header file of my module so developers who will use my module know which function to call for required functionality. Now I want to inquire one thing: Can I expose only desired functions in C. e.g.

I have test.c having:

#include "test.h"
void A()
{
  if( some condition is true )
    B();
  else
   return;
}

void B()
{
  //some code here
}

and in test.h, I have only one function exposed i.e.

void A();

Now B() clearly is dependent on condition put in A() otherwise it can not be run and as only A() is exposed in test.h then user wont know that he/she can also directly call B(). Now my fear is that if user gets to know (or by guess) that there is some function in my module called B() which can be called directly by bypassing A(), then it can compromise my implementation.

I know that C++ would be better in this case because of public and private methods and I also have an idea that I can prevent B() being called directly by using some flag check of A() in B() but I want to know if there is any other method so that user cant call my functions (like B()) which are not exposed in header file.


Solution

  • Make function B a static function:

    static void B(void)
    {
      //some code here
    }
    

    Its visibility will be limited to the translation unit where it is defined. B will have internal linkage; A will have external linkage.