My 100 Daze of Code
https://github.com/davidjwalling/100-days-of-code
Visual Code Debugging on macOS and Linux
We have at least two low-level debugging options on macOS, gdb and lldb. While gdb can be used, it requires that the gdb program be code-signed. Seemingly, from posts on the interwebs, this process differs across more recent macOS versions. And, even then, it is clearly not effective for some users. I prefer using lldb on macOS. However, with this approach, an additional extension is installed in Visual Code to provide the "glue" between lldb itself and Visual Code. More details on that below.
Today, I'll also provide an alternate way to organize the source code and consolidate the three existing CMakeLists.txt files into one. Where before I had compartmentalized the source into library and executable folders, libava and ava, respectively, I've combined everything at the project root level to demonstrate a simplified organization.
When debugging in Visual Code, the launch.json file can be used to define multiple debugging configurations for different operating system environments. For example, we'll use gdb on Linux but lldb on macOS. What I'm curious about here is that Visual Code seems to warn on paths and identifiers not known on a specific platform. For example, I can define two configurations in launch.json, one for macOS and one for Linux. But, on Linux, VS Code warns on macOS paths not found. This may take further research.
Lastly, and, hopefully, no one will mind this, I've begun to pare down the contents of the repo to include only files necessary to this project. I'll use this blog to reflect progress and thoughts.
Revisiting Visual Code Extensions
For Linux, only the "C/C++ Extension Pack" extension for Visual Code had been installed. This works well for Linux, since gdb debugging is supported inherently. But on macOS, which uses lldb, we use an additional extension, "CodeLLDB" by Vadim Chugunov. There are other lldb extensions, but I had the best success with this one and I think that is reflected in the download counts.
Note that "C/C++ Extension Pack" is in fact a set of six extensions, at least at this time. With CodeLLDB added, the total installed extensions on macOS look like this now.
Simplify Code Organization
Up to now, we've organized source into library and executable folders, each with its own cmake file. Here, we'll use a much simpler, flatter organization and merge the three CMakeLists.txt files into one.
Notice that we now have two files in the .vscode folder. The first, "c_cpp_properties.json," defines information about our platform configurations. The second, "launch.json," now contains debugging information for both the macOS and Linux platforms.
c_cpp_properties.json
We now have gdb and lldb configurations in launch.json for our debugging.
Consolidated CMakeLists.txt
The consolidated "CMakeLists.txt" file defines both the library (libava) and the executable (ava). It also defines both build and install operations.
The build operation is the same as before, only performed at the repository base. Note, however, that we now include the "-DCMAKE_BUILD_TYPE=Debug" option when building with cmake.
Reconfirm Debugging on Linux
After our updates, repeat our debugging on Linux so that we can confirm that our headers are found properly and that single-step debugging is operational. Note that we're running the "(gdb) Launch" debugging configuration defined in launch.json.
Confirm Build on macOS
Update the repository and "git pull" again on macOS to update the local repository on our Mac. Follow the same build instructions.
If you look closely, you'll see I added an "xcrun xcodebuild" statement to suppress those annoying xcodebuild warnings.
Debug with Visual Code on macOS
Set a breakpoint on the IDriver declaration in main.cpp. This is because we set CodeLLDB's "stopOnEntry" attribute to false. When set to true, I saw that it stopped at the dylib loader instead of the main routine.
Step into until the terminal output is shown. Then "F5" to complete.
Comments
Post a Comment