How can I change the default build output directory in Visual Studio Code
Asked Answered
D

3

5

I'm new to Visual Studio Code. I want to define the output directory in my csproj. I don't know where to change in Visual Studio Code the destination folder to locate the dll files generated.

Detergent answered 26/10, 2017 at 16:43 Comment(2)
Is it not Right Click a project >> Properties>>Build>>Output>>Output Path like the normal visual studios?Frecklefaced
No...it does not work this way in Visual Studio CodeDetergent
S
4

VSCode uses dotnet CLI and particularly for building the dotnet build command. Among others, it has the following option

-o|--output <OUTPUT_DIRECTORY>
Directory in which to place the built binaries.

Assuming your building task is defined in .vscode/tasks.json file:

{
    "version": "2.0.0",
    "tasks": [
        {
            "taskName": "build",
            "command": "dotnet build",
            ...
        }
    ]
}

You may add -o arg with the desired path. For example, change to:

...
"command": "dotnet build -o ${workspaceRoot}/bin/another_Debug/",
...

where ${workspaceFolder} is one of VSCode predefined variables:

${workspaceFolder} the path of the workspace folder that contains the tasks.json file

Stepup answered 26/10, 2017 at 18:45 Comment(1)
This command shows error like this MSBuild version 17.6.1+8ffc3fe3d for .NET MSBUILD : error MSB1001: Unknown switch. Full command line: '/usr/share/dotnet/sdk/7.0.302/MSBuild.dll -maxcpucount -verbosity:m -restore -consoleloggerparameters:Summary -o /home/user/TestBuild/Sp ..Trial1/Trial1.csproj -distributedlogger:Microsoft.DotNet.Tools.MSBuild.MSBuildLogger,/usr/share/dotnet/sdk/7.0.302/dotnet.dll*Microsoft.DotNet.Tools.MSBuild.MSBuildForwardingLogger,/usr/share/dotnet/sdk/7.0.302/dotnet.dll' Switches appended by response files: Switch: -o /home/user/TestBuild/XXXExpectant
C
3

If you want a consistent behavior from VSCode, VS and the dotnet CLI, I suggest editing the csproj file to set the output path by adding this:

<PropertyGroup>
  <OutputPath>..\custom-output-dir\</OutputPath>
  <!-- Remove this if you multi-target and need the created net*, netcoreapp* subdirs -->
  <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
</PropertyGroup>

The second property prevents the SDK from adding a subdirectory for the target framework, which is needed for multi-targeting builds, but not so if you only target a single framework (= when you only have a TargetFramework property).

You can also use other properties when setting the path, for instance:

<OutputPath>..\custom-output-dir\$(Configuration)\$(MSBuildProjectName)\</OutputPath>

Assuming your project is MyProj.csproj, the output path would then be ..\cusotm-output-dir\Debug\MyProj\

Clientele answered 27/10, 2017 at 6:29 Comment(0)
C
1

Assuming this is your task.json in VScode:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger.",
        }
    ],
    "version": "2.0.0"
}

You can simply change the path of your binaries by changing this line: "${fileDirname}/${fileBasenameNoExtension}" to "${fileDirname}/bin/${fileBasenameNoExtension}". Now all of your bianries will go to the folder bin.

Cry answered 25/2, 2023 at 18:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.