47

I want to enable -std=gnu++11 in Sublime Text 3's C++ Single File build on Ubuntu 12.04.

I have already upgraded the tool chain to the latest g++ and do not want to see the following error on every build:

error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.

I browsed to /home/myuname/.config/sublime-text-3 but cannot find any file to edit.

How can I edit the build settings?

J0e3gan
  • 8,740
  • 10
  • 53
  • 80

2 Answers2

85

edited

My original answer works, but there's a much better way of doing this, by creating your own build system. This use case is exactly why the feature is there.

Go to ToolsBuild SystemNew Build System… (all the way at the bottom) and enter the contents below. Save as C++ 11 Single File.sublime-build, and it will now be accessible in the build system menu. Select it, hit CtrlB to build, and then hit CtrlShiftB to run the resulting program. Or you can use a Build and Run option and call it by hitting CtrlB, then selecting that option.

{
    "cmd": ["g++", "-std=gnu++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": ["${file_path}/${file_base_name}"]
        },
        {
            "name": "Build and Run",
            "cmd": ["g++ -std=gnu++11 ${file} -o ${file_path}/${file_base_name} && ${file_path}/${file_base_name}"],
            "shell": true
        }
    ]
}

If you need to edit it in the future, the file is in the User folder of Packages. The Packages directory is the one opened when selecting Preferences → Browse Packages…:

  • Linux: ~/.config/sublime-text-3/Packages or ~/.config/sublime-text/Packages
  • OS X: ~/Library/Application Support/Sublime Text 3/Packages or ~/Library/Application Support/Sublime Text/Packages
  • Windows Regular Install: C:\Users\YourUserName\AppData\Roaming\Sublime Text 3\Packages or C:\Users\YourUserName\AppData\Roaming\Sublime Text\Packages
  • Windows Portable Install: InstallationFolder\Sublime Text 3\Data\Packages InstallationFolder\Sublime Text\Data\Packages

The exact path depends on version and whether or not you upgraded from Sublime Text 3.

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • 1
    I Am doing that. Why has it not been stated on the sublime website is what I wonder. –  May 21 '14 at 17:31
  • Does not build , the same error I get when I do ctrl+b { "shell_cmd": "g++ \"${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=gnu++11 \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\"" } ] } –  May 21 '14 at 17:36
  • I want to do a "-lpthread" for each file , To enable multi threading , "cmd": ["g++", "${file}", "-o", "${file_path}/${file_base_name}", "-std=gnu++11","-lpthread" ,"-g"], Does not ----link +++links the pthread library to my cpp file. thanks –  May 21 '14 at 18:49
  • 1
    @raikrahul - as far as I know, the options should come before the file name - `["g++", "-std=gnu++11", "-lpthread", "-g", "${file}", "-o", "${file_path}/${file_base_name}"]`. Basically, write out the command the same way you'd run it at the command line, or in your Makefile. And speaking of which, instead of editing the `.sublime-build` file for more and more complex options, I'd highly recommend using a Makefile instead, and just have the build system run `make`. – MattDMo May 21 '14 at 19:17
  • Thank you for referecne to PackageReourceViewer. It helps a lot! – vittore Sep 11 '16 at 16:39
  • 2
    If you add/create a custom build script, it will be in a different location. For example, mine is in: **~/.config/sublime-text-3/Packages/User/c++11.sublime-build** – rtfminc Aug 29 '18 at 02:58
  • 1
    If it is a custom build, on MacOS they are on: `~/Library/Application Support/Sublime Text 3/Packages/User` – Jorge Sampayo Feb 13 '20 at 15:15
  • Why do they make it so hard to edit or even rename these files?! Why not have an edit option in the Sublime menu. This is crazy. – Joshua Pinter Oct 28 '20 at 21:38
  • Can confirm this works on MacOS Big Sur 11.5 – aayushgupta Jul 29 '21 at 12:26
4

In my case, the problem is that in Windows, ST3 was calling py instead of python which was the default. If you change python in "cmd": ["python", "-u", "$file"] for your local python interpreter, the new system should work.

{
        "cmd": ["python3", "-u", "$file"],
        "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
        "selector": "source.python",
    
        "env": {"PYTHONIOENCODING": "utf-8"},
    
        "windows": {
            "cmd": ["python", "-u", "$file"],
        },
    
        "variants":
        [
            {
                "name": "Syntax Check",
                "cmd": ["python3", "-m", "py_compile", "$file"],
    
                "windows": {
                    "cmd": ["py", "-m", "py_compile", "$file"],
                }
            }
        ]
    }
9395
  • 51
  • 1