You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Sublime Text 3构建系统,实现C++11编译并打开CMD运行?

在Sublime Text 3中配置支持C++11且自动打开CMD的构建系统

需求与现有问题

  • 要实现的效果:在Sublime Text 3里编译运行C++11代码,运行时自动弹出独立的CMD窗口,不用软件内置控制台。
  • 现有两个构建系统都有缺陷:
    1. 能支持C++11编译,但运行时在Sublime内置控制台执行,不符合要求:
      {"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}\""
          }
      ]
      }
      
    2. 运行时会自动打开CMD,但只支持C98标准,满足不了C11的需求:
      { 
          "cmd":
           [
               "g++", "-Wall", "-ansi", "-pedantic-errors", "$file_name", "-o", 
               "${file_base_name}.exe", "&&", "start", 
               "cmd", "/k" , "$file_base_name"
          ], 
           "selector": "source.cpp", 
           "working_dir": "${file_path}", 
           "shell": true 
      } 
      

修改方案

修改任意一个构建系统都能实现需求,下面是两种具体的修改方式:

方案一:修改第二个构建系统

原系统里的-ansi参数等价于指定C98标准,直接把它换成-std=c++11就能支持C11了,同时可以保留其他编译提示参数,修改后的代码如下:

{ 
    "cmd":
     [
         "g++", "-Wall", "-std=c++11", "-pedantic-errors", "$file_name", "-o", 
         "${file_base_name}.exe", "&&", "start", 
         "cmd", "/k" , "${file_base_name}.exe"
    ], 
     "selector": "source.cpp", 
     "working_dir": "${file_path}", 
     "shell": true 
} 
  • 补充说明:/k会让CMD窗口在程序运行完后保持打开,方便查看输出;如果想让程序结束后窗口自动关闭,把/k换成/c就行。

方案二:修改第一个构建系统的Run变体

保留原系统的编译配置,只修改Run变体的运行命令,加上start cmd /k来启动独立CMD窗口:

{"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}\" && start cmd /k \"${file_path}/${file_base_name}\""
    }
]
}
  • 同样,把/k换成/c就能让窗口在程序结束后自动关闭。

内容的提问来源于stack exchange,提问作者Lowell0803

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 09:30:46