Conditional Inclusion of C functions


When developing libraries for embedded systems, its is important to keep the code size under control due to the limited amounts of available memory.

This article discusses a method for using the GCC pre-processor to implement a mechanism to control which functions are included and which are excluded from a library.


Prepare the source code

A pre-processor identifier is associated to each function that we want to control (from here on referred to as conditional function), for instance, for a function called my_function, the pre-processor identifier MYFUNCSPLIT could be used.

  • A. Each declaration of function is enclosed in an pre-processor ifdef statement as follows :
#ifdef MYFUNCSPLIT
 
myfunction() {
 
}
 
#endif
  • B. In each source file containing conditional functions add the following pre-processor macros
#ifndef SPLITFILE
#define MYFUNCSPLIT1
#define MYFUNCSPLIT2
#define MYFUNCSPLIT3
...
#endif

At compile time

Now when we want to control function inclusion, we need to define the pre-processor identifier SPLITFILE and the pre-processor identifiers of the functions that we need to include.

For example, the command line for including myfunction2 and myfunction3 while excluding myfunction1 would be :

gcc -DSPLITFILE -DMYFUNCSPLIT2 -DMYFUNCSPLIT3

What happens is that by defining the pre-processor identifier SPLITFILE, none of the pre-processor identifiers MYFUNCSPLITX will be defined because of what we have done in Step (B) when preparing the code. This means that only functions for which pre-processor identifiers were defined manually using the gcc -D option will be compiled.



Labels: , Wireless Internet Security Coding Network Monitoring

Comment

Enter your comment (wiki syntax is allowed):
GNGUM

Wireless Internet Security Performance RADIUS server Wireless Internet Security Performance RADIUS server