I have a library (let's call it MyLib
) which has the following structure:
mylib_global.h - determines whether a Q_DECL_IMPORT
or Q_DECL_EXPORT
is behind the MYLIBSHARED_EXPORT
prefix which is used to expose specific symbols of the library when writing an application that uses the library:
#ifndef MYLIB_GLOBAL_H
#define MYLIB_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(MYLIB_LIBRARY) // Set using DEFINES inside MyLib.pro
# define MYLIBSHARED_EXPORT Q_DECL_EXPORT
#else
# define MYLIBSHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // MYLIB_GLOBAL_H
MyLib.h - contains the main class which also includes various other classes:
#include "mylib_global.h"
#include "MyLibOtherClass1.h"
#include "MyLibOtherClass2.h"
// Symbol will be exposed to the application that links against MyLib
// and can be directly interacted with from the application's code
class MYLIBSHARED_EXPORT MyLibMainClass {
public:
...
private:
MyLibOtherClassA ocA;
MyLibOtherClass1 oc1;
MyLibOtherClass2 oc2;
...
}
// Symbol will not be exposed to the application that links against MyLib
// however it is indirectly used through the `MyLibMainClass`
class MyLibOtherClassA {
...
}
Source files and other classes (header+source) - contain the declarations and definitions for the underlying logic of MyLib
.
As you can see I have a mixture of classes - some are defined in the same header file as the library's main class (it controls all other class instances), others are in different header files. There are just very few classes with the MYLIBSHARED_EXPORT
which I need to expose in order to be able to access the library's features when linking against it.
This is perfectly fine when writing a normal application. However I've decided to write some tests (using the Qt test framework) since I alter the code quite often (for now :)), there are many complex dependencies in there and I want to make sure that after each change all tests I have written are covered and the functionality remains the same as intended.
When writing tests I need to access ALL classes to ensure that every cog is turning in the right direction (even though it's not exposed to the outside). How do I deal with this? I can add MYLIBSHARED_EXPORT
in front of all classes but this will also allow normal users to see stuff, that is only intended to be used indirectly. This can be regulated more precisely using another #define
which can be set through the project's settings and only when defined all class that don't have the prefix will get one, otherwise they will be not exposed. While this solution is very likely to do the trick it will create a code mess, which I'd like to avoid if possible.
Because lots of the functionality are not exported, you've already set on not testing the final deliverable: you can't just grab the final product and run the test harness on it.
Then, the simplest solution is to include all the headers/sources into the test harness itself.
You can factor out the list of sources and headers into a separate file, that would be included in the project file for the deliverable, and for the test harness. This is easy to do in both cmake and qmake.