How to add linker directories to cmake gtest_discover_tests

0

I’m using cmake and gtest for my project. My gtest executable is using the same name as the program executable. I know this is not ideal but I cannot change it.

The compiler I’m using is installed in a different directory than the system GCC (which is older). I’ve set the compiler path using:

set(CMAKE_C_COMPILER /opt/gcc-5.4.0/bin/gcc-5.4.0)
set(CMAKE_CXX_COMPILER /opt/gcc-5.4.0/bin/g++-5.4.0)

When linking my executable I’m using target_link_directories:

target_link_directories(target PUBLIC /opt/gcc-5.4.0/lib /opt/gcc-5.4.0/lib64)

Without gtest, the target compiles, links and runs properly, however when adding gtest:

  gtest_discover_tests(target WORKING_DIRECTORY <gtests_soruce_dir>)

The gtest target fails to link:

target: /lib64/libstdc++.so.6: version `CXXABI_1.3.8' not found (required by executable_name)
target: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by executable_name)
target: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by executable_name)
target: /lib64/libstdc++.so.6: version `CXXABI_1.3.9' not found (required by executable_name)

CMake Error at /home/gilkalish/.local/share/JetBrains/Toolbox/apps/CLion/ch-0/202.6397.106/bin/cmake/linux/share/cmake-3.17/Modules/GoogleTestAddTests.cmake:40 (message):
  Error running test executable.

    Path: 'executable_path'
    Result: 1
    Output:

This is the very last step while linking gtest, after the executable was linked.

/home/gilkalish/.local/share/JetBrains/Toolbox/apps/CLion/ch-0/202.6397.106/bin/cmake/linux/bin/cmake -D TEST_TARGET=target -D TEST_EXECUTABLE=/target_path/target -D TEST_EXECUTOR= -D TEST_WORKING_DIR=<gtest_source_dir> -D TEST_EXTRA_ARGS= -D TEST_PROPERTIES= -D TEST_PREFIX= -D TEST_SUFFIX= -D NO_PRETTY_TYPES=FALSE -D NO_PRETTY_VALUES=FALSE -D TEST_LIST=target_TESTS -D CTEST_FILE=/target_path/target[1]_tests.cmake -D TEST_DISCOVERY_TIMEOUT=5 -P /home/gilkalish/.local/share/JetBrains/Toolbox/apps/CLion/ch-0/202.6397.106/bin/cmake/linux/share/cmake-3.17/Modules/GoogleTestAddTests.cmake

I can add the path to my custom GCC libraries using LD_LIBRARY_PATH, then linking works, but I would like to find a solution that will allow me to specify in cmake where to look for that libraries when linking the tests.

Also tried updating to cmake 3.18 that gave the same results. Any tips will be appreciated, Thanks!

CMake doesn’t “look for” standard libraries such as libstdc++ generally. It’s up to the compiler to figure that out on its own. You shouldn’t need to add the link directories for them as the compiler should use the appropriate library for the compiler automatically. It seems that some other system library is adding the system path to the linker’s search paths and it finds that copy before the toolchain-provided one?