Qt Creator Static Library With Unit Test Suite
This page describes the standard technique used within SPT to create static library (the same technique is used to tie a unit test suite to a Qt desktop application project).
Create Static Library Project
- Create a new static library project using the Qt Creator new project assistant, or by creating a text file similar to sptcore.pro.
- Add source files to your library as appropriate.
- Create a new application project using Qt Creator or by creating a text file similar to coretest.pro.
- Add the test source files to the unit test application project. You can use the main.cpp and AutoTest.h files from the Qt Blog to automatically execute your test suite(s).
- With both the static library and unit test application projects open in Qt Creator, add the static library project as a dependency for the unit test application project. This will ensure that the library is built prior to attempting to build the unit test application. Note that you need to perform this step each time you run Qt Creator (version 2.0.1, hopefully saved as setting in future versions).
- You will now be able to compile both your static library and unit test applications.
Mac OS X Notes
On Mac OS X, the linker does not retain the location of the static library as specified in the project file. Running
otool -Lwill show that the executable expects the static library in the same directory as the executable (may be same on Windows). Mac OS X gcc does not honour the Rpath setting, nor does it have anything as simple as
crle. They do provide a very robust
install_name_toolutility that can be used to remap the location of linked libraries, but that is an overkill for simple unit tests (and a very elegant solution for shipping self contained application bundles).
The trick is to add an additional build step that copies the static library from the library build directory to the unit test application build directory. This will ensure that the library is always available when the unit test application is run.