This is just a variation of the method given by @ Michał Górny (I end up with a comment space there) ...
You can create an include file of the following form:
#ifndef PREFIX # define RENAME(f) #else # define RENAME(f) PREFIX ## f #endif #define func_foo RENAME(func_foo) #define func_bar RENAME(func_bar) #undef RENAME
At least gcc allows you to specify the inclusion of a header file from the command line with the -include rename.h option (provided that this file is called rename.h ). Since you are using gcc alternatives ( -O3 and Os ), I assume that you are using gcc for the rest of this answer. Otherwise, if your C compiler is reasonable, you should do it the same way.
You can easily create two or even three versions of your library that can be linked at the same time if you want by providing various parameters for your C compiler (here via the CFLAGS parameter):
CFLAGS += -include rename.h -DPREFIX=fast_ -D_BUILD_FAST -O3 -DBENCHMARKING CFLAGS += -include rename.h -DPREFIX=small_ -D_BUILD_SMALL -Os -DBENCHMARKING CFLAGS += -D_BUILD_FAST -O2
If your library's header files look very regular, and if you declare static private library functions, it's easy to extract functions from these header files with some dummy script, using very simple regular expressions to automatically generate rename.h for you. This is a natural build goal if you use make or something like that. All global variables must also be renamed using the same method for simultaneous use.
There are three main points in this decision:
- The ugly business of renaming can be hidden in one file, you do not need to edit the actual source files, especially you do not need to clutter up the source files, but they can keep them clean and easy to read.
- Renaming can be easily automated if you follow a few simple principles (coding conventions for header files and header files will declare all global variables and functions).
- There is no reason to make the comparative analysis more cumbersome if you need to run the test program several times (this is true if you are as lazy as I am and I don’t like repetitive tasks as much as I do - I know that many people don’t care, this is somewhat a matter of preference).
source share