MSVC: tests: fix linking with yangobj
This fixes the LNK4217 linker warning. This project for some reasons
creates an object library (`yangobjs` target in CMake) and proceeds to
link tests against that instead of the usual way of linking against
`libyang` the library properly. This is a problem on MSVC because the
tests would normally process the API symbols as `__declspec(dllimport)`,
while at the same time linking the object library with an implementation
of these symbols into the same image. At the same time we cannot simply
set `-DLIBYANG_BUILD` globally because we're *also* building tools such
as `yanglint` which does *not* embed these object files, but links
against `-lyang` normally, and that one needs `__declspec(dllimport)`.
The alternative would be to get rid of the `yangobj` altogether, which
would make the code more straightforward IMHO. That would also make the
previous commit (`MSVC: tests: link with all the compat libraries`)
superfluous because the usual rules of CMake transitive linking of
libraries would take over. However, Michal indicated that the reason of
`yangobj` is that some of these tests are for internal, unexported
functions, which means that `yangobj` will have to stay for now.
See-also: https://docs.microsoft.com/en-us/cpp/error-messages/tool-errors/linker-tools-warning-lnk4217
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index c1fe852..6f36f31 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -14,6 +14,7 @@
endforeach()
add_executable(${TEST_NAME} ${TEST_SOURCES} $<TARGET_OBJECTS:yangobj>)
+ target_compile_definitions(${TEST_NAME} PRIVATE LIBYANG_BUILD)
# Set common attributes of all tests
set_target_properties(${TEST_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests")