简体   繁体   中英

How to Automatically Run C++11 on Sublime Text 3 on macOS?

I'd like to automatically run c++ using Sublime Text 3 build system:

It works when I manually set the build system:

> Tools > Build System > C++11

What should I be changing to be able to set,

> Tools > Build System > Automatic 

and run C++11?

C++11.build-system

{
    "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++",
    "quiet": true,
    "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}'"]
        }
    ]
}

Test

#include <stdio.h>

int main()
{
    printf("Hello World!\n");
}

Output (Manual)

Hello World!

Output (Automatic)

[Finished in 0.1s]

My guess is that maybe it'd run using,

> Tools > Build System > C++ Single File 

C++ Single File.build-system:

// {
//  "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\"",
//  "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
//  "working_dir": "${file_path}",
//  "selector": "source.c++",

//  "variants":
//  [
//      {
//          "name": "Run",
//          "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\""
//      }
//  ]
// }

{
    "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}'"]
        }
    ]
}

Sublime chooses the build system for Automatic based on the syntax in use, which associates with the selector in the build system. For the case of C, that's source.c , for C++ it's source.c++ , and so on.

When you choose Tools > Build , Sublime matches the syntax of the current file against all of the build systems that it currently knows about, and chooses based on that. A few different things can happen here:

  1. No build system is associated with the scope of the file; no build is selected and No Build System is displayed in the status bar when you execute the build. In this case, nothing happens.

  2. There is exactly one build that is associated with the scope; that build is chosen and directly executed.

  3. There is more than one build that is associated with the scope. When this happens the first time, Sublime prompts you with a quick panel to ask you what build you want to execute, and then executes that build. So long as you leave the build system set the same, future builds will continue to automatically select and use that build.

It's important to note that when you use a variant in a build system, although there is only one sublime-build file, that file defines more than one build, so in that case, #3 above automatically applies.

From looking at your build system above, it seems like it should be working, particularly if manually selecting the build system in the Tools > Build System menu does what you expect it to.

Your custom build system has quiet: true enabled, which stops Sublime from displaying the execution time (and error information on build failure), but in the case of an automatic build your output above looks like it's printing [Finished] .

From that I would agree that in this case Sublime is choosing a different build system than you expect, which may indeed be the internal build (though based on your output above, you may have modified it).

Since the build you provided works when you do it manually, my best guess would be that you've hit case 3 above; there is more than one build that matches the scope of the file.

In such a case, using Tools > Build With... will get Sublime to prompt you again to choose what build you want. Doing that and making sure you pick the build you want may clear the problem for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM