David C. Rankin
David C. Rankin

Reputation: 84599

VS2017 Open Folders - configure 'Compile C' context in tasks.vs.json?

With VS 2017, you have the option of File->Open->Open Folder to open all sources in the Solution Explorer without needing to create a project. Theoretically, this will allow you to work with any of the small single-source files in a directory to compile, debug, etc.. without needing to set up a project for each one. However, I have yet to find a way to add a working 'Compile' option to the Solution Explorer context-menu that will invoke the compiler to build the individual files.

Microsoft provides several reference pages aimed at explaining the process:

This has been tangentially touched on in the SO question VS2017 'Open Folder'-project fails to generate CMake cache though that is limited to cmake specifically and ended up being a typo in the questioners attempt to follow CMake support in Visual Studio

The problem I am having is when I add a task to tasks.vs.json to compile the C files in a directory, VS responds with an error in the message window that cl cannot be found as an internal or external command. The tasks.vs.json task I am attempting to use is:

{
  "version": "0.2.1",
  "tasks": [
    {
      "taskName": "Echo filename",
      "appliesTo": "*.c",
      "type": "command",
      "command": "${env.COMSPEC}",
      "args": ["cl /nologo /W3 /wd4996 /Ox /Foobj/ /Febin/${fileBasename} /Tc ${file}"]
    },
    {
      ... (normal default entries for launch...)
    }
  ]
}

The inherited environments for msvc_x86 and msvc-x64 for both "Debug" and "Release" are properly created by default in the directory within CppProperties.json.

I have attempted to create an external tool for the VS Command Prompt through Tools->External Tools... Add and launching cmd.exe with /k "C:\Program Files (x86)\Microsoft Visual Studio 17.0\Common7\Tools\VsDevCmd.bat" (which opens the desired developer command line just fine), but does not provide an external command to which to pass the compile string to compile a source file from the Solutions Explorer list.

I simply want to invoke the cl.exe compiler on the file selected in the Solution Explorer list with the following command line:

cl /nologo /W3 /wd4996 /Ox /Foobj/ /Febin/${fileBasename} /Tc ${file}

I may be reading the documentation wrong, but how do you craft a task entry in tasks.vs.json (or elsewhere) that will provide a simple context-menu entry to allow compiling the C-sources opened with File->Open->Open Folder?

Upvotes: 3

Views: 960

Answers (1)

zhaofeng-shu33
zhaofeng-shu33

Reputation: 776

According to Customize build and debug tasks for "Open Folder" development, this works.

{
  "version": "0.2.1",
  "tasks": [
    {
        "taskName": "cl-task",
        "appliesTo": "*.c",
        "type": "command",
        "command": "cl",
        "args": [ "${file}" ],
        "envVars": {
            "VSCMD_START_DIR": "\"${workspaceRoot}\""
        }
    }
  ]
}

CppProperties.json provides information for browsing, that is for IntelliSense behavior.

To specify environment variables, use envVars instead. You can create a file VSWorkspaceSettings.json in the root folder and add

{
    "envVars": { "PATH": "%PATH%;C:\\Users\\zhaofeng-shu33\\Anaconda3\\Scripts" }
}

Then the following task will work. It simply outputs help message for program conda, which is an executable located within folder Scripts and is not added to the global environment variable.

{
    "taskName": "conda-task",
    "appliesTo": "*.c",
    "type": "command",
    "command": "${env.COMSPEC}",
    "args": [ "conda --help" ]
}

Upvotes: 2

Related Questions