如何用 sublime text 编译实现C/C++代码的编译运行

如何用 Sublime Text 实现C/C++代码的编译运行_百度知道Sublime&text2&C/C++&编译环境设置
是一款强大的跨平台代码编辑器,小巧而且丰富实用的功能是Visual
Studio不能比拟的,但是编译运行是一个软肋,本文通过在sublime中配置g++编译器实现程序的编译功能。具体做法一共有四个步骤:
下载安装Sublime text2 for
下载安装 MinGW 与 系统变量设置
Sublime Building System
一、下载安装Sublime text2 for
下载地址:/
二、下载安装 MinGW
与系统变量设置
2.1 安装MinGW
本文使用的C/C++编译器是gcc/g++,所以需要下载安装MinGW(下载地址:http://sourceforge.net/projects/mingw/)
软件安装界面:
安装完成后会让用户选择需要下载的Package
选择要下载的Package:
如图所示,选择 MinGW Compiler Suite之后,添加 The GNU C++ Compiler
的相关项即可。
2.2 设置环境变量
右击我的电脑,点属性-&高级-&环境变量。
在环境变量PATH 添加系统&MinGW&的实际安装位置,如:
D:\Program Files\MinGW 或者比如本文中演示的C:\MinGW\bin。在PATH里加入C:\MinGW\bin(具体路径请根据你的MinGW选择)。如果PATH里面还有其他的变量,记得要加个英文半角分号。一般&PATH
中的变量会非常的多,不同变量之间使用;分隔。<img src="/blog7style/images/common/sg_trans.gif" real_src ="/mw690/0021pgHJgy6I0d43kVJd3&690" STYLE="margin: 0display:block" WIDTH="357" HEIGHT="145" NAME="image_operate_13240"
ALT="Sublime&text2&C/C++&编译环境设置"
TITLE="Sublime&text2&C/C++&编译环境设置" />
新建LIBRARY_PATH变量,如果有的话,在值中加入C:\MinGW\lib,这是标准库的位置。<img src="/blog7style/images/common/sg_trans.gif" real_src ="/mw690/0021pgHJgy6I0d8psJb79&690" STYLE="margin: 0 display:" NAME="image_operate_73373"
ALT="Sublime&text2&C/C++&编译环境设置"
TITLE="Sublime&text2&C/C++&编译环境设置" />
新建C_INCLUDEDE_PATH变量,值设为C:\MinGW\include。<img src="/blog7style/images/common/sg_trans.gif" real_src ="/mw690/0021pgHJgy6I0d6oxqqea&690" STYLE="margin: 0display:block" WIDTH="357" HEIGHT="150" NAME="image_operate_71281"
ALT="Sublime&text2&C/C++&编译环境设置"
TITLE="Sublime&text2&C/C++&编译环境设置" />
检查变量设置:Win+R输入: cmd ,在命令行中输入: g++ -v
,有内容输入证明环境变量配置正确.如果出现 'g++' 不是内部或外部命令,也不是可运行的程序或批处理文件。
检查上一步配置.
如果出现上面的画面,说明了你安装成功了,否则就是环境变量设置的问题。需要修改一下环境变量。在已经存在的环境变量前面添加上mingw的路径。因为Windows的环境变量修改之后不会立马生效,需要重启电脑。如果想省事,不想重启电脑的话可以直接在cmd中输入set
path c:\这样环境变量就会被强制刷新。
三、Sublime Building System
Windows下,要在Sublime Text
2中实现编译、运行C/C++代码,需要修改或新建一个C++编译配置。
具体是:Sublime Text 2中Tools -& Build System -&
New Build System
输入如下内容,并将文件保存为C++Bulider.sublime-bulid。
& &"cmd": ["g++", "${file}",
"-o", "${file_path}/${file_base_name}"],
& &"file_regex":
"^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
& &"working_dir":
"${file_path}",
& &"selector": "source.c,
source.c++",
& &"encoding":
& &"shell": true,
& &"variants":
&"name": "Run",
&//"cmd": ["CMD", "/U", "/C", "g++ ${file} -o
${file_base_name} &&
${file_base_name}"]&
&"cmd": [ "start",
"${file_path}/${file_base_name}.exe"]
四、编译测试
经过以上步骤搭建好C/C++编译环境后,就可以在Sublime
Text 2中编译运行C/C++代码了。
如图所示:
ctrl+B:生成
ctrl+shift+B:运行
生成的可执行文件在cpp同目录下:
下一篇:sublime 中模板的设置
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。原文链接:/double_dua/blog/static//
sublime Text 2 是一个强大的跨平台的文本编辑器。
这几天都在用这个编辑器来写C++的程序。
刚刚装上的时候不能编译运行啊什么的痛苦死了。
编译问题 :
首先你的电脑里面要有g++的编译器。怎么看有木有呢?
只要打开命令控制台 输入 gcc 或者 g++ 若提示如下图所示即可。
如果你没有的话就去下载一个DEV-C++或者C-Free安装起来。
然后你就要去设置环境变量
计算机-&右键属性-&高级系统设置-&环境变量
找到系统变量里面的path在后面添加你g++的根目录
例如我电脑上的dev是在D:\PROGRAM FILES\DEV-CPP\MINGW32\BIN
到目前为止在sublime里面可以编译程序了。并且它会在你cpp文件的目录下产生一个同名的exe文件。
但是当你要运行的时候就出现问题了。
这个该怎么办呢?每次到目录下面找到exe文件然后双击打开感觉很麻烦!!!
到Tools -& Build System -& New Build System
在新打开的文件里面输入下面的内容然后保存。
"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",
"cmd" : ["${file_path}/${file_base_name}"]
然后你在试下运行编译好的程序。
这样程序的输出结果就显示在下面的灰色的框框里面了。
但是这样还是有些问题。就是不能输入东西。
然后我呢是用输入输出重定向freopen来解决这个问题= =
在该cpp文件目录下面新建一个txt文本来存放输入数据。
ps: cpp文件路径或是g++的路径最好都是英文。因为sublime text2 对中文不支持
------------------------------------------------------------------------------
dev c++环境变量那段我按作者说的去做不行,而且本来电脑上没有Path这个环境变量,要自己新建一个,我的环境变量值为E:\Program Files\DEV-CPP\Bin(我的dev装在E盘)
设置该环境变量就是为了要能够在命令行调用这些程序:
阅读(...) 评论()如何用 Sublime Text 实现C&#47;C++代码的编译运行_百度知道Posts - 38,
Articles - 5,
Comments - 34
如果离成功只有一步之遥,那谁都会咬牙坚持下来;如果忽然发现距离很远,那谁都会放弃。而关键的一咬牙,上了一个层次,有人就会春风得意,越活越成功;有人没上去,永远在苦逼中挣扎求生存。然后想,原来在当时,我们俩的距离才那么近。
23:50 by Rudrj2, ... 阅读,
Sublime Text2的高亮显示代码,非常好用,界面也非常漂亮,Sublime Text2工具栏有编译项,所以想让它来编译C和C++代码,不想每次几行代码也都打开visual studio,网上有gcc的解决方案。我想用微软的编译器,因为电脑里面已经装了Visual studio 2010,所以想用系统已有的编译器了.
&1& &如果你的编译环境是GCC并且已经可以在命令行里用gcc编译源文件,那么Sublime Text2不要任何配置就可以对单个源文件进行编译和运行,下面的东西可以不看了。
&2& 如果没有GCC或者就是希望用Visual Studio里的编译器CL进行编译运行,那么你可以安装下面的操作完成配置。
一、利用VS2010搭建命令行编译环境
本人机子是vista,在&我的电脑&上,右键找到&属性&,选择&高级系统设置&,进到&环境变量&里面;
(1)创建三个系统变量
  在命令行输入set命令会有对应的VS信息,vs2010是VS100COMNTOOLS
&1&名字: VS100Common
&&&&&& 值: C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7
&2&名字: VS100VC
&&&&&& 值: C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC
&3&名字: VS100SDK
&&&&&& 值: C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A
(2)创建(或追加)三个用户变量
&1&名字: PATH
值 :%VS100VC%\BIN;%VS100Common%\IDE;%VS100Common%\TOOLS;%VS100SDK%\BIN;%PATH%
&2&名字: INCLUDE
值 :%VS100VC%\INCLUDE;%VS100VC%\ATLMFC\INCLUDE;%VS100SDK%\INCLUDE;%INCLUDE%
&3&名字: LIB
值 :%VS100VC%\LIB;%VS100VC%\ATLMFC\LIB;%VS100SDK%\LIB;%LIB%
(3)打开终端运行cl,如果没有提示非命令的话,基本配置成功了,本人机子是vista直接重开cmd就可以运行了。。如果其他系统无法运行的话,考虑重启。
(4)随便写一个程序:
#include &iostream&
int main()
unsigned int a = ~0;
if( a&65536 )
cout&&"32 bit"&&
cout&&"16 bit"&&
(5)编译运行:
二、Sublime Text2搭建C/C++开发环境
(1)打开Sublime Text2,选择tools,然后选择Build System,然后选择 New&Build System。
  然后在里面输入下面的代码:
"cmd": ["CL", "/Fo${file_base_name}", "/O2", "${file}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"encoding":"cp936",
"variants":
"name": "Run",
"cmd": ["CMD", "/U", "/C", "CL /Fo${file_base_name} /O2 ${file} && ${file_base_name}"]
(2)然后保存为:MSComplie.sublime-build,注意后缀一定为sublime-build。
上面的代码仅仅是在原来代码的基础了针对windows平台下CL的修改了两节,同时加了几个逗号,并且修改了编译环境的编码,因为默认的Sublime Text 2的编码是UTF-8。。
代码的原理很简单,就是在命令行里编译源文件的命令&CL /FoObjectName /O FileName .
打开上面那个demo测试下:ctrl+B编译
Ctrl+Shift+B运行程序界面如下
如果编译运行时遇到如下错误:
LINK : fatal error LNK1104
你编译产生的***.exe文件已经装入内存了,故编译好以后无法将编译后的 ***.exe文件覆盖上去。只需要打开任务管理进行关闭对应的EXE进程。
下面的文字摘自官方文档Build_Systems,以供参考:&/en/latest/reference/build_systems.html
File Format
.build-system&files use JSON. Here&s an example:
"cmd": ["python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
Array containing the command to run and its desired arguments. If you don&tspecify an absolute path, the external program will be searched in yourPATH, one of your system&s environmental variables.
On Windows, GUIs are supressed.
file_regexOptional. Regular expression (Perl-style) to capture error output ofcmd. See the next section for details.line_regexOptional. Iffile_regex&doesn&t match on the current line, butline_regex&exists, and it does match on the current line, thenwalk backwards through the buffer until a line matchingfileregex&isfound, and use these two matches to determine the file and line to go to.selectorOptional. Used whenTools | Build System | Automatic&is set totrue.Sublime Text uses this scope selector to find the appropriate build systemfor the active view.working_dirOptional. Directory to change the current directory to before runningcmd.The original current directory is restored afterwards.encodingOptional. Output encoding ofcmd. Must be a valid python encoding.Defaults toUTF-8.target
Optional. Sublime Text command to run. Defaults toexec&(Packages/Default/exec.py).This command receives the configuration data specified in the.build-system&file.
Used to override the default build system command. Note that if you chooseto override the default command for build systems, you can add arbitraryvariables in the.sublime-build&file.
Optional. Dictionary of environment variables to be merged with the currentprocess& before passing them tocmd.
Use this element, for example, to add or modify environment variableswithout modifying your system&s settings.
shellOptional. Iftrue,cmd&will be run through the shell (cmd.exe,bash&).path
Optional. This string will replace the current process&PATH&beforecallingcmd. The old&PATHvalue will be restored after that.
Use this option to add directories toPATH&without having to modifyyour system&s settings.
Capturing Error Output withfile_regex
The&file_regex&option uses a Perl-style regular expression to capture upto four fields of error information from the build program&s output, namely:file name,line number,column number&anderror message. Usegroups in the pattern to capture this information. Thefile name&field andtheline numberfield are required.
When error information is captured, you can navigate to error instances inyour project&s files withF4&andShift+F4. If available, the capturederror message&will be displayed in the status bar.
Platform-specific Options
The&windows,osx&andlinux&elements let you provideplatform-specific data in the build system. Here&s an example:
"cmd": ["ant"],
"file_regex": "^ *\\[javac\\] (.+):([0-9]+):() (.*)$",
"working_dir": "${project_path:${folder}}",
"selector": "source.java",
"windows":
"cmd": ["ant.bat"]
In this case,ant&will be executed for every platform except Windows, whereant.bat&will be used instead.
Build systems expand the following variables in.sublime-build&files:
$file_path
The directory of the current file, e. g.,C:Files.
The full path to the current file, e. g.,C:FilesChapter1.txt.
$file_name
The name portion of the current file, e. g.,Chapter1.txt.
$file_extension
The extension portion of the current file, e. g.,txt.
$file_base_name
The name only portion of the current file, e. g.,Document.
The full path to thePackages&folder.
The full path to the current project file.
$project_path
The directory of the current project file.
$project_name
The name portion of the current project file.
$project_extension
The extension portion of the current project file.
$project_base_name
The name only portion of the current project file.
Place Holders for Variables
Features found in snippets can be used with these variables. For example:
${project_name:Default}
This will emit the name of the current project if there is one, otherwiseDefault.
${file/\.php/\.txt/}
This will emit the full path of the current file, replacing.php&with.txt.

我要回帖

更多关于 sublime text怎么编译 的文章

 

随机推荐