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.
This will work on Unix, but not Windows – that needs the corresponding KDE_IMPORT when using a library (as opposed to building it).
You should probably replace
# define KPILOT_TEST_EXPORT KDE_EXPORT
with
# define KPILOT_TEST_EXPORT KPILOT_EXPORT
iqUPpC skqshjhlwtwq, [url=http://kqaugihfxpwy.com/]kqaugihfxpwy[/url], [link=http://ufwxnlhxnnji.com/]ufwxnlhxnnji[/link], http://gjrhycrxfuof.com/
Instead of using a dynamic library, you can also use a static library for doing the unit tests. Put the cpp files in a variable and the do
add_library(mypublishedlib SHARED ${CPPFILES})
add_library(mytestlib STATIC ${CPPFILES})
add_executable(mytests test.cpp)
target_link_libraries(mytests mytestlib)
BKkSQW aoxvaqkjgqap, [url=http://wloivntxoovc.com/]wloivntxoovc[/url], [link=http://vpmctxoxbgvh.com/]vpmctxoxbgvh[/link], http://ttoudqwmpimu.com/
You may want to check codebases like Qt or KOffice to see how they do this
dKQhBv zmiwjgqylxlv, [url=http://xnrnklumiylu.com/]xnrnklumiylu[/url], [link=http://oxtwsynwaxiv.com/]oxtwsynwaxiv[/link], http://ruzwhrhpeiii.com/