C/C++ for Visual Studio Code

repository·main·Indexed 27 days ago

https://github.com/microsoft/vscode-cpptools

An extension providing advanced language support for C and C++ developers, including IntelliSense, integrated debugging, and code browsing. It supports MSVC, Clang, and GCC across Windows, Linux, and macOS. Note that the extension does not include a compiler or debugger; these must be pre-installed on the system.

Tokens
10.3K
Snippets
24
Records
54
Agent score
91%

What's inside vscode-cpptools

  1. Use C/C++ Extension UI Themes

    main

    The C/C++ Extension UI Themes provide color schemes designed to match Visual Studio themes. These themes include support for semantic colorization scopes used by the C/C++ extension, ensuring that C/C++ code elements are correctly highlighted.

    Available theme styles include:

    • Modern Themes: Closely resemble the Enhanced themes from recent versions of Visual Studio.
    • 2017 Themes: Replicate the look of Visual Studio 2017.

    To use these, install the cpptools-themes package within VS Code and select your preferred theme from the Color Theme picker.

  2. Build the extension

    main

    You can build the extension using several methods depending on your workflow:

    Using VS Code Tasks

    Press ctrl-shift-b to access build tasks:

    • Compile: Performs a one-time build.
    • Watch: Compiles changed files automatically as you save them. This is recommended for active development.

    Using the Command Line

    Use the following Yarn commands:

    • yarn compile: Builds the extension once.
    • yarn watch: Watches for changes and recompiles automatically.

    Using F5 Debug

    When using the launch.json configurations, a preLaunchTask is automatically triggered which runs yarn watch and waits for the build to be ready before launching.

    yarn compile
    # or
    yarn watch
  3. Generate native strings and options schema

    main

    These commands are used when modifying the underlying data structures of the extension.

    • yarn generate-native-strings: Generates nativeStrings.ts and localized_string_ids.h from ./src/nativeStrings.json. Use this if you add localized strings to the native side.
    • yarn generate-options-schema: Inserts the options schema into package.json using tools/OptionsSchema.json and tools/VSSymbolSettings.json.
    yarn generate-native-strings
    yarn generate-options-schema
  4. Configure debugger entitlements for macOS

    main
    When running the debugger on macOS, specific entitlements are required for the process to execute correctly. This repository provides the necessary .plist entitlement files used by the debugger to comply with macOS security requirements. These entitlements are aligned with those used by the .NET runtime.
  5. Use an isolated VS Code environment

    main

    The repository supports running VS Code and the extension in a completely isolated environment (separate VS Code installation, private extensions, and user folders). This is useful for testing without affecting your main VS Code setup.

    • Launch isolated VS Code: Use yarn code.
    • Reset environment: To delete the isolated folders and configuration files, use yarn code reset or yarn test reset. The next time you run yarn code or yarn test, a fresh environment will be reinstalled.

    Note: The isolated environment uses a blue theme to distinguish it from your standard VS Code. However, when debugging scenario tests, you cannot use the isolated environment because VS Code requires the extension host to use the same binary as the debugger.

    yarn code
    yarn code reset
  6. Configure IntelliSense for MinGW (Extension version 0.16.1 and earlier)

    main

    For users of MinGW on Windows running extension version 0.16.1 or earlier, you must manually configure c_cpp_properties.json to ensure IntelliSense works correctly.

    Key requirements:

    1. intelliSenseMode: Set this to clang-x64.
    2. includePath: You must include the specific system header paths used by your MinGW installation. You can find the correct paths by running gcc -v -E -x c++ nul in your terminal.
    3. defines: You must define __GNUC__=# where # is the major version of your installed toolchain (e.g., __GNUC__=6).
    4. Version Matching: Ensure all paths in includePath and browse.path match your specific MinGW version number (e.g., changing 6.3.0 to your installed version).

    To create this file, select C/Cpp: Edit Configurations from the VS Code command palette.

    {
        "configurations": [
            {
                "name": "MinGW",
                "intelliSenseMode": "clang-x64",
                "includePath": [
                    "${workspaceRoot}",
                    "C:/MinGW/lib/gcc/mingw32/6.3.0/include/c++",
                    "C:/MinGW/lib/gcc/mingw32/6.3.0/include/c++/mingw32",
                    "C:/MinGW/lib/gcc/mingw32/6.3.0/include/c++/backward",
                    "C:/MinGW/lib/gcc/mingw32/6.3.0/include",
                    "C:/MinGW/include",
                    "C:/MinGW/lib/gcc/mingw32/6.3.0/include-fixed"
                ],
                "defines": [
                    "_DEBUG",
                    "__GNUC__=6",
                    "__cdecl=__attribute__((__cdecl__))"
                ],
                "browse": {
                    "path": [
                        "C:/MinGW/lib/gcc/mingw32/6.3.0/include",
                        "C:/MinGW/lib/gcc/mingw32/6.3.0/include-fixed",
                        "C:/MinGW/include/*",
                        "${workspaceRoot}"
                    ],
                    "limitSymbolsToIncludedHeaders": true,
                    "databaseFilename": ""
                }
            }
        ]
    }
  7. Build and Debug the Active C++ File

    main

    You can automatically configure build tasks and start a debug session for your current C++ file using the 'Build and Debug Active File' feature. This feature automates the creation of configuration files and the execution of the build/debug cycle.

    There are three ways to trigger this:

    1. Command Palette: Open the command palette and select C/C++: Build and Debug Active File. This generates a tasks.json file, builds the active source file, and launches the debugger.
    2. Context Menu: Right-click anywhere in the editor field while editing a file in your workspace and select Build and Debug Active File. This also generates a tasks.json file, builds the file, and launches the debugger.
    3. Keyboard Shortcut: Press F5. This method is more comprehensive as it configures both a tasks.json and a launch.json file, builds the active source file, and then launches the debugger.
  8. Debug MIEngine on Linux or macOS with MonoDevelop

    main

    To debug MIEngine on Linux/macOS using MonoDevelop (Xamarin Studio):

    Prerequisites:

    Setup:

    1. Launch MonoDevelop with the remote attach flag:
      cd "\Program Files (x86)\MonoDevelop\bin"
      set MONODEVELOP_SDB_TEST=1
      MonoDevelop.exe
    2. Create an empty 'Misc/Generic Project' in MonoDevelop to enable the Run menu.
    3. In the project 'Options' -> 'Run' -> 'Custom Commands', set 'Execute' to a valid command (e.g., c:\windows\notepad.exe).
    4. Enable remote debugging in the extension by opening ~/.vscode/extensions/ms-vscode.cpptools-<version>/debugAdapters/OpenDebugAD7 and uncommenting the line at the bottom.

    Attaching:

    1. In MonoDevelop, go to Run -> Run With -> Custom Command Mono Soft Debugger.
    2. Enter the IP and port of the Linux/macOS machine and click Connect.
    cd "\Program Files (x86)\MonoDevelop\bin"
    set MONODEVELOP_SDB_TEST=1
    MonoDevelop.exe
  9. Structure and creation of tests

    main

    The test architecture is divided into Unit tests and Scenario tests:

    Test Directory Layout

    • test/common/: Infrastructure and common code.
    • test/unit/: Low-level unit tests (not running in a VS Code environment). Must be in *.test.ts files.
    • test/scenarios/<SCENARIONAME>/: Scenario tests using @vscode/test-electron.
      • assets/: Workspace files. If a *.code-workspace file exists, it is used as the workspace; otherwise, the folder itself is used.
      • tests/: Location of Mocha tests. Must be in *.test.ts files.

    Running and Debugging Tests

    • Unit Tests: Run via the VS Code Test Explorer (requires mocha test explorer) or via the command line using yarn test.
    • Scenario Tests: To debug scenario tests, select the VS Code Tests option in the debugger and press F5. Note that you cannot use the isolated VS Code environment for this.