I am working on a Project, which is installed by defining export LD_DIBRARY_PATH=...
.
My Problem is, that when I'm developing, the compiled test files linking to the installed library and not to the currently compiled library.
My project is depended on another project inside my LD_DIBRARY_PATH
, so I cannot unset the path for testing.
I found the following questions, however they could not help me:
- CMake: link test with library in build directory, not with installed version
- gtest is linking to an installed library
I could reproduce this with the following example:
./CMakeLists.txt
./src/libA.cpp
./src/CMakeLists.txt
./include/libA.hpp
./test/test_libA.cpp
./test/CMakeLists.txt
CMakeLists.txt
cmake_minimum_required(VERSION 3.19)
project(
LibraryTest
VERSION 0.1
LANGUAGES CXX)
add_subdirectory(src)
enable_testing()
add_subdirectory(test)
install(
TARGETS ${PROJECT_NAME}Lib
EXPORT ${PROJECT_NAME}Targets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
install(
EXPORT ${PROJECT_NAME}Targets
FILE ${PROJECT_NAME}Targets.cmake
NAMESPACE ${PROJECT_NAME}::
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)
include/libA.hpp
#pragma one
bool foo();
src/CMakeLists.txt
add_library(
${PROJECT_NAME}Lib SHARED
libA.cpp)
target_include_directories(
${PROJECT_NAME}Lib PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include/>)
add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME}Lib )
src/libA.cpp
#include "libA.hpp"
bool foo()
{
return true;
}
test/CMakeLists.txt
# Download GTest
include(FetchContent)
option(INSTALL_GTEST "Enable installation of googletest." OFF)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest
GIT_TAG v1.13.0)
FetchContent_MakeAvailable(googletest)
include(GoogleTest)
# Add test
add_executable(test_libA test_libA.cpp)
target_link_libraries(
test_libA
${PROJECT_NAME}::${PROJECT_NAME}
GTest::gtest_main
GTest::gtest
GTest::gmock
)
gtest_add_tests(TARGET test_libA)
test/test_libA.cpp
#include "libA.hpp"
#include <gtest/gtest.h>
TEST(libA, dummy)
{
EXPECT_TRUE(foo());
}
int main(int argc, char *argv[])
{
::testing::InitGoogleTest(&argc, argv);
auto result = RUN_ALL_TESTS();
return result;
}
Building with cmake 3.22.1
and g++ 12.1.0
:
mkdir build
cd build
cmake ..
make
make install
ldd
results in the following output
ldd test/test_libA
libLibraryTestOut.so => $LD_LIBRARY_PATH/lib/libLibraryTestLib.so