microsoft / microsoft/vscode-cpptools

Enable support for switching between different output targets with compile_commands.json

Open
#10,508 5 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Feature Request Feature: Configuration Language Service
Dominant language
TypeScript
Stars
6.2k
Forks
1.7k
Avg merge
14h 46m
Merged PRs (30d)
61

Description

    Hi @sean-mcmanus 

Here is a minimal example of this scenario

main.cpp

#include <iostream>

void func1()
{
#ifdef TARGET_1
    std::cout << "Target 1" << std::endl;
#elif defined(TARGET_2)
    std::cout << "Target 2" << std::endl;
#else
    std::cout << "Other" << std::endl;
#endif
}

int main()
{
    func1();
    return 0;
}

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/bin/g++",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "linux-gcc-x64",
            "compileCommands": ".vscode/compile_commands.json"
        }
    ],
    "version": 4
}

compile_commands.json

[
    {
        "file": "/home/karim/test/main.cpp",
        "directory": "/home/karim/test/build",
        "arguments": [
            "g++",
            "-c",
            "-o",
            "b.o",
            "-DTARGET_2",
            "/home/karim/test/main.cpp"
        ],
        "output": "/home/karim/test/build/b.o"
    },
    {
        "file": "/home/karim/test/main.cpp",
        "directory": "/home/karim/test/build",
        "arguments": [
            "g++",
            "-c",
            "-o",
            "a.o",
            "-DTARGET_1",
            "/home/karim/test/main.cpp"
        ],
        "output": "/home/karim/test/build/a.o"
    }
]

Note that "output" field is used to distinguish between different processing modes (output file) of the same source file as documented by the Json Compilation Database Specification

compile_commands_1

compile_commands_2

Originally posted by @KarimAshraf1995 in https://github.com/microsoft/vscode-cpptools/discussions/10408#discussioncomment-4893606

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how compile_commands.json entries are selected for a source file, using the two entries for main.cpp and their output fields as the behavioral example. Check how c_cpp_properties.json connects the compilation database to IntelliSense. Done means the extension can distinguish and switch between the TARGET_1 and TARGET_2 processing modes instead of treating one command as canonical.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, vscode
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.