{"componentChunkName":"component---src-templates-post-template-js","path":"/csharp-debug-onvscode-en","result":{"data":{"markdownRemark":{"id":"2c5f9744-917e-59bc-ae70-288e4aacb2ee","html":"<blockquote>\n<p>This page has been machine-translated from the <a href=\"/csharp-debug-onvscode\">original page</a>.</p>\n</blockquote>\n<p>This article is a memo for quickly preparing an environment where you can run C# programs on WSL.</p>\n<p>We will build an ELF-format console application with .NET.\nWe will also configure debugging for a C# program through VSCode connected to WSL.</p>\n<h2 id=\"install-net-50-on-wsl-ubuntu\" style=\"position:relative;\"><a href=\"#install-net-50-on-wsl-ubuntu\" aria-label=\"install net 50 on wsl ubuntu permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Install .NET 5.0 on WSL Ubuntu</h2>\n<p>First, to create programs in C#, install the cross-platform .NET 5.0 on Ubuntu running under WSL.</p>\n<p><a href=\"https://dotnet.microsoft.com/download#windowscmd\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Download .NET (Linux, macOS, and Windows)</a></p>\n<p>For installation, the same commands as the Debian 10 instructions in the following document are sufficient.</p>\n<p><a href=\"https://docs.microsoft.com/en-us/dotnet/core/install/linux-debian\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Install .NET on Debian - .NET | Microsoft Docs</a></p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">wget</span> https://packages.microsoft.com/config/debian/10/packages-microsoft-prod.deb -O packages-microsoft-prod.deb\n<span class=\"token function\">sudo</span> dpkg -i packages-microsoft-prod.deb\n\n<span class=\"token comment\"># Install the SDK</span>\n<span class=\"token function\">sudo</span> <span class=\"token function\">apt-get</span> update<span class=\"token punctuation\">;</span> <span class=\"token punctuation\">\\</span>\n  <span class=\"token function\">sudo</span> <span class=\"token function\">apt-get</span> <span class=\"token function\">install</span> -y apt-transport-https <span class=\"token operator\">&amp;&amp;</span> <span class=\"token punctuation\">\\</span>\n  <span class=\"token function\">sudo</span> <span class=\"token function\">apt-get</span> update <span class=\"token operator\">&amp;&amp;</span> <span class=\"token punctuation\">\\</span>\n  <span class=\"token function\">sudo</span> <span class=\"token function\">apt-get</span> <span class=\"token function\">install</span> -y dotnet-sdk-5.0</code></pre></div>\n<h2 id=\"install-the-extension-in-vscode\" style=\"position:relative;\"><a href=\"#install-the-extension-in-vscode\" aria-label=\"install the extension in vscode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Install the extension in VSCode</h2>\n<p>Next, install the extension needed to debug C# in VSCode.</p>\n<p>If you search for C#, just install the extension that appears at the top.\nMake sure VSCode is connected to WSL when you install it.</p>\n<p><img src=\"assets/2021_06_02_cs_on_wsl.assets/image-20210602195127867.png\" alt=\"image-20210602195127867\"></p>\n<h2 id=\"create-a-net-project\" style=\"position:relative;\"><a href=\"#create-a-net-project\" aria-label=\"create a net project permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Create a .NET project</h2>\n<p>Next, create a .NET project on WSL.\nCreate it as a console application.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">dotnet new console -o <span class=\"token operator\">&lt;</span>project_name<span class=\"token operator\">></span></code></pre></div>\n<p>Once the project has been created, there should be a directory in the current directory with the same name as the one you specified for <code class=\"language-text\">project_name</code>.</p>\n<h2 id=\"verify-that-it-can-be-run-from-the-command-line\" style=\"position:relative;\"><a href=\"#verify-that-it-can-be-run-from-the-command-line\" aria-label=\"verify that it can be run from the command line permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Verify that it can be run from the command line</h2>\n<p>If you move into the project directory you created and run the <code class=\"language-text\">run</code> command, the code in <code class=\"language-text\">Program.cs</code> will be executed.\nWhen creating and debugging a C# program, this is the file you will edit first.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token builtin class-name\">cd</span> <span class=\"token operator\">&lt;</span>project_name<span class=\"token operator\">></span>\ndotnet run</code></pre></div>\n<h2 id=\"debug-in-vscode\" style=\"position:relative;\"><a href=\"#debug-in-vscode\" aria-label=\"debug in vscode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Debug in VSCode</h2>\n<p>Since we have confirmed that it runs from the console, next let’s try running it under the debugger from VSCode.\nThe following two files are used at this point.</p>\n<ul>\n<li>launch.json</li>\n<li>tasks.json</li>\n</ul>\n<p>Create both of them directly under the <code class=\"language-text\">.vscode</code> directory.</p>\n<p>When you debug a .NET application on WSL from VSCode, the files above that are usually generated automatically do not work properly.\nBecause of that, you need to manually add the information required for C# debugging to these files.</p>\n<p>The contents that need to be written in each are as follows.\nIn both cases, note that <code class=\"language-text\">project_name</code> must be replaced with the name of the project you created.</p>\n<ul>\n<li>launch.json</li>\n</ul>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token punctuation\">{</span>\n    // Use IntelliSense to learn about possible attributes.\n    // Hover to view descriptions of existing attributes.\n    // For <span class=\"token function\">more</span> information, visit: https://go.microsoft.com/fwlink/?linkid<span class=\"token operator\">=</span><span class=\"token number\">830387</span>\n    <span class=\"token string\">\"version\"</span><span class=\"token builtin class-name\">:</span> <span class=\"token string\">\"0.2.0\"</span>,\n    <span class=\"token string\">\"configurations\"</span><span class=\"token builtin class-name\">:</span> <span class=\"token punctuation\">[</span>\n        <span class=\"token punctuation\">{</span>\n            <span class=\"token string\">\"name\"</span><span class=\"token builtin class-name\">:</span> <span class=\"token string\">\".NET Core Launch (console)\"</span>,\n            <span class=\"token string\">\"type\"</span><span class=\"token builtin class-name\">:</span> <span class=\"token string\">\"coreclr\"</span>,\n            <span class=\"token string\">\"request\"</span><span class=\"token builtin class-name\">:</span> <span class=\"token string\">\"launch\"</span>,\n            <span class=\"token string\">\"preLaunchTask\"</span><span class=\"token builtin class-name\">:</span> <span class=\"token string\">\"build\"</span>,\n            <span class=\"token string\">\"program\"</span><span class=\"token builtin class-name\">:</span> <span class=\"token string\">\"<span class=\"token variable\">${workspaceFolder}</span>/procon_cs/bin/Debug/net5.0/&lt;project_name>.dll\"</span>,\n            <span class=\"token string\">\"args\"</span><span class=\"token builtin class-name\">:</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span>,\n            <span class=\"token string\">\"cwd\"</span><span class=\"token builtin class-name\">:</span> <span class=\"token string\">\"<span class=\"token variable\">${workspaceFolder}</span>/&lt;project_name>\"</span>,\n            <span class=\"token string\">\"console\"</span><span class=\"token builtin class-name\">:</span> <span class=\"token string\">\"internalConsole\"</span>,\n            <span class=\"token string\">\"stopAtEntry\"</span><span class=\"token builtin class-name\">:</span> <span class=\"token boolean\">false</span>\n        <span class=\"token punctuation\">}</span>,\n        <span class=\"token punctuation\">{</span>\n            <span class=\"token string\">\"name\"</span><span class=\"token builtin class-name\">:</span> <span class=\"token string\">\".NET Core Attach\"</span>,\n            <span class=\"token string\">\"type\"</span><span class=\"token builtin class-name\">:</span> <span class=\"token string\">\"coreclr\"</span>,\n            <span class=\"token string\">\"request\"</span><span class=\"token builtin class-name\">:</span> <span class=\"token string\">\"attach\"</span>\n        <span class=\"token punctuation\">}</span>\n    <span class=\"token punctuation\">]</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<ul>\n<li>tasks.json</li>\n</ul>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token punctuation\">{</span>\n    // See https://go.microsoft.com/fwlink/?LinkId<span class=\"token operator\">=</span><span class=\"token number\">733558</span>\n    // <span class=\"token keyword\">for</span> the documentation about the tasks.json <span class=\"token function\">format</span>\n    <span class=\"token string\">\"version\"</span><span class=\"token builtin class-name\">:</span> <span class=\"token string\">\"2.0.0\"</span>,\n    <span class=\"token string\">\"tasks\"</span><span class=\"token builtin class-name\">:</span> <span class=\"token punctuation\">[</span>\n        <span class=\"token punctuation\">{</span>\n            <span class=\"token string\">\"label\"</span><span class=\"token builtin class-name\">:</span> <span class=\"token string\">\"build\"</span>,\n            <span class=\"token string\">\"command\"</span><span class=\"token builtin class-name\">:</span> <span class=\"token string\">\"dotnet\"</span>,\n            <span class=\"token string\">\"type\"</span><span class=\"token builtin class-name\">:</span> <span class=\"token string\">\"process\"</span>,\n            <span class=\"token string\">\"args\"</span><span class=\"token builtin class-name\">:</span> <span class=\"token punctuation\">[</span>\n                <span class=\"token string\">\"build\"</span>,\n                <span class=\"token string\">\"<span class=\"token variable\">${workspaceFolder}</span>/&lt;project_name>/&lt;project_name>.csproj\"</span>,\n                <span class=\"token string\">\"/property:GenerateFullPaths=true\"</span>,\n                <span class=\"token string\">\"/consoleloggerparameters:NoSummary\"</span>\n            <span class=\"token punctuation\">]</span>,\n            <span class=\"token string\">\"problemMatcher\"</span><span class=\"token builtin class-name\">:</span> <span class=\"token string\">\"<span class=\"token variable\">$msCompile</span>\"</span>\n        <span class=\"token punctuation\">}</span>\n    <span class=\"token punctuation\">]</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>With that, the setup is complete, so try debugging your C# program from VSCode.</p>\n<p>Open the debug toolbar from the left sidebar and run the .NET program under the debugger.</p>\n<h2 id=\"summary\" style=\"position:relative;\"><a href=\"#summary\" aria-label=\"summary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Summary</h2>\n<p>I wanted to be able to casually write C# on WSL, so I set up this environment on a whim.</p>\n<p>I love C#, but launching Visual Studio every time is a bit of a hassle, so I did not have many chances to write it before. Now that I can run it easily from VSCode, I hope to study C# more going forward.</p>","fields":{"slug":"/csharp-debug-onvscode-en","tagSlugs":["/tag/c-en/","/tag/備忘録/","/tag/vs-code-en/","/tag/english/"]},"frontmatter":{"date":"2021-06-02","description":"This article builds an ELF-format console application with .NET and also sets up debugging for a C# program through VSCode connected to WSL.","tags":["C# (en)","備忘録","VSCode (en)","English"],"title":"Quick notes on running C# on WSL and debugging it in VSCode","socialImage":{"publicURL":"/static/dc4d8b7f8795f3c3d3489d9957d155f2/no-image.png"}}}},"pageContext":{"slug":"/csharp-debug-onvscode-en"}},"staticQueryHashes":["251939775","401334301","825871152"]}