Compiling C# projects with VSCode on Ubuntu

2019-03-11 22:32发布

问题:

I set up VSCode on Ubuntu 14.04 according to the various tutorials available in the documentation - I tried as many as I could make sense of. The editor runs without issue and (after working through Mono version discrepancies) provides a superior coding experience as compared to most of the alternatives in my opinion.

My issue comes when trying to compile my C# project. This is functionality that I would have expected when completing the Getting Started guide. After hitting ctrl + shift + B I'm initially prompted to create a tasks.json file which looks to provide project specific configuration of shortkey actions. From comments in the initial tasks.json generated, it appears to be targeting Windows and refers to a tsc.exe program which is a TypeScript compiler.

I've spent a little time building projects with MonoDevelop on the same laptop but never had to setup the compilation step. Am I wrong in assuming this should be functionality available out of the box, or have I missed a step for properly handling C# projects?

回答1:

I must have been impatient when looking through the default tasks.json file last night. There is a section that refers to msbuild (towards the bottom):

// Uncomment the section below to use msbuild and generate problems 
// for csc, cpp, tsc and vb. The configuration assumes that msbuild 
// is available on the path and a solution file exists in the  
// workspace folder root. 
/* 
{   
    "version": "0.1.0",
    "command": "msbuild",
    "args": [
        // Ask msbuild to generate full paths for file names.       
        "/property:GenerateFullPaths=true"  
    ],
    "taskSelector": "/t:",
    "showOutput": "silent",
    "tasks": [
        {
            "taskName": "build",
            // Show the output window only if unrecognized errors occur.            
            "showOutput": "silent",
            // Use the standard MS compiler pattern to detect errors, warnings          
            // and infos in the output.
            "problemMatcher": "$msCompile"
        }   
     ] 
 }
*/

Just comment out the rest of the file, uncomment the above JSON text, and change "command" from "msbuild" to "xbuild" (the Mono equivalent). Now hitting ctrl+shift+B successfully compiles the project.

Hopefully, this manual tinkering with configuration files will be less necessary or tedious once it comes out of preview.

EDIT

Marking this as answer for now. Will update or accept a better answer should things change during the evolution of the product.