怎样让sublime text3运行c/c++代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎样让sublime text3运行c/c++代码相关的知识,希望对你有一定的参考价值。

我的解决办法:新建一个build systems
//my_cpp


"cmd": ["g++", "$file", "-o", "$file_path/$file_base_name"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "$file_path",
"selector": "source.c, source.c++",
"shell": true,
"variants":
[

"name": "Run",
"cmd": ["g++", "$file", "-o", "$file_path/$file_base_name", "&", "$file_path/$file_base_name.exe"]

]

但是只能通过点手动run,快捷键ctrl shift B不起作用

这样就解决了:
//my_cpp

//"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "$file_path",
"selector": "source.c, source.c++",
"shell": true,
"cmd": ["g++", "$file", "-o", "$file_path/$file_base_name", "&", "$file_path/$file_base_name.exe"]


这是我试出来更好的
//C++_My

//"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "$file_path",
"selector": "source.c, source.c++",
"shell": true,
"cmd": [ "g++", "$file", "-o", "$file_path/$file_base_name", "&","start", "$file_path/$file_base_name.exe"]
参考技术A 下载MinGW 下载地址: sourceforge.net/projects/mingw/files/ 打开下载的安装程序,按如下方式安装 `Install`->`Change`->`D:\MinGW`->`Continue`->`Continue` `mingw32-base`->`mingw32-gcc=g++`->`Installation`->`Apply Changes`->`Apply`->

sublime text 2 基于 MinGW 的 C/C++ 配置文件

  之前按照网上的方法在 sunlime text 2 中配置了 C/C++ 开发环境,但是每次在菜单切换C和C++不同的编译配置,感觉很麻烦,折腾了下,自己按照个人喜好对配置文件做了点改动,最后结果如下:

 

快捷键说明:

运行:

  在底部文本栏显示结果:Ctrl + ,

  在cmd窗口中显示结果:Ctrl + .

C:

  编译:Ctrl + Shift + B

  编译并运行(在底部文本栏显示结果):Ctrl + Shift + ,

  编译并运行(在cmd窗口中显示结果):Ctrl + Shift + .

C++:

  编译:Ctrl + B

  编译并运行(在底部文本栏显示结果):Ctrl + Alt + ,

  编译并运行(在cmd窗口中显示结果):Ctrl + Alt + .

 

编译配置文件: Menu --> Tools --> Build System --> New Build System...

如果未安装ConverToUTF8插件,请按照注释中的说明修改(第6行)

MinGW64-11.sublime-build :

{
     "cmd": ["g++", "${file}", "-o", "${file_path}/${file_base_name}", "-Wall", "-std=c++11"],
     "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
     "working_dir": "${file_path}",
     "selector": "source.c, source.c++",
     "encoding": "GBK",  //If you haven`t install ConvertToUTF8, please install it or delete/comment this line or use "encoding": "utf-8",
     "shell": true,

     "variants":
     [
          {
               //Shortcut: Ctrl+Shift+B
               "name": "Run", //Build C
               "cmd": ["gcc", "${file}", "-o", "${file_path}/${file_base_name}", "-Wall", "-std=c11"]
          },
          {
               //Shortcut: Ctrl+,
               "name": "RunLocal", //Run in bottom text box
               "cmd": ["${file_path}/${file_base_name}"]
          },
          {
               //Shortcut: Ctrl+.
               "name": "RunCmd",   //Run in cmd window
               "cmd": [ "start", "CMD", "/C", "${file_path}/${file_base_name} & pause"]
          },
          {
               ////Shortcut: Ctrl+Shift+,
               "name": "BuildCAndRunLocal",  //Build C & run in bottom text box
               "cmd": ["CMD", "/C", "gcc ${file} -o ${file_path}/${file_base_name} -Wall -std=c11 & ${file_path}/${file_base_name}.exe"]
          },
          {
               //Shortcut: Ctrl+Shift+.
               "name": "BuildCAndRunCmd",    //Build C & run in cmd window
               "cmd": ["start", "CMD", "/C", "@echo off & gcc ${file} -o ${file_path}/${file_base_name} -Wall -std=c11 & @echo on & ${file_path}/${file_base_name}.exe & pause"]
          },
          {
               //Shortcut: Ctrl+Alt+,
               "name": "BuildCppAndRunLocal",     //Build C++ & run in bottom text box
               "cmd": ["CMD", "/C", "g++ ${file} -o ${file_path}/${file_base_name} -Wall -std=c++11 & ${file_path}/${file_base_name}.exe"]
          },
          {
               //Shortcut: Ctrl+Alt+.
               "name": "BuildCppAndRunCmd",  //Build C++ & run in cmd window
               "cmd": ["start", "CMD", "/C", "@echo off & g++ ${file} -o ${file_path}/${file_base_name} -Wall -std=c++11 & @echo on & ${file_path}/${file_base_name}.exe & pause"]
          }
     ]
}

 

快捷键配置文件:Menu --> Preferences --> Key Bindings - User

Default (Windows).sublime-keymap :

[
	{ "keys": ["ctrl+0"], "command": "build", "args": {"variant": "BuildC" } },
	{ "keys": ["ctrl+,"], "command": "build", "args": {"variant": "RunLocal" } },
	{ "keys": ["ctrl+."], "command": "build", "args": {"variant": "RunCmd" } },
	{ "keys": ["ctrl+shift+,"], "command": "build", "args": {"variant": "BuildCAndRunLocal" } },
	{ "keys": ["ctrl+shift+."], "command": "build", "args": {"variant": "BuildCAndRunCmd" } },
	{ "keys": ["ctrl+alt+,"], "command": "build", "args": {"variant": "BuildCppAndRunLocal" } },
	{ "keys": ["ctrl+alt+."], "command": "build", "args": {"variant": "BuildCppAndRunCmd" } }
]

  

以上是关于怎样让sublime text3运行c/c++代码的主要内容,如果未能解决你的问题,请参考以下文章

sublime Text3写好了C语言文件,但是不能运行。

sublime text3编译运行C,Java程序的一些配置

Sublime Text3在Windows下怎样运行PHP代码

SUBLIME TEXT3 运行TURTLE后,如何不自动关闭TURTLE的界面窗口

如何在sublime text3运行nodejs

sublime text3 最新激活码