How to run a command on powershell in vscode? - c++

I am making a c++ program in vscode. And i want to compile the c++ program using a specific command (for example g++ file_name.cpp -o file_name.exe). But cant understand how to do it using tasks in vscode?All i want is to be able to run the above compilation command on the powershell in vscode. How can i do it??

I recently found out how to do it.
You can download the coderunner extension in vscode.
Then go to the settings of this extension.
Then go to the executor_map.json folder.
In this folder you will see json pairs like key value pairs.
The 'key' is the language name and 'value' is the string which is directly pasted in the powershell of vscode.
So now you can easily modify the string to build your projects the way you want.
For Example in the case of C++ , I can give a g++ command with a lot of different flags like -Wall -Wshadow etc.
Code runner also auto detects the language you are working in. All you need to do is just press run, and your project will be build using your custom command.
If there is some other easier way you can share that too.

Just click "Terminal" then "New Terminal" in the toolbar above, you'll get a powershell terminal by default.

Related

how do I run my code on cmd instead of vscode's internal terminal

everyone. I'm kind of new in this field. So bear with it. I'll try to be as specific as I can:
let's say when I run a code(c++ file) in VScode it runs that code on VScode's internal terminal..like this => VScode
but I want that code to run on my Window's CMD like "CodeBlocks" software. Like this => CodeBlocks
but I don't know how to do it in VScode. I mean, when I click on 'run' button it should execute that code on CMD. I tried many ways but it's not working. Help please and thanks in advance.
VSCode has a built-in terminal. That is why in the first case(first image in your question) you see the output as it is. If you don't want to use the built in terminal provided by VSCode then i suggest you open a standalone/separate terminal. And then cd into the project you want to build/compile and then compile the program from there.
Basically, open a terminal externally then go(cd) to your workspace folder and finally compile and run in the external terminal.

How to compile and run C++ code in VS Code easily?

So I just got into using VS Code. I am currently working with C++ and I am using Mingw as my compiler. So far I have been using the terminal at the bottom of VS Code to compile and run like this:
g++ program.cpp then doing ./program.exe
The process of using the terminal at the bottom is time consuming especially when I need to compile and run code frequently. This is also annoying when creating classes when you have to compile multiple files into .o extensions etc.
Can anyone help with this process? Am I doing something wrong, should there be an easier way? Maybe a makefile?
Thanks!
If you want to compile and run C++ code in visual studio code(Vs-code) in windows. This include following steps.
Download Visual studio code.
Go on Add extension Type C++ and install "C/C++" by Microsoft.
Go to Visual Code studio docs for "C++" OR https://code.visualstudio.com/docs/languages/cpp
Install MinGW-x64 vis MSYS2 website and run this on shell "pacman -S --needed base-devel mingw-w64-x86_64-toolchain"
Then go to windows setting and look for Edit environment variables for your account. Then in advance settings >> Environment Variable.
In "system variable" choose path and edit it and add a new path.
You can find a new path in your directory where you have installed the MinGW-x64. you might find it in C:\msys64\mingw64\bin. or where ever you have installed it.
When you have added the new path then go to any shell/cmd and Type g++ --version
if you get the version then you have succeded.
If you find something like command not recognized then please check where you have done wrong or skipped any step.
Otherwise startover.
thanks--

Setup C++ on Visual Studio Code [duplicate]

I am programming in C in Visual Studio Code, but I can't compile, as VSC only offers three compilers built in - Node.js, C# Mono, and Extension development. After a little bit of digging I came across the Visual Studio Marketplace. This seemed like the right sort of thing, but only four uncommon languages were there.
I can only assume that C debugging support is built in, I just can't find it or I am going the wrong way about doing it. I attempted to create a new launch.json (the manifest that seems to hold the compiling/debugging settings for each file) and manually entering the GCC binaries that I have, but that didn't end up working. I'm currently stuck manually compiling the C source file I am working on through command prompt.
Would really help if someone could point me in the right direction on what to do.
tl;dr - Help from anyone debugging C in Visual Studio Code
Windows 8, if that matters
Cheers!
Caution
A friendly reminder: The following tutorial is for Linux user instead of Windows
Tutorial
If you want to debug your c++ code with GDB
You can read this ( Debugging your code ) article from Visual Studio Code official website.
Step 1: Compilation
You need to set up task.json for compilation of your cpp file
or simply type in the following command in the command window
g++ -g file.cpp -o file.exe
to generate a debuggable .exe file
Step 2: Set up the launch.json file
To enable debugging, you will need to generate a launch.json file
follow the launch.json example or google others
Step 3: Press (Ctrl+F5) to start compiling
this launch.json file will launch the configuration when you press the shortcut (Ctrl+F5)
Enjoy it!
ps. For those who want to set up tasks.json, you can read this from vscode official (-> TypeScript Hello World)
Press Ctrl + Shift + P to pull up the Command Pallette, and Type ext install cpptools. It will install everything you need to debug C and C++.
Debugging in VS code is very complete, but if you just need to compile and run:
https://code.visualstudio.com/docs/languages/cpp
Look in the debugging section, and it will explain everything.
There is a much easier way to compile and run C code using GCC, no configuration needed:
Install the Code Runner Extension
Open your C code file in Text Editor, then use shortcut Ctrl+Alt+N, or press F1 and then select/type Run Code, or right click the Text Editor and then click Run Code in context menu, the code will be compiled and run, and the output will be shown in the Output Window.
Moreover you could update the config in settings.json using different C compilers as you want, the default config for C is as below:
"code-runner.executorMap": {
"c": "gcc $fullFileName && ./a.out"
}
For Windows:
Install MinGW or Dev C++
Open Environment Variables
In System Variable select Path -> Edit -> New
Copy this C:\Program Files (x86)\Dev-Cpp\MinGW64\bin to the New window.
(If you have MinGW installed copy its /bin path).
To check if you have added it successfully: Open CMD -> Type "gcc" and it should return:
gcc: fatal error: no input files compilation terminated.
Install C/C++ for Visual Studio Code && C/C++ Compile Run || Code Runner
If you installed only C/C++ Compile Run extension you can compile your program using F6/F7
If you installed the second extension you can compile your program using the button in the top bar.
Screenshot: Hello World compiled in VS Code
Just wanted to add that if you want to debug stuff, you should compile with debug information before you debug, otherwise the debugger won't work. So, in g++ you need to do g++ -g source.cpp. The -g flag means that the compiler will insert debugging information into your executable, so that you can run gdb on it.
You need to install C compiler, C/C++ extension, configure launch.json and tasks.json to be able to debug C code.
This article would guide you how to do it: https://medium.com/#jerrygoyal/run-debug-intellisense-c-c-in-vscode-within-5-minutes-3ed956e059d6
EDIT: As of ~March 2016, Microsoft offers a C/C++ extension for Visual Studio Code and therefor the answer I originally gave is no longer valid.
Visual Studio Code doesn't support C/C++ very well. As such it doesn't >naturally support gcc or gdb within the Visual Studio Code application.
The most it will do is syntax highlighting, the advanced features like >intellisense aren't supported with C. You can still compile and debug code >that you wrote in VSC, but you'll need to do that outside the program itself.

Command line parameters in netbeans

I want to run my program as if I was running it using a terminal. Because I want to do different things depending on what attributes the user of the program provides while running it. Is there any way to do that in netbeans?
See this tutorial which explains how to specify command line arguments for a C++ project in Netbeans 7.2.
It works very similarly to Java, in that you right-click the project, select Properties, then the Run page, then set the command line options you want to feed your application when running it in Netbeans.

Can't run programs on my mac

I've been programming on windows for about three months now and when my computer finally died I bought a mac. I'm really happy with it except I can't figure out how to run my c++ programs.
On windows it would open up in command prompt so I figured it would do the same thing except with terminal.
After I write my programs (in Xcode) I can compile them and it'll tell me if there are syntax errors but the run and run related buttons are all Grey and unusable.
It's incredibly annoying that I can't see how the program that I put 4 hours into runs and even more annoying that I don't know if I've made a runtime error. Someone please help.
Thank you very much, James
Assuming that you're trying to write a program that you interact with on the command line, you want to create a "Command Line Tool" project, found under "Application" in the Mac OS X section of the New Project dialog. Choose "C++ stdc++" from the "Type" dropdown, as well. This will give you a main.cpp that should look familiar to you, with a "Hello, World!" sample.
Note that when you Run this program, it might appear to do nothing - You need to open the Console (from the Run menu, or shift+command+R) to see your output.
Xcode deals in projects. If you're just opening the source file, there is no project. Create a project with the files in it and Xcode should let you run it.
Alternately, you can just use G++ on the command line to compile your files and run the resulting executable there.
Try compiling from the terminal as stated above. The a.out file should be created, and should run as long as Xcode has been installed. It should work fine from the terminal, and point out any errors in the compiler's output.
To runs the a.out file, ensure you are in the files directory in terminal and type ./a.out