I finally tried out what I wanted to try for some time now. The problem (if you can call it a problem) I had is that one of the libs I worked on for my GSoC contained class that I wanted to test but that I did not want to expose to the rest of the world. As I did not have the time nor the knowledge to make that happen I just exported the classes so that the testcode could link against it.
Well that changed today and I took some time to figure this out. (Note: I’m not an expert on C++, libraries and such so if I do stupid things here, let me know). In the KPilot code we have an KPILOT_EXPORT define which looks like this:
#include <kdemacros.h>
#ifndef KPILOT_EXPORT
# if defined(MAKE_KPILOT_LIB)
/* We are building this library */
# define KPILOT_EXPORT KDE_EXPORT
# else
/* We are using this library */
# define KPILOT_EXPORT KDE_IMPORT
# endif
#endif
The way I understand this snippet is that it makes the symbol that is prefixed with it visible anyhow. But what I wanted is that the symbol is only exported when the tests are enabled. Therefore I added another define which looks like this:
#ifndef KPILOT_TEST_EXPORT
# if defined(HAVE_TESTS_ENABLED)
# define KPILOT_TEST_EXPORT KDE_EXPORT
# else
# define KPILOT_TEST_EXPORT KDE_NO_EXPORT
# endif
#endif
To make this work I added this to the CMakeLists.txt file:
string(COMPARE EQUAL ${KDE4_BUILD_TESTS} "ON" buildtests)
macro_bool_to_01(buildtests HAVE_TESTS_ENABLED)
configure_file(config-kpilot.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-kpilot.h )
And the config-kpilot.h.cmake file got a line added that looks like:
#cmakedefine HAVE_TESTS_ENABLED
Now I can use:
class KPILOT_TEST_EXPORT IDMapperXmlSource
{
....
};
and the given class gets only global in the library when testing is enabled. I wondered why there is no such export in kdemacros.h as I found this quite a nice solution. The library won’t export symbols in release mode, which is a bit cleaner than just exporting everyting only to be able to test imo. But as said, I’m only a novice on this kind of things =:). This whole thing made wondering if there is some GUI tool to explore libraries.