microsoft / vscode-cmake-tools

CMake integration in Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=vector-of-bool.cmake-tools
MIT License
1.45k stars 443 forks source link

in vscode, How to use v141 toolset in VS2022 with cmake #3883

Closed iGwkang closed 1 month ago

iGwkang commented 1 month ago

Brief Issue Summary

The problems I searched for are different from my scenario. In my VSCode, I set cmake.generator to Ninja, and I have installed v141(VS2017) toolset in my VS2022. I have to use Ninjabecause I want to generate compile_commands.json At this point I want to use the v141 toolset in VS2022 in VSCode. what should I do?

I've tried adding the following to cmake-tools-kits.json:

  {
    "name": "Visual Studio Professional 2022 Release - amd64 (v141)",
    "visualStudio": "3254f39f",
    "visualStudioArchitecture": "x64",
    "preferredGenerator": {
      "name": "Visual Studio 15 2017",
      "toolset": "v141,host=x64"
    },
    "isTrusted": true
  },

But it didn't work.

CMake Tools Diagnostics

[proc] D:\dev_env\CMake\bin\cmake.EXE --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Release -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -SD:/work/remote_desktop -Bd:/work/remote_desktop/build -G Ninja
[cmake] -- The C compiler identification is MSVC 19.40.33812.0
[cmake] -- The CXX compiler identification is MSVC 19.40.33812.0
[cmake] -- Detecting C compiler ABI info
[cmake] -- Detecting C compiler ABI info - done
[cmake] -- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/MSVC/14.40.33807/bin/Hostx64/x64/cl.exe - skipped
[cmake] -- Detecting C compile features
[cmake] -- Detecting C compile features - done
[cmake] -- Detecting CXX compiler ABI info
[cmake] -- Detecting CXX compiler ABI info - done
[cmake] -- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/MSVC/14.40.33807/bin/Hostx64/x64/cl.exe - skipped
[cmake] -- Detecting CXX compile features
[cmake] -- Detecting CXX compile features - done

Debug Log

No response

Additional Information

No response

v-frankwang commented 1 month ago

@iGwkang I reproduced this issue using a simple project, after I added the following code:

{
    "cmake.configureSettings": {
        "CMAKE_GENERATOR_TOOLSET": "v141",
     }
}

to the settings.json file, the generated project uses the toolset v141.

Actual result: image image

iGwkang commented 1 month ago

@v-frankwang Thank you for your answer, this didn't solve my problem. I mentioned above that I need to use Ninja because I need to generate compile_commands.json for clangd code prompts.

v-frankwang commented 1 month ago

@iGwkang I'm very sorry, I didn't understand what you were trying to say, can you re-describe this problem you have? Could you provide a simple project about your problem and some screenshots? This will make it easier for us to investigate this issue. Looking forward to your reply!

iGwkang commented 1 month ago

@v-frankwang Sorry, my English is not very good. image

I need to set cmake.generator to Ninja to generate the compile_commands.json file for clangd. However, while using Ninja, I want it to call the v141 toolset from VS2022, using the VS2017 compiler. The method you provided would generate a VS solution instead of the compile_commands.json file.

v-frankwang commented 1 month ago

@gcampbell-msft Users wanting to use the Ninja generator use the v141 toolset from VS2022 and use the compiler from vs2017. I get an error when I set the CMake generator to Ninja using v141toolset, can you provide me with some help?

Below is my reproduction project: image

gcampbell-msft commented 1 month ago

@iGwkang My suggestion would be to use CMakePresets.json. This would allow you to use the toolset field to control what toolset you want to use.

You could, within the Preset and within a configure preset, define the following:


      "toolset": {
        "strategy": "external",
        "value": "v141"
      },
iGwkang commented 1 month ago

@gcampbell-msft Thanks for your answers.

Although this method can use the v141 toolset, it cannot generate compile_commands.json, even though I have set CMAKE_EXPORT_COMPILE_COMMANDS.

{
    "version": 8,
    "configurePresets": [
        {
            "name": "Ninja",
            "displayName": "Ninja",
            "description": "",
            "generator": "Ninja",
            "binaryDir": "${sourceDir}/out/build/${presetName}",
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Release",
                "CMAKE_CXX_COMPILER": "cl.exe",
                "CMAKE_C_COMPILER": "cl.exe",
                "CMAKE_TOOLCHAIN_FILE": "",
                "CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}",
                "CMAKE_EXPORT_COMPILE_COMMANDS": "ON"
            },
            "toolset": {
                "strategy": "external",
                "value": "v141,host=x64"
            }
        }
    ],
    "buildPresets": [
        {
            "name": "Ninja-v141-Release",
            "description": "Ninja-v141-Release",
            "displayName": "Ninja-v141-Release"
        }
    ]
}

C:\dev_env\CMake\bin\cmake.EXE -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=cl.exe -DCMAKE_C_COMPILER=cl.exe -DCMAKE_TOOLCHAIN_FILE= -DCMAKE_INSTALL_PREFIX=D:/work/raylink/remote_desktop/out/install/Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -SD:/work/raylink/remote_desktop -BD:/work/raylink/remote_desktop/out/build/Ninja -G Ninja [cmake] -- The C compiler identification is MSVC 19.16.27051.0 [cmake] -- The CXX compiler identification is MSVC 19.16.27051.0 [cmake] -- Detecting C compiler ABI info [cmake] -- Detecting C compiler ABI info - done [cmake] -- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/MSVC/14.16.27023/bin/HostX64/x64/cl.exe - skipped [cmake] -- Detecting C compile features [cmake] -- Detecting C compile features - done [cmake] -- Detecting CXX compiler ABI info [cmake] -- Detecting CXX compiler ABI info - done [cmake] -- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/MSVC/14.16.27023/bin/HostX64/x64/cl.exe - skipped [cmake] -- Detecting CXX compile features [cmake] -- Detecting CXX compile features - done

iGwkang commented 1 month ago

@gcampbell-msft Hi, Is there a correct way to do this?

v-frankwang commented 1 month ago

@iGwkang Thank you very much for your reply, I used the CMakePresets.json file in your comment above to verify this, and was able to generate the file you mentioned, compile_commands.json, after the configuration was complete!

ENV: CMake tools:v1.19.28(pre-release) C/C++:v1.21.4

Actual results: image

iGwkang commented 1 month ago

@v-frankwang Thanks, I recreated a cmake test project, and it did generate compile_commands.json, but it did not successfully use the v141 toolset. Your screenshot still uses the v143 toolset.

iGwkang commented 1 month ago

@v-frankwang @gcampbell-msft Thanks for your reply, I have solved it, here is my latest file

{
    "version": 8,
    "configurePresets": [
        {
            "name": "base",
            "displayName": "base",
            "description": "",
            "generator": "Ninja",
            "binaryDir": "${sourceDir}/build/${presetName}",
            "cacheVariables": {
                "CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
                "CMAKE_INSTALL_PREFIX": "${sourceDir}/install/${presetName}"
            },
            "architecture": {
                "value": "x64",
                "strategy": "external"
            },
            "hidden": true
        },
        {
            "name": "MSVC-v141",
            "cacheVariables": {
                "CMAKE_CXX_COMPILER": "cl.exe",
                "CMAKE_C_COMPILER": "cl.exe"
            },
            "toolset": {
                "strategy": "external",
                "value": "v141"
            },
            "hidden": true
        },
        {
            "name": "Debug",
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Debug"
            },
            "hidden": true
        },
        {
            "name": "Release",
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Release",
                "CMAKE_INTERPROCEDURAL_OPTIMIZATION": true
            },
            "hidden": true
        },
        {
            "name": "MSVC-v141-Release",
            "inherits": [
                "base",
                "Release",
                "MSVC-v141"
            ]
        },
        {
            "name": "MSVC-v141-Debug",
            "inherits": [
                "base",
                "Debug",
                "MSVC-v141"
            ]
        }
    ]
}