2

I'm using SublimeText-3 on macOS to do C++ programming, trying to take advantage of the build feature.

In this context I'll expect to (only) build my program by doing Cmd+B. And Cmd+Shift+B will run it.

But when I hit Cmd+B both happen, build and run.

Here are the contents of C++ Single File.sublime-build:

{
        "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=c++14 \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\""
                }
        ]
}

Anything strange in it?

How do I split build and run to different commands?

Some reference: Sublime text 3 - compile program and run in terminal

In Preferences - Key Bindings I see:

{ "keys": ["f7"], "command": "build" },
{ "keys": ["super+b"], "command": "build" },
{ "keys": ["super+shift+b"], "command": "build", "args": {"select": true} },

I think it's ok.

KcFnMi
  • 5,516
  • 10
  • 62
  • 136

1 Answers1

0

Have you checked your key bindings? They might somehow be assigned to the same key. In Preferences - Key Bindings, you should see your settings. I'm not a mac user, but your c++ build file looks fine. The setting for build in your key bindings should be like this:

{ "keys": ["ctrl+b"], "command": "build" },
{ "keys": ["ctrl+shift+b"], "command": "build", "args": {"select": true} }

Don't forget to change ctrl to whatever you have on macs.

Utku Tombul
  • 176
  • 1
  • 1
  • 7
  • Question edit to append information related to your answer. Do you see any problem? – KcFnMi May 27 '17 at 14:24
  • I made a quick search on google, seems like people are having complaints on "super" key usage in Sublime Text. Could you try changing key bindings of build commands to something else without "super" key, and try if it behaves the same? – Utku Tombul May 27 '17 at 14:31
  • Well, `f7` is also assigned for that purpose (as presented in the question) and the behavior is the same. – KcFnMi May 27 '17 at 14:36
  • 1
    Can you try selecting "Tools - Build With" and clicking on C++ without the run option? Does it act the same? – Utku Tombul May 27 '17 at 14:50
  • It behaves differently. But I don't have C++14 features on that. How do I enable C++14 for it? I think it's a matter of adding `-std=c++14` somewhere. – KcFnMi May 27 '17 at 15:11
  • 1
    Just add -std=c++14 to shell_cmd parameter on top like it has on "Run" variant's shell_cmd. What I understood is; either your build-and-run shortcut overlaps with build shortcut or you have two different .sublime-build files for C++. – Utku Tombul May 27 '17 at 15:26