How to run code in visual studio code

How to run code in visual studio code

How to run C++ Code in Visual Studio Code

How to run code in visual studio code. Смотреть фото How to run code in visual studio code. Смотреть картинку How to run code in visual studio code. Картинка про How to run code in visual studio code. Фото How to run code in visual studio code

I am writing this article because I had faced several issues while running the C++ code in VS Code. As usual, everyone was using the CodeBlocks IDE and DevC++ and many more. But I was already used to Visual Studio Code for all my programming stuff. In this article, I’ll show you how to set up your compiler in VsCode. I will be using a Windows OS throughout this article.

So let’s Start..

Prerequisites:

Download and install a C++ compiler:

Head to www.mingw.org and click the “Download/Installer” link to download the MinGW setup file, or click here for Windows, here for Linux, and here for Mac.

After downloading, install MinGW and wait for the “ MinGW Installation Manager” to show up.

How to run code in visual studio code. Смотреть фото How to run code in visual studio code. Смотреть картинку How to run code in visual studio code. Картинка про How to run code in visual studio code. Фото How to run code in visual studio code

When the “ MinGW Installation Manager” shows up, click on mingw32-gcc-g++ then select “ Mark for Installation”.

How to run code in visual studio code. Смотреть фото How to run code in visual studio code. Смотреть картинку How to run code in visual studio code. Картинка про How to run code in visual studio code. Фото How to run code in visual studio code

In the menu at the top left corner, click on “ Installation > Apply Changes”

How to run code in visual studio code. Смотреть фото How to run code in visual studio code. Смотреть картинку How to run code in visual studio code. Картинка про How to run code in visual studio code. Фото How to run code in visual studio code

Wait and allow to install completely. Ensure you have a stable internet connection during this process.

Edit your PATH environment variable to include the directory where the C++ compiler is located :

Install Code Runner extension in VS Code :

Now we are done with our compiler set up, let’s install Code Runner

Code Runner allows you to Run code snippet or code file for multiple languages: C, C++, Java, JavaScript, PHP, Python, Perl, Perl 6, Ruby, Go, Lua, Groovy, PowerShell, BAT/CMD, BASH/SH, F# Script, F# (.NET Core), C# Script, C# (.NET Core), VBScript, TypeScript, CoffeeScript, Scala, Swift, Julia, Crystal, OCaml Script, R, AppleScript etc.

Search in VsCode marketplace tab:

How to run code in visual studio code. Смотреть фото How to run code in visual studio code. Смотреть картинку How to run code in visual studio code. Картинка про How to run code in visual studio code. Фото How to run code in visual studio code

How to run code in visual studio code. Смотреть фото How to run code in visual studio code. Смотреть картинку How to run code in visual studio code. Картинка про How to run code in visual studio code. Фото How to run code in visual studio code

After installing restart VsCode. Open your C++ file in VsCode.

Here’s a basic hello world program below:

How to run code in visual studio code. Смотреть фото How to run code in visual studio code. Смотреть картинку How to run code in visual studio code. Картинка про How to run code in visual studio code. Фото How to run code in visual studio code

Run your code using Code Runner:

1.Use the shortcut Ctrl+Alt+N

2. Press F1 and then select/type Run Code

3. Right-click the Text Editor and then click Run Code in the editor context menu.

The code will run and the output will be shown in the Output Window. Open the output window with `Ctrl+ shortcut.

How to run code in visual studio code. Смотреть фото How to run code in visual studio code. Смотреть картинку How to run code in visual studio code. Картинка про How to run code in visual studio code. Фото How to run code in visual studio code

DO NOT SKIP THIS STEP :

By default, VsCode’s output terminal is read-only. It means that You can’t use terminal to take input by default. To fix this, you need to manually enable read-write. Steps as follow:

How to run code in visual studio code. Смотреть фото How to run code in visual studio code. Смотреть картинку How to run code in visual studio code. Картинка про How to run code in visual studio code. Фото How to run code in visual studio code

How to: Run a C# program in Visual Studio

How to run a program depends on what you start from, the type of program, and whether you want to run under the debugger. In the simplest case, to build and run an open project in Visual Studio:

Start from a project

You can run a C# project or .csproj file if it’s a runnable program. If the project contains a C# file with a Main method, and its output is an executable or .exe file, it will probably run if it builds successfully.

If your program code is already in a Visual Studio project, open the project. To do so, you can double-click or tap on the .csproj file in Windows File Explorer, or choose Open a project in Visual Studio, browse to find the .csproj file, and select the file.

After the project loads in Visual Studio, if your Visual Studio solution has more than one project, make sure to set the project with the Main method as the startup project. To set the startup project, right-click on the project name or node in Solution Explorer and choose Set as Startup Project from the context menu.

How to run code in visual studio code. Смотреть фото How to run code in visual studio code. Смотреть картинку How to run code in visual studio code. Картинка про How to run code in visual studio code. Фото How to run code in visual studio code

How to run code in visual studio code. Смотреть фото How to run code in visual studio code. Смотреть картинку How to run code in visual studio code. Картинка про How to run code in visual studio code. Фото How to run code in visual studio code

To run the program, press Ctrl+F5, select Debug > Start without debugging from the top menu, or select the green Start button.

Visual Studio tries to build and run your project. At the bottom of the Visual Studio screen, the build output appears in the Output window, and any build errors appear in the Error List window.

If the build succeeds, the app runs as appropriate for the type of project. Console apps run in a terminal window, Windows desktop apps start in a new desktop window, and web apps run in a browser hosted by IIS Express.

Start from code

If you start from a code listing, code file, or small number of files, first make sure the code is a runnable program from a trusted source. Any app with a Main method is probably a runnable program. You can use the Console Application template to create a project to work with the app in Visual Studio.

Code listing for a single file

Several code listings or files on disk

Several files in a folder

If you have a folder with many files, first check for a project or solution file. Programs that Visual Studio creates have project and solution files. In Windows File Explorer, look for files with the .csproj or .sln extension. Double-click the .csproj file to open it in Visual Studio. See Start from a Visual Studio solution or project.

If the code is from another development environment, there’s no project file. Open the folder by choosing Open > Folder in Visual Studio. See Develop code without projects or solutions.

Start from a GitHub or Azure DevOps repo

If the code you want to run is in a GitHub or Azure DevOps repo, you can use Visual Studio to open the project directly from the repo. See Open a project from a repo.

Run the program

To start building the program, press the green Start button on the Visual Studio toolbar, or press F5 or Ctrl+F5. Using the Start button or F5 runs the program under the debugger.

Visual Studio attempts to build and run the code in your project. If a build doesn’t succeed, see the following sections for some ideas on how to get the project to build successfully.

Troubleshooting

Add references

To build properly, the code must be correct and have the right references to libraries or other dependencies. Red squiggly underlines in code or entries in the Error List show errors even before you compile and run the program. If the errors relate to unresolved names, you probably need to add a reference or a using directive, or both. If the code references any missing assemblies or NuGet packages, you need to add those references to the project.

Visual Studio tries to help you identify missing references. When a name is unresolved, a light bulb icon appears in the editor. Select the light bulb to see suggestions on how to fix the issue. Fixes might be to:

Add a using directive

Here’s an example of a missing using directive. You can add using System; to the start of the code file to resolve the unresolved name Console :

How to run code in visual studio code. Смотреть фото How to run code in visual studio code. Смотреть картинку How to run code in visual studio code. Картинка про How to run code in visual studio code. Фото How to run code in visual studio code

How to run code in visual studio code. Смотреть фото How to run code in visual studio code. Смотреть картинку How to run code in visual studio code. Картинка про How to run code in visual studio code. Фото How to run code in visual studio code

Add an assembly reference

.NET references can be assemblies or NuGet packages. In source code, the publisher or author usually explains what assemblies the code requires and what packages it depends on. To add a reference to a project manually, right-click the References node in Solution Explorer and choose Add Reference. In the Reference Manager, locate and add the required assembly.

How to run code in visual studio code. Смотреть фото How to run code in visual studio code. Смотреть картинку How to run code in visual studio code. Картинка про How to run code in visual studio code. Фото How to run code in visual studio code

How to run code in visual studio code. Смотреть фото How to run code in visual studio code. Смотреть картинку How to run code in visual studio code. Картинка про How to run code in visual studio code. Фото How to run code in visual studio code

You can find assemblies and add references by following the instructions in Add or remove references by using the Reference Manager.

Add a NuGet package

If Visual Studio detects a missing NuGet package, a light bulb appears and gives you the option to install the package:

How to run code in visual studio code. Смотреть фото How to run code in visual studio code. Смотреть картинку How to run code in visual studio code. Картинка про How to run code in visual studio code. Фото How to run code in visual studio code

How to run code in visual studio code. Смотреть фото How to run code in visual studio code. Смотреть картинку How to run code in visual studio code. Картинка про How to run code in visual studio code. Фото How to run code in visual studio code

If that doesn’t solve the issue or Visual Studio can’t locate the package, try searching for the package online. See Install and use a NuGet package in Visual Studio.

How to Run Code in VS Code

One of the most popular source-code editors, Visual Studio Code, commonly called VS Code, is very beginner-friendly. Its user-friendly interface and advanced features make it a favorite for both beginners and veteran programmers alike.

How to run code in visual studio code. Смотреть фото How to run code in visual studio code. Смотреть картинку How to run code in visual studio code. Картинка про How to run code in visual studio code. Фото How to run code in visual studio code

If you’re new to VS Code and looking for a simplified guide on running code, you’re in the right place. We’ll guide you through setting up required software and running code using familiar languages like C/C++ and Python, as well as how to run command-line arguments.

The Shortcut to Run Code in VS Code

In VS Code, you only need to use a shortcut to run your code. That shortcut is Ctrl + Alt + N. There are a few more ways to run code.

Pressing F1 and then choosing “Run Code” also works. If you want to type it out after pressing F1, you’re free to do that as well.

Users can right-click the text editor and then select “Run Code” in the context menu. With only two clicks, your code will run.

“Run Code” is also an option in the editor title menu and the file explorer’s context menu.

Should you want to stop your code from running, the shortcut is Ctrl + Alt + M. Pressing F1 also lets you choose the “Stop Code Run” option. This option is also available in the editor title menu and Output Channel.

Lastly, you can right-click on the Output Channel to open the context menu and select “Stop Code Run.”

Learning these shortcuts and methods should make running and stopping code a little easier in various situations.

How to Run C Code in VS Code

Besides knowing C code and VS Code, you’ll have to download the C/C++ extension. Without it, you won’t be able to run C code within VS Code.

These are the instructions for running C code in VS Code:

Setting Up the Required Software

Coding in VS Code

Once you complete the one-time software setup, you’ll be able to use VS Code and the C language easily. Just make sure the other required programs are correctly installed before running code, especially the packages.

How to Run Python Code in VS Code

Running Python code in VS Code is relatively easy. You’ll need an extension and Python interpreter. The former is found in the VS Code extensions marketplace, but Python interpreters vary depending on how they’re installed.

Before you install the others, you should already have Python on your PC. Verifying it beforehand is also essential.

With the preliminaries out of the way, let’s get into the coding process.

The interpreter you install depends on your needs, but VS Code works incredibly well with Python regardless of your interpreter choice.

How to Run JS Code in VS Code

JavaScript works even better in VS Code due to the latter already having JavaScript IntelliSense, refactoring, and additional advanced features for the language. Since VS Code works so well with JS Code, there’s little preliminary configuring needed to start working right away.

Here’s how you’ll run JavaScript code in VS Code:

Your directory and files will be named differently, so we only included these as placeholder names.

A more straightforward way to run JavaScript code also exists:

If you only want to test a few lines of code and then save it, this method shines. You can finish within a minute and have a file ready to store for future use.

How to Run Code With Arguments in VS Code

Command-line arguments are for debugging or launching code files, and VS Code is entirely compatible with these types of codes and debugging. In this section, you’ll learn how to run code with arguments.

Before you do so, you have to get “launch.json.” If you don’t have it, you can create it by following these steps:

The following steps are for running code with arguments:

These are the basics of running your code with arguments. In this case, we’re talking about Python since it’s the simplest example.

Coding Made Easy

With this knowledge, running code in VS Code should become as natural as breathing – with enough practice. The various languages you can use with VS Code make it a powerful and straightforward IDE to pick up. As such, many developers highly recommend aspiring programmers learn how to use it.

Do you like using VS Code? What do you think about the interface? Let us know your thoughts in the comments section below.

Prerequisites

Create the app

Start Visual Studio Code.

Select File > Open Folder (File > Open. on macOS) from the main menu.

In the Open Folder dialog, create a HelloWorld folder and select it. Then click Select Folder (Open on macOS).

In the Do you trust the authors of the files in this folder? dialog, select Yes, I trust the authors.

Open the Terminal in Visual Studio Code by selecting View > Terminal from the main menu.

The Terminal opens with the command prompt in the HelloWorld folder.

In the Terminal, enter the following command:

The project template creates a simple application that displays «Hello World» in the console window by calling the Console.WriteLine(String) method in Program.cs.

Replace the contents of Program.cs with the following code:

The first time you edit a .cs file, Visual Studio Code prompts you to add the missing assets to build and debug your app. Select Yes, and Visual Studio Code creates a .vscode folder with launch.json and tasks.json files.

If you don’t get the prompt, or if you accidentally dismiss it without selecting Yes, do the following steps to create launch.json and tasks.json:

In the latest version of C#, a new feature named top-level statements lets you omit the Program class and the Main method. Most existing C# programs don’t use top-level statements, so this tutorial doesn’t use this new feature. But it’s available in C# 10, and whether you use it in your programs is a matter of style preference.

Run the app

Run the following command in the Terminal:

The program displays «Hello World!» and ends.

How to run code in visual studio code. Смотреть фото How to run code in visual studio code. Смотреть картинку How to run code in visual studio code. Картинка про How to run code in visual studio code. Фото How to run code in visual studio code

Enhance the app

Enhance the application to prompt the user for their name and display it along with the date and time.

NewLine is a platform-independent and language-independent way to represent a line break. Alternatives are \n in C# and vbCrLf in Visual Basic.

Save your changes.

In Visual Studio Code, you have to explicitly save changes. Unlike Visual Studio, file changes are not automatically saved when you build and run an app.

Run the program again:

Respond to the prompt by entering a name and pressing the Enter key.

How to run code in visual studio code. Смотреть фото How to run code in visual studio code. Смотреть картинку How to run code in visual studio code. Картинка про How to run code in visual studio code. Фото How to run code in visual studio code

Press any key to exit the program.

Additional resources

Next steps

Prerequisites

Create the app

Start Visual Studio Code.

Select File > Open Folder (File > Open. on macOS) from the main menu.

In the Open Folder dialog, create a HelloWorld folder and click Select Folder (Open on macOS).

Open the Terminal in Visual Studio Code by selecting View > Terminal from the main menu.

The Terminal opens with the command prompt in the HelloWorld folder.

In the Terminal, enter the following command:

The template creates a simple «Hello World» application. It calls the Console.WriteLine(String) method to display » Hello World! » in the console window.

Main is the application entry point, the method that’s called automatically by the runtime when it launches the application. Any command-line arguments supplied when the application is launched are available in the args array.

Run the app

Run the following command in the Terminal:

The program displays «Hello World!» and ends.

How to run code in visual studio code. Смотреть фото How to run code in visual studio code. Смотреть картинку How to run code in visual studio code. Картинка про How to run code in visual studio code. Фото How to run code in visual studio code

Enhance the app

Enhance the application to prompt the user for their name and display it along with the date and time.

Open Program.cs by clicking on it.

The first time you open a C# file in Visual Studio Code, OmniSharp loads in the editor.

How to run code in visual studio code. Смотреть фото How to run code in visual studio code. Смотреть картинку How to run code in visual studio code. Картинка про How to run code in visual studio code. Фото How to run code in visual studio code

Select Yes when Visual Studio Code prompts you to add the missing assets to build and debug your app.

How to run code in visual studio code. Смотреть фото How to run code in visual studio code. Смотреть картинку How to run code in visual studio code. Картинка про How to run code in visual studio code. Фото How to run code in visual studio code

NewLine is a platform-independent and language-independent way to represent a line break. Alternatives are \n in C# and vbCrLf in Visual Basic.

Save your changes.

In Visual Studio Code, you have to explicitly save changes. Unlike Visual Studio, file changes are not automatically saved when you build and run an app.

Run the program again:

Respond to the prompt by entering a name and pressing the Enter key.

How to run code in visual studio code. Смотреть фото How to run code in visual studio code. Смотреть картинку How to run code in visual studio code. Картинка про How to run code in visual studio code. Фото How to run code in visual studio code

Press any key to exit the program.

Additional resources

Next steps

How to build and run C++ code in Visual Studio Code?

I have a tasks.json script that currently compiles the code

This works fine, but when I want to run the file I have to run the exe from command line. Is it possible to do this in the task as well? So if it finishes building succesfully it then runs a different task?

3 Answers 3

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

Switch to Trending sort

If anyone else comes across this when searching like I did, you can now set the property preLaunchTask in your launch.json to your build task’s name property and it will run before your launch.

«name»: «Debug (gdb) Launch», «preLaunchTask»: «Build All»,

Will run the «name»: «Builld All» in your tasks.json before launching your program.

You can read the information on this on the Debugging in Visual Code docs page.

You can configure multiple tasks in Visual Studio Code, one of which will allow you to build your executable, and the other will run your executable.

Optionally, you could also look into Visual Studio Code’s «Run Mode» (see here). If you use «Run Mode», you should be able to configure Visual Studio Code to build your executable, and then launch it.

I’m not extremely familiar with «Run Mode», thus I will detail how to define multiple tasks to achieve a similar result.

Disclaimer: Visual Studio Code does not support tasks that use different shell commands (see here).

That’s right. At its current state, Visual Studio Code doesn’t have «native» support for defining tasks that use different shell commands.

Disclaimer: Visual Studio Code’s task-output pane will not allow you to pass input to your program interactively.

If your program relies on user-input (for example, from stdin), you’re probably better off not using Visual Studio Code to run your executable.

Basically, what we’ll need to do, is define two tasks, one of which will be a build task, the other will be our launch task.

The reason behind us doing this, is because we want each of the tasks we’re about to define, to be passed as arguments to a new shell instance.

The next step, is to define our build and launch tasks. When we do so, we’ll need to make sure we place the command we want to run, as a task argument. For example:

Finally, what we’ll do, is add the «isBuildCommand» property to our build task (and make sure it’s true ), as well as add the «isTestCommand» property to our launch task (and, again, make sure it’s true ).

After all of that, our tasks.json file could look something like this:

Note: If placing each task argument in their own string within the args array doesn’t work, you can also try placing all of the arguments in a single string within the args array. Example:

Note: If you would like to be able to invoke your task(s) via keyboard shortcuts, you have the «workbench.action.tasks.build» and «workbench.action.tasks.test» editor commands at your disposal.

If you need an example of binding keys to those commands, here’s an example of how I have them mapped in my keybindings.json file:

Edit: You probably only need to define a keyboard shortcut for the test task, as the build task probably already has one defined. Check here before you take the time to define a different keyboard shortcut.

Источники информации:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *