[CMake] Unable to locate the project's shared library when running the tests
Roger Leigh
rleigh at codelibre.net
Wed Sep 2 04:15:46 EDT 2015
On 02/09/2015 08:40, Yaron Cohen-Tal wrote:
> Hi,
>
> My project is a shared library, and my tests are linked to that shared
> library. When I try to run the tests, they fail because they can't find
> the DLL of the project. The "CMakeLists.txt" of the tests is in a
> different folder ("test/") than that of the shared lib ("src/"), and
> therefore they're built in different directories. Currently I'm using
> Visual Studio, and the DLL of the project is built in "src\Debug\", and
> the executables of the tests are built in "test\Debug\"; That is, for a
> debug build of course. What would be the best solution to b able to run
> the tests? I'm currently with Visual Studio but I guess the same problem
> would arise on Linux and OS X, and it should work there too.
On Linux/BSD/MacOS it's using rpath so it works without any special
effort on your part.
On Windows, you do need to set the test environment so it can find all
the DLLs. To do this, I did this for libtiff:
macro(tiff_test_reader name command infile)
add_test(NAME "${name}"
COMMAND "${CMAKE_COMMAND}"
[... other options...]
"-DTIFFINFO=$<TARGET_FILE:tiffinfo>"
"-DLIBTIFF=$<TARGET_FILE:tiff>"
-P "${CMAKE_CURRENT_SOURCE_DIR}/TiffTest.cmake")
endmacro()
where TIFFINFO is an executable run by the test and LIBTIFF is the
library used by this executable. Note we pass these options as
generator expressions so you can run "ctest -C Debug|Release" and have
it use the executable and library from the debug or release build.
TiffTest.cmake is a wrapper to run the test. It's doing this:
# Add the directory containing libtiff to the PATH (Windows only)
if(WIN32)
get_filename_component(LIBTIFF_DIR "${LIBTIFF}" DIRECTORY)
file(TO_NATIVE_PATH "${LIBTIFF_DIR}" LIBTIFF_DIR)
set(ENV{PATH} "${LIBTIFF_DIR};$ENV{PATH}")
endif()
and then it runs the test command and specified options via
execute_process().
i.e. we've added a level of indirection when running the tests via a
standalone cmake script.
There may be better ways of doing this. I'd certainly be keen to
simplify things!
Regards,
Roger
More information about the CMake
mailing list