'CLion skips breakpoints when executing tests using gtest

I've got a simple C++ library with some tests using GTest:

.
├── CMakeLists.txt
├── LICENSE
├── nametract.cpp
├── nametract.h
├── README.md
└── tests
    ├── CMakeLists.txt
    └── nametract_test.cpp

I can run the tests just fine using CLion's gtest run configuration - it runs them as expected. It also does recognise all the tests in nametract_test.cpp - if I want, I can run them individually:

Some tests

However, when I set a breakpoint in my test case, or in nametract.cpp, and run my tests, all the breakpoints are skipped. It does say that it is running with debugger, the tests themselves are being run and CLion reports the breakpoints as being enabled - however, it doesn't stop code execution on those. What might be the cause of this problem? How could I fix it?

Main CMakeLists.txt:

cmake_minimum_required(VERSION 3.20)
project(nametract)

set(CMAKE_CXX_STANDARD 17)

add_library(nametract nametract.cpp)

add_subdirectory(tests)

CMakeLists.txt in tests folder:

include(FetchContent)

project(tests)

FetchContent_Declare(
        googletest
        GIT_REPOSITORY https://github.com/google/googletest.git
        GIT_TAG        955c7f837efad184ec63e771c42542d37545eaef
)

FetchContent_MakeAvailable(googletest)

add_executable(tests_run nametract_test.cpp)

target_link_libraries(tests_run nametract)
target_link_libraries(tests_run gtest gtest_main)


Solution 1:[1]

I've been trapped in the same problem for a day. I finally find a solution, and hope that is helpful.

I am using a Macbook Pro. What I did is quite simple:

  1. Unlink gtest from brew. Run brew unlink gtest command. And then add gtest source directory to my project. (yes I installed gtest via brew rather than build from source);
  2. Mute IPO support from CMakeLists.txt (I set CMAKE_INTERPROCEDURAL_OPTIMIZATION to true for performance. So now I set it to false). And I find this option will force gtest to be built as release version.

Here is my guess: gtest installed via brew install gtest has only release version; and IPO option also enforces gtest to be built in release version. And that's why none of the breakpoints is hit.

My situation may not be exactly same as yours. However check any possibility for switching release/debug must help.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jiajie Zhang