Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 915
awatan Avatar asked Nov 01 '12 09:11

awatan


People also ask

Why do we need functions in C?

Why we need functions in C. Functions are used because of following reasons – a) To improve the readability of code. b) Improves the reusability of the code, same function can be used in any program rather than writing the same code from scratch. c) Debugging of the code would be easier if you use functions, as errors are easy to be traced.

How to use library functions in C programming?

A major plus point of C programming is that we can add a user-defined to any library to use it in other programs. As mentioned earlier, all library functions are included in different header files saved with a .h extension. To use any library functions, you need to use the header files at the beginning of the program.

How many functions are there in a C program?

2) Each C program must have at least one function, which is main (). 3) There is no limit on number of functions; A C program can have any number of functions. 4) A function can call itself and it is known as “ Recursion “. I have written a separate guide for it. return type: Data type of returned value.

What are user defined functions in C programming?

The functions that we create in a program are known as user defined functions or in other words you can say that a function created by user is known as user defined function. Now we will learn how to create user defined functions and how to use them in C Programming Syntax of a function


2 Answers

Make function B:

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.

like image 144
ouah Avatar answered Oct 26 '22 04:10

ouah


Another kind of linkage that is only supported by gcc/clang on some *NIX is 'hidden' linkage.

You can define a function as such like so:

__attribute__((visibility, ("hidden"))) void foo(void) {
      return;
}

This will allow the function to be called from another point in the shared object but no point outside that. That is it could be called from a different translation unit but not from the app using your library.

See http://gcc.gnu.org/onlinedocs/gcc-4.4.1/gcc/Function-Attributes.html for more information.

like image 23
Will Avatar answered Oct 26 '22 02:10

Will