Reputation: 59575
I have a legacy project that serves as a library even though it compiles as an executable. Other projects link with the individual object files that get produced in the process of creating the executable. So this is neither a static, dynamic, nor header-only library. My current plan is to convert it to a static library.
Every other project needs to reference OBJ files from the library, like so:
+ mylib (executable; yes, really)
+ myclass.h
+ myclass.cpp
+ myclass2.h
+ myclass2.cpp
+ main.cpp (contains a main method with "unit tests")
+ myapp (executable)
--> reference needed to myclass.obj and myclass2.obj
+ main.cpp (starts the app)
+ mylib.test (unit test project, future replacement for mylib/main.cpp)
--> reference needed to myclass.obj and myclass2.obj
+ myclasstest.cpp
+ myclass2test.cpp
Does this approach have a name? I want to know the name, so that I can then use it in a search engine in order to find ways of converting mylib type into something else.
I see that this is not ideal and has many disadvantages. I'm not asking for that.
Upvotes: 1
Views: 137
Reputation: 61575
It looks as if the legacy project was a multi-source-file program project that was amateurishly adapted for unit-testing.
You want to refactor it non-amateurishly and you have the right idea.
The customary basic approach to structuring a program project for unit-tests is make it build three targets:
Application Library: A static library archiving the object files compiled from all of
the app's sources except for the one that defines the app's main
. The source that defines main
should not define any further functionality: everything else should be defined in
the Application Library.
Unit Test Driver: An executable that runs the unit tests. This target has its own
source code to define it's main
and implement the unit-tests. This target links
agains the Application Library to acquire the functionality to be unit-tested.
Application: This only has source code to define the app's main
. It also
links against the Application library, to acquire all the rest of its functionality.
The Application and the Unit Test Driver targets both depend on the Application Library and are otherwise independent.
Unit testing frameworks such as GoogleTest and Boost Test can massively reduce the coding cost of the unit tests, perhaps even defining the main function for the Unit Test Driver.
There isn't an industry standard name for a "library" that just consists of a set of object files, because any actual library is a distinct artefact made from set of object files. But a build-system generator might define a "virtual library" as a set of object files that it represents by a single symbol, and the CMake system calls such a library an object library.
Upvotes: 0