Search code examples
ctestingattributesweak

Is it bad practice to use the weak attribute to write test code in c?


Is it good practice to use the weak attribute in production code so that for test code one can override functions?


Solution

  • I don't know if it is good practice, but there are ways to override functions in test code that do not interfere with production code:

    • using macros that redefine the function names you wish to override
    • using LD_PRELOAD
    • using macros that expand to __weak in debug mode and to nothing in production mode.