Set only required functions in C

I am writing a small module of the API library library in C. I will compile this module and pass it on to my fellow developers, and I will lay out some required functions in the header file of my module so that the developers who will use my module know that to call the required functionality . Now I want to find out one thing: can I open only the desired functions in C. for example.

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 open by ie

 void A(); 

Now B () clearly depends on the condition set in A (), otherwise it cannot be started, and as soon as A () is set to test.h, then the user will not know that he can also directly call B () , Now I'm afraid that if the user finds out (or guesses) that in my module called B () there is a function that can be called directly, bypassing A (), then this may jeopardize my implementation.

I know that C ++ would be better in this case due to public and private methods, and I also have the idea that I can prevent B () from being called directly using some check of the A () flag in B (), but I want to know if there is any other method so that the user cannot call my functions (e.g. B ()) that are not displayed in the header file.

+4
source share
2 answers

Make function B :

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

Its visibility will be limited by the unit of translation where it is defined. B will have an internal connection; A will have an external connection.

+9
source

Another kind of link that is only supported by gcc / clang on some * NIX is a "hidden" link.

You can define a function as such:

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

This will allow you to call the function from another point in the shared object, but it does not make sense outside of this. This could be called from a different translation unit, but not from an application using your library.

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

+4
source

Source: https://habr.com/ru/post/1443393/


All Articles