Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C11 and C++11 problems in Sublime Text 3

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 !!!

like image 218
Luchnik Avatar asked May 02 '14 18:05

Luchnik


People also ask

Can I run C program in Sublime Text 3?

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.

Does C++ support C11?

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++.

Can you code C++ on Sublime Text 3?

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.

How do I edit a build in Sublime Text 3?

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.


1 Answers

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-packages 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:

  • When you press ctrl+b, ST3 will automatically build using the C++11 flag for gcc.
  • If you want to run your program (I think that's what you mean by "see the output inside Sublime Text") press ctrl+shift+b after the program has been compiled.
  • If you want to build using gcc but without the C++11 flags, press ctrl+shift+p and type in "Build: Build without C++11" and select the option that pops up.
like image 194
Alec Avatar answered Sep 24 '22 09:09

Alec