Issue
I've been switching a project to CMake and VSCode on my Mac, and encountered a baffling problem: when I enable LTO/IPO, VSCode's C++ debugger doesn't highlight the execution line when breaking. This appears to affect any build target for which I enable IPO.
Here's a minimum example demonstrating the problem:
[main.cpp]
#include <iostream>
int main()
{
std::cout << "This is some text!" << std::endl;
__asm__("int $3"); // Alternatively, use a breakpoint
return 0;
}
[CMakeLists.txt]
cmake_minimum_required(VERSION 3.23.0)
project(TestLTODebugging)
include(CheckIPOSupported)
check_ipo_supported()
add_executable(example main.cpp)
set_target_properties(example PROPERTIES
INTERPROCEDURAL_OPTIMIZATION TRUE
)
I'm using Clang and Ninja, using quick-debugging via VSCode's CMake Tools extension. When run, the app appears to pause as expected, but the line is not highlighted:
And if all I do is turn off IPO/LTO, the issue goes away:
Anyone know where to look for the problem? I'm new to CMake and using VSCode for C++, but this seems super basic and I can't find anything online about this problem. I've had no problems with this when generating an Xcode project with the same source.
While I'm primarily going to be debugging without optimizations, the project I'm working on requires working with optimizations enabled a lot of the time, and I want the debugger to work. I appreciate any help or advice.
EDIT: I've confirmed that this issue does not occur on my PC, where I also tested with Ninja, Clang, and the VSCode debugger. So the problem is either with my Mac environment or is Mac-specific?
EDIT 2: I've now confirmed this affects me with both Ninja and Make files on my Mac. Additionally, both Ninja.build and compile_commands.json (when using Make) show that the only difference between enabling and disabling IPO is the addition of the -flto=thin
flag. The -g
flag is never removed, and using nm
in Terminal shows that the debug symbols are still being generated. I'm now more confident that this is a bug with VS Code or CMake Tools.
Solution
I faced a similar problem -- in my case I source annotations for Instruments were missing.
From here:
Note
On Darwin, when using
-flto
along with-g
and compiling and linking in separate steps, you also need to pass-Wl,-object_path_lto,<lto-filename>.o
at the linking step to instruct the ld64 linker not to delete the temporary object file generated during Link Time Optimization (this flag is automatically passed to the linker by Clang if compilation and linking are done in a single step). This allows debugging the executable as well as generating the.dSYM
bundle using dsymutil(1).
Additional information here.
TL;DR: just add -Wl,-object_path_lto,lto.o
to your linker options -- e.g. for CMake: add_link_options(-Wl,-object_path_lto,lto.o)
.
Answered By - swineone Answer Checked By - Senaida (WPSolving Volunteer)