![]() |
Home | Libraries | People | FAQ | More |
If there is considerable logic in an appliation's main
function, this can be difficult to unit test. The One
Definition Rule prevents us from having the application's main
and the unit test executable's main
in the same executable.
Furthermore, even if we could perform some trick to resolve the multiply
defined symbol[4], we would still need to figure out how to link the test executable
with the application's main
function to test the code.
When it becomes difficult to test code where it is currently, such as in
the body of main
, the simplest thing to do is to move
the code somewhere else that makes it easy to test. Here is one way to accomplish
this:
main
to app_main
.
app_main
to a library.
main
that calls app_main
.
app_main
by linking against the library.
The delegating main
looks like this:
extern int app_main(int argc, char* argv[]); int main(int argc, char* argv[]) { return app_main(argc, argv); }
If you don't already have a library for app_main
, then
create one and add all the other application code to the library as well.
The build logic for making the executable will consist of compiling a single
source file containing your delegating implementation of main
and linking against a library containing the rest of the application. This
will give you a starting point for writing unit tests for anything in your
application.