C11 and C++11 problems in Sublime Text 3

12,001

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.
Share:
12,001
Luchnik
Author by

Luchnik

Updated on June 28, 2022

Comments

  • Luchnik
    Luchnik almost 2 years

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

  • shiin
    shiin over 9 years
    You do not have to modify the '.sublime-package' file. Instead you can place the new 'C++.sublime-build' file in your user package folder in a subfolder also called 'C++'. In my case the file is in this position '....\SublimeText\Data\Packages\User\C++\C++.sublime-build'.
  • SwiftsNamesake
    SwiftsNamesake over 9 years
    I'm wondering if there's a typo in the last item of the first command line array ("${file}"").
  • Alec
    Alec over 9 years
    @SwiftsNamesake Yeah, looks like a misplaced quotation mark. Fixed.
  • sookie
    sookie almost 8 years
    @Alec : Last line missing a double quotation mark. Once fixed it works perfectly!
  • Alec
    Alec almost 8 years
    @sookie Thanks! Fixed.
  • h3t1
    h3t1 almost 4 years
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference.