I am currently writing a C project that includes a separate test build.
The tests are different C processes that uses the source code to test every defined function in my code (TDD).
I want monkey patching in those tests. I took some time to see what it could be done and I think the easiest option is to use gcc attribute feature: https://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/Function-Attributes.html
For the test build, I would like all of my functions to be weak (IE, I want the same effect as if I would have written __attribute__ (( weak))
in front of any function declaration of my C file.)
I would like to know if there is a way of passing this weak attribute at weak by default as a compilation option rather than doing it manually each time I need it. Right now I am using a macro that test if the build is in test and if so add this line before every function I want. I would rather avoid complexifing the source code just to allow some test features.
This topic on google is polluted by the repetition of the same the same trivial attribute usage, but I can not find any way of setting attribute defaults.
However, I found this book https://link.springer.com/chapter/10.1007%2F978-1-4302-0704-7_4 that may contain the solution but I wont pay 30 buck just in hope for that. So here I am asking the question.
Some may think it's a bad idea but in my scenario it is fine I think. Remember that my test suite is composed of a lot of mono C file linked aginst my code and remember that the normal build wont be impacted by the new test build option.
How about following?
#pragma weak func1
#pragma weak func2
#pragma weak func3
...
gcc -include weak.h ...
I never tried this approach myself, but doing that your code won't be affected, which seems what you want most
PS
just tried it, for couple of symbols source file seems to work on Ubuntu 18.04 x64