Configure Vscode C + + to run directly as sublime as Ctrl+shift+b compiled

Source: Internet
Author: User

Demand:

Recently like tossing various editors.

With the vscode of the official C + + plugin, with good Launch.json and Tasks.json after the use of debug or feel very good.

But the feeling for ACM Some do not use, but not too sensitive, it is not very convenient to use.

Just want to get the image Subliime comes with:

1. Run directly after compiling

2, the output can be displayed in the following

Process:

Given that vscode default ctrl+shift+b is build, just write the "build" task in Tasks.json, like this

{    //See https://go.microsoft.com/fwlink/?LinkId=733558    //For the documentation about the Tasks.json format"Version": "0.1.0",    "Tasks": [        {            "TaskName": "Build",            "Command": "g++",                        "Isshellcommand":true,            "Args": [                "-O",                "${workspaceroot}/main",                "${workspaceroot}/main.cpp"            ],            "Showoutput": "Always"        }    ]}

We can also write the command to run.

But there are many ways to do it, such as:

1. Add "&&" and "${workspaceroot}/main" after the args

2, the tasks array to write two task dictionaries, respectively, is a different task, a build is a run. Later looked at Microsoft's documentation, seemingly just define a different task, I initially thought it was sequential execution (perhaps I did not see it completely).

Neither of these methods is possible, and finally a way to customize a new Bash command is to write all the commands in the compilation and run.

Workaround:

1, create a new file, named GBR (g++ build and run, oneself blind), wherever you put it, assuming that in the ~ (home directory)

Touch GBR

2, edit this file, whatever editor Gedit,vim,sublime,vscode does not matter, anyway content is not much, gedit enough.

Enter the content.

g++-O $1 $1. CPP $ 1

A parameter that is passed outside

For example, the file you want to compile and run is ~/main.cpp

Just enter the GBR ~/main on the line.

3, set the file as an executable file, I gave 755 permissions directly.

chmod 755 ~/GBR

4, this time you in the ~ directory can already through./GBR ~/main run, but to let Vscode can use this command, we just move the file to the/usr/bin on the line.

sudo MV ~/GBR/USR/BIN/GBR

At this point in the terminal you can use the GBR ~/main to run this command.

5, finally in the Vscode to modify the Tasks.json configuration can be.

{    //See https://go.microsoft.com/fwlink/?LinkId=733558    //For the documentation about the Tasks.json format"Version": "0.1.0",    "Tasks": [        {            "TaskName": "Build",            "Echocommand":true,            "Command": "GBR",            "Isshellcommand":true,            "Args": [                "${workspaceroot}/main"            ],            "Showoutput": "Always"        }    ]}
The Echocommand parameter is for the command to be shown below.

This method is only a small exploration of my own, but I think there must be a more convenient way, perhaps in the future will find.

Subsequent optimizations:

1. Compiling c++14

2, calculate and output the program run time.

To modify the GBR file:

#!/bin/bash# Get Start TimeStart=$(Date+%s%N) Start_ms=${start:0: -}# Buildg+-O $1$1.CPP-std=c++ -# run$1# Get End TimeEnd=$(Date+%s%N) End_ms=${end:0: -} #outputprintf"[Finished in%.1fs]\n"$(Echo "scale=1; ($end _ms-$start _ms)/1000000"| bc

Configure Vscode C + + to run directly as sublime as Ctrl+shift+b compiled

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.