VS Code Setup for C/C++ Project: Part 2 CMake

Please check this post before reading this.

Step 1. Install CMake Tools extension by Microsoft

Install CMake Tools

Check cmake version on terminal

Step 2. Add CMAKELists.text

cmake_minimum_required(VERSION 3.20)
project(mixedCandCXX LANGUAGES C CXX)
add_executable(mixedCandCXX
main.cpp
helper.c
)

Clean up project folders that generated by VS Code Setup for C/C++ Project: Part 1.

Step 3. CMake: Configure

Step 4. Let’s build program using CMake: Build

Select CMake: Build

You can see Build finished

Step 5. Let’s run a program

To run a program you can choose 2 options

  • CMake: Debug
    • You can see the results in Debug Console
  • CMake: Run Without Debugging
    • You can see the results in Terminal Tab

If you want to check breakpoints use CMake: Debug others choose CMake: Run Without Debugging.

This is the output by running a CMake: Run Without Debugging option.

I ran into an issue when I selected the CMake: Debug. It is caused by conflicts with Swiftly.

Starting: "/Users/sungwookbaek/.swiftly/bin/lldb" --interpreter=mi error: unknown option: --interpreter=mi
Use 'Ildb --help' for a complete list of options.
"/Users/sungwookbaek/.swiftly/bin/lldb" exited with code 1 (0x1).

Fixing the LLDB Issue

The LLDB provided by Swiftly does not support the --interpreter=mi option required by the VS Code C/C++ debugger.

Let’s resolve this issue. Select a CMake: Select a Kit.

Next, Select Clang (Yellow Box)

Add a Breakpoint and run CMake: Debug again.

Now It works fine 🎉

Conclusion

In this post, we added CMake support to a mixed C and C++ project in VS Code.

We created a CMakeLists.txt file, configured the project, built the executable, and ran and debugged the program using CMake Tools. We also resolved an LLDB conflict caused by Swiftly by selecting the Clang kit.

This setup is enough for a small C/C++ project. However, as a project grows, manually managing source files and build settings can become difficult. CMake provides a more organized and portable way to manage the build process.

In the next post, I will explore CMake in more detail, including project structure, build types, and targets.

Comments

Leave a Reply

Discover more from Shawn

Subscribe now to keep reading and get access to the full archive.

Continue reading