Question: I have a file - in a large legacy codebase - containing methods that access databases.
No classes are used, just a header file with the method declarations, and the source file with the implementation.

I want to override these methods to eliminate the DB access during unit testing.  

I thought of the following options:  

1. Make file into class and override methods.  

The main minus here is that it results in alot of changes throughout the codebase.  

Not ideal, though it does improve the code...
2. Wrap the whole source file with an `#ifdef PRODUCTION_CODE` and create a new source file containing the stub and wrap it with the opposite, i.e. make the whole thing compilation dependent. Problem here is that in a build system that performs regression tests I would have to compile twice, once in order to create the apps and do regression tests, and an additional time to create the unit test executables.

Any recommended ways of doing this?

Answer: 
You can try to override at the linker level. Have two different .cpp files that implement the functions in the header that describes the DB interface - one calling the real DB, one calling a fake interface. When you link for unit test, replace one with the other (Target Specific Variables in GNU make may help).
You may also want to look at Michael Feathers' book [*Working Effectively with Legacy Code*](https://rads.stackoverflow.com/amzn/click/com/0131177052). Not only does he discuss exactly these types of problems, but the book includes numerous examples in C++ (in addition to Java, C, and C#). Feathers is also the original creator of [CppUnit](http://sourceforge.net/apps/mediawiki/cppunit).