Luchnik
Luchnik

Reputation: 1087

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

Upvotes: 8

Views: 12593

Answers (4)

convexbytes
convexbytes

Reputation: 182

Sublime Text 3 makes harder to modify the default settings; to change them, you can use the package "PackageResourceViewer". Install this package (PackageControl is recommended), then hit Ctrl+shift+P and execute the commands:

PackageResourceViewer: OpenResource -> C++ -> C++ Single File.sublime-build

Add the option "-std=C++11" after every "g++ " command you find. Save and close the file. You should be able to compile now.

(The file should look like the one below after the changes)

{
    "shell_cmd": "g++ -std=c++11 \"${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",
            "shell_cmd": "g++ -std=c++11 \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\""
        }
    ]
}

Upvotes: 0

dimsum88
dimsum88

Reputation: 57

Alec's solution worked well for me, but the run variant called "Run" will not build with C++11, so I added one more run option, called "Run with C++11":

{
    "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": "Run with C++11",
            "cmd": ["bash", "-c", "g++ '${file}' -std=c++11 -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}'"]
        }
    ]
}

Upvotes: 0

Saras Arya
Saras Arya

Reputation: 3112

Sometimes when you do this, you might encounter an error, say a syntax error, or some other kind of error to avoid all that use the standard C++ configuration, just add the keyword -std=c++11 after g++ in shell_cmd Example

{
    "shell_cmd": "g++ -std=c++11 \"${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++ '${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}'"]
        }
    ]
}

That does it. C++98 is the same config without -std=c+11 options.

For more info on how to deal with these kinds of file please head over to the sublime documentation about build systems

Upvotes: 0

Alec
Alec

Reputation: 32319

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.

Upvotes: 7

Related Questions