问题
I know that we can compile program as C++ using g++ compiler. But g++ compiler defaults to 98 version. To run it as C++ 14, we need to add -std=c++14
in terminal.
Sublime Text is considered a valuable editor for competitive programming due to its light weight and features. In these competitions time is important and thus the time is wasted in copying in text file and then running from terminal. C++ 14 features a rich library and other important features in comparison to 98. Thus one would desire to be able to compile the code on sublime text and as C++ 14.
But how can I make sure that when I compile the code within Sublime Text 3, it compiles as default to C++14?
After some fiddling around I came up the following solution -
- Go to packages and extract C++.sublime-package
- Open C++ Single File.sublime-build
Change
"shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\""
to
"shell_cmd": "g++ -std=c++14 \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\""
Added
-std=c++14
in both build and run- Save it and overwrite package originally in Sublime Text 3
Is this the right way to do so? It is named as single file - Is this something to worry about if I want to use multiple files ?
回答1:
It's better to create a new build system, here's how:
Click on Tools -> Build System -> New Build System
.
This is what I use (notice the flag -std=c++14
)
{
"cmd":["bash", "-c", "g++ -std=c++14 -Wall '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"cmd":["bash", "-c", "g++ -std=c++14 '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]
}
]
}
(I believe you can just use the line you already have and save it to a new build system file.)
Now save the new build, select it in Tools -> Build System
.
And now you can build and run with CTRL + B
回答2:
The best way to edit default files is to use PackageResourceViewer.
Once installed, run the PackageResourceViewer: Open Resource
command from the command palette
. You will be shown a list of packages which you can navigate into to select the file you wish to edit.
The selected file will be opened in a new document, and any changes will be saved to a new file located at Packages/PackageThatYouEdited/FileThatYouEdited.ext
The benefit of this method is that the edited file in Packages
will now override the original file that resides within the sublime-package
file, but the original file will remain intact. Should you choose to delete the edited file, the original file in the sublime-package
will once again be active.
回答3:
Usually, it's bad to edit default files. Just create a new build system Pallet -> Build: New Build System add your changes there and save it as you want to. Then, when you want to use it just select Build With: (new build system name) If you also want the Build - Run option, use this code instead:
{
"cmd": ["g++", "-std=c++14", "${file}", "-o", "${file_path}/${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"cmd":["bash", "-c", "g++ -std=c++1y '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]
}
]
}
来源:https://stackoverflow.com/questions/37118573/compiling-program-as-c-14-in-sublime-text-3-as-default