Wednesday, 3 March 2021

Integrating Visual Studio Compiler Tools With Visual Studio Code

Integrating Visual Studio compiler tools with Visual Studio Code is a really powerful solution for developing and debugging C/C++ code.

Assuming you have installed Visual Studio Code and Visual Studio compiler tools then before continuing I highly recommend installing the C/C++ for Visual Studio Code extension.

Here are the steps I have for the integration process:

Enable a "VS 2019 Community Prompt Here" integration with Explorer. I've based my solution on Daniel Cazzulino's excellent CommandPromptHere solution that is available here: https://github.com/kzu/CommandPromptHere.

This supports x86 compiler configuration by default so I've modified it slightly to enable x64 compilation and called the file "VS201964BitComCmdHere.inf"

;
; "CMD Prompt Here" PowerToy
;
; Copyright 1996 Microsoft Corporation
[version]
signature="$CHICAGO$"
[DefaultInstall]
CopyFiles = VS201964BitComCmdHere.Files.Inf
AddReg    = VS201964BitComCmdHere.Reg
[DefaultUnInstall]
DelFiles  = VS201964BitComCmdHere.Files.Inf
DelReg    = VS201964BitComCmdHereUninstall.Reg
[SourceDisksNames]
55="%VS201964BitComCmdHereName%","",1
[SourceDisksFiles]
vs2019cmdhere-enterprise.inf=55
[DestinationDirs]
VS201964BitComCmdHere.Files.Inf = 17
[VS201964BitComCmdHere.Files.Inf]
VS201964BitComCmdHere.INF
[VS201964BitComCmdHere.Reg]
HKLM,%UDHERE%,DisplayName,,"%VS201964BitComCmdHereName%"
HKLM,%UDHERE%,UninstallString,,"rundll32.exe syssetup.dll,SetupInfObjectInstallAction DefaultUnInstall 132 %17%\VS201964BitComCmdHere.inf"
HKCR,Directory\Background\Shell\VS201964BitComCmdHere,,,"%VS201964BitComCmdHereAccel%"
HKCR,Directory\Background\Shell\VS201964BitComCmdHere\command,,,"cmd.exe /k call ""C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat"" && pushd ""%V"""
HKCR,Directory\Shell\VS201964BitComCmdHere,,,"%VS201964BitComCmdHereAccel%"
HKCR,Directory\Shell\VS201964BitComCmdHere\command,,,"cmd.exe /k call ""C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat"" && pushd ""%V"""
HKCR,Drive\Shell\VS201964BitComCmdHere,,,"%VS201964BitComCmdHereAccel%"
HKCR,Drive\Shell\VS201964BitComCmdHere\command,,,"cmd.exe /k call ""C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat"" && pushd ""%1"""
[VS201964BitComCmdHereUninstall.Reg]
HKLM,%UDHERE%
HKCR,Directory\Shell\VS201964BitComCmdHere
HKCR,Drive\Shell\VS201964BitComCmdHere
HKCR,Directory\Background\Shell\VS201964BitComCmdHere
[Strings]
VS201964BitComCmdHereName="Developer Command Prompt Here for VS2019 Community"
VS201964BitComCmdHereAccel="VS 2019 Community Prompt"
UDHERE="Software\Microsoft\Windows\CurrentVersion\Uninstall\VS201964BitComCmdHere"

To use VS Code with Visual Studio, in the project folder you need to create a .vscode/launch.json file:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "cl.exe - Build and debug active file",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "console": "externalTerminal",
            //"console": "internalConsole",
            "preLaunchTask": "C/C++: cl.exe build active file"
        }
    ]

}

and a .vscode/tasks.json file:

{
"version": "2.0.0",
"tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: cl.exe build active file",
            "command": "cl.exe",
            "args": [
                "/Zi",
                "/EHsc",
                "/nologo",
                "-W4",
                "-D" "_CRT_SECURE_NO_WARNINGS=1",
                "/Fe:",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "${file}",
                "gnuplot_c.lib"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$msCompile"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compiler: cl.exe"
        }
    ]

}

Now open VSCode and open the project folder: File | Open Folder ...

Save the workspace: File | Save Workspace As ...

Close VSCode

Now to use VSCode with Visual Studio, right click on the project folder in Explorer and choose: "VS2019 Community Prompt"

Type the following:

code workspace.code-workspace

You can now compile and debug your application, with full break point and single stepping support.


No comments:

Post a Comment