I upgrade my Sublime Text 3 for C/C++ but I have to write code in modern versions like C11 and C++11.
When I try C11 code like this:
#include <stdio.h>
int main( int argc, char ** argv )
{
puts("C99 Version:");
for( int i = 0; argv[i]; i++ ) {
printf("%d: %s\n", i, argv[i]);
}
getchar();
return 0;
}
Sublime gives an errors:
C:\Users\pc\Desktop\CPPproject\c99.c:7:2: error: 'for' loop initial declarations are only allowed in C99 or C11 mode
for( int i = 0; argv[i]; i++ ) {
^
C:\Users\pc\Desktop\CPPproject\c99.c:7:2: note: use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 to compile your code
Can You explain me how to use -std=c99, -std=gnu99, -std=c11 or -std=gnu11 options?
==================================================================================
The same thing with C++11. Here is the code:
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
int main( int argc, char ** argv ) {
stringstream version;
version << "GCC version: "
<< __GNUC__ << "." << __GNUC_MINOR__ << "." << __GNUC_PATCHLEVEL__
<< "\nVersion string: " << __VERSION__;
cout << version.str() << endl;
vector<string> v = { "one", "two", "three" }; // C++11 feature - initializer list
for( string s : v ) { // C++11 feature - range-based for loop
cout << s << endl;
}
return 0;
}
and a list of errors:
C:\Users\pc\Desktop\CPPproject\main.cpp: In function 'int main(int, char**)':
C:\Users\pc\Desktop\CPPproject\main.cpp:17:45: error: in C++98 'v' must be initialized by constructor, not by '{...}'
vector<string> v = { "one", "two", "three" }; // C++11 feature - initializer list
^
C:\Users\pc\Desktop\CPPproject\main.cpp:17:45: error: could not convert '{"one", "two", "three"}' from '<brace-enclosed initializer list>' to 'std::vector<std::basic_string<char> >'
C:\Users\pc\Desktop\CPPproject\main.cpp:19:18: error: range-based 'for' loops are not allowed in C++98 mode
for( string s : v ) { // C++11 feature - range-based for loop
^
Help me solve these problems please!
And another one question: When I run the code - .exe file appears in the same folder with source code and I have to open it. Are there any possibilities to see the output inside Sublime Text when I click "ctrl+b" ???
Thank's !!!
Lots and lots of features that pretty much work out of the box, open source, just install and use. How can I run C program in Sublime Editor Text 3 in Linux? You can build/compile in Sublime Editor, but you can't run it on Sublime Editor. You can run it through terminal.
C++11 supports some, but not all, of the features that are in C99 but not in C90. (Some C99-specific features either are supported differently in C++, or were not deemed suitable.) C11 added a number of features on top of C99; most of those new features were not also added to C++.
Sublime Text provides build systems to allow users to run external programs. Create a new build system for Sublime Text for setting up C++ compilation. Open Sublime Text editor and then go to Tools > Build System > New Build System. Paste the following code in the file and save it.
sublime-build , and it will now be accessible in the build system menu. Select it, hit Ctrl B to build, and then hit Ctrl Shift B to run the resulting program. Or you can use a Build and Run option and call it by hitting Ctrl B , then selecting that option. I Am doing that.
As stated in the comments, it's your build system that you need to alter. I ST3 it's a bit trickier to modify the default packages. There are plugins to do this, but I have yet to use them. Here is a plugin free way.
First, you will need a new .sublime-build
. I think this one should work.
{
"cmd": ["g++", "-std=c++11", "-o", "${file_path}/${file_base_name}", "${file}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"cmd": ["bash", "-c", "g++ '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]
},
{
"name": "Build without C++11",
"cmd": ["g++ '${file}' -o '${file_path}/${file_base_name}'"]
}
]
}
Then, you will need to navigate to where default packages for ST3 are stored. It seems you are using Windows. The location of the .sublime-package
s is probably something like C:\Program Files\Sublime Text 3\Packages.
Once inside this folder, you should see a bunch of .sublime-package
files with the names of all the languages that have builtin support. Choose the C++.sublime-package
and make a copy of it to some other directory. Then, rename it to C++.zip
and extract it. Remove the C++.sublime-build
file and replace it with a file named the same way containing the above code. Re-zip the file and rename it back to C++.sublime-package
and place it back into the folder you got it from.
Now:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With