sublime text 2 配置 c\c++ 开发环境
1.搭建基础配置:
-
下载安装 MinGW 下载
-
配置环境变量
我的电脑 -> 属性 -> 高级系统设置 -> 环境变量
注意 PATH 中的变量会非常的多,不同变量之间使用
;
分隔. -
新建环境变量
MinGW_PATH
填写系统 mingw 的实际安装位置,如:D:\Program Files\MinGW
-
编辑
PATH
在最后添加%MinGW_PATH%\bin;%PATH%;
-
检查变量设置
-
win+r
,输入:cmd
,在命令行中输入:g++ -v
,有内容输入证明环境变量配置正确.如果出现'g++' 不是内部或外部命令,也不是可运行的程序或批处理文件。
检查上一步配置.
2.sublime text 2 的初始化配置
-
下载安装 sulime text 2 下载
-
基础插件安装
-
Ctrl+Shift+p
输入:install
,回车.输入:Theme - Aqua
-
按
Ctrl+`
调出console粘贴以下代码到底部命令行并回车: -
重启Sublime Text 2。如果在Perferences->package settings中看到package control这一项,则安装成功。
-
import urllib2,os; pf='Package Control.sublime-package'; ipp=sublime.installed_packages_path(); os.makedirs(ipp) if not os.path.exists(ipp) else None; urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler())); open(os.path.join(ipp,pf),'wb').write(urllib2.urlopen('http://sublime.wbond.net/'+pf.replace(' ','%20')).read()); print('Please restart Sublime Text to finish installation')
-
Aqua Theme 主题:(可选)
-
其他插件:(待更新)
-
个人配置文档设置 >
Sublime Text 2 -> Preferences -> Settings - User
// 尽量不要修改其中的Setting - Defunct
文件.添加如下代码,使用
shift+space
呼出自动提示,最外面有
[]
包裹,小项之间使用,
分隔:{ "keys": ["shift+space"], "command": "auto_complete" }, { "keys": ["shift+space"], "command": "replace_completion_with_auto_complete", "context": [ { "key": "last_command", "operator": "equal", "operand": "insert_best_completion" }, { "key": "auto_complete_visible", "operator": "equal", "operand": false }, { "key": "setting.tab_completion", "operator": "equal", "operand": true } ] }
-
我的c++ build 文件
-
其中的cb_console_runner.exe点此下载 放置c盘根目录下,同样可以将其放置在其他目录,需将其路径放入PATH变量中.
-
修改快捷键设置,修改
Sublime Text 2 -> Preferences -> key Bindings - User
添加如下: -
c++ build 文件windows 下实现一键编译,错误停止,弹终端显示
{ "cmd": ["g++", "${file}", "-o", "${file_path}/${file_base_name}","-Wall" ,"&&","start","C:/cb_console_runner.exe","${file_path}/${file_base_name}"], "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$", "working_dir": "${file_path}", "selector": "source.c, source.c++", "shell": true, "encoding": "cp936", "variants": [ { "name": "Run", "cmd": ["start","C:/cb_console_runner.exe","${file_path}/${file_base_name}"] } ] }
{ "keys": ["f5"], "command": "build"}
实现f5编译运行,出错停止.如下图所示:
-
"font_face":"courier new bold"
:设置字体为Courier new
, 样式为加粗 -
"font_size": 10.5
: 设置字号:10.5 -
"theme": "AppKit.sublime-theme"
: 使用上面安装的主题. -
"auto_complete": false
:关掉该死的自动提示,还是建议你现在关掉的好,不然在安装完另一个插件之后,只要打字就出现提示,烦死人了鸟. -
"tab_completion": true
:打开tab
补全,直接显示最佳匹配项,多次按键有惊喜. -
我喜欢不打扰人的补全,通过使用快捷键呼出补全,这样的话,可以修改一下
Sublime Text 2 -> Preferences -> key Bindings - User
:
3.安装并配置sublimeClang 插件
-
安装sublimeClang插件:
-
ctrl+shift+p
呼出控制面板输入Package Control: Add Repository
,输入以下网址: -
ctrl+shift+p
呼出控制面板输入Package Control: Upgrade Package
选择升级组件 -
首先需要升级你的 package contro,不然会出现提示已安装,但是怎么都找不到的问题:
https://sublime.wbond.net/testing/packages.json
-
安装
sublimeClang
: 通过Package Control
安装SublimeClang
即可.
-
插件设置
"options": [ "-m32", "-w", "-ferror-limit=9", "-fgnu-runtime", "-fms-extensions", "-nostdinc", // 此处填写自己pc中的实际位置和版本号. "-isystem", "C:\\MinGW\\lib\\gcc\\mingw32\\4.6.2\\include", "-isystem", "C:\\MinGW\\lib\\gcc\\mingw32\\4.6.2\\include\\c++", "-isystem", "C:\\MinGW\\lib\\gcc\\mingw32\\4.6.2\\include\\c++\\mingw32", "-isystem", "C:\\MinGW\\include", "-D__GNUC__=4", "-D__GNUC_MINOR__=2", "-D__GNUC_PATCHLEVEL__=1", "-D__GXX_ABI_VERSION__=1002", "-Di386=1", "-D__i386=1", "-D__i386__=1", "-DWIN32=1", "-D_WIN32=1", "-D__WIN32=1", "-D__WIN32__=1", "-DWINNT=1", "-D__WINNT=1", "-D__WINNT__=1", "-D_X86_=1", "-D__MSVCRT__=1", "-D__MINGW32__=1", "-D__STDC_VERSION__=201112L" ],
-
修改
Sublime Text 2 -> Preferences -> package setting -> SublimeClang -> settings - Default
-
show_output_panel
: true或false,错误输出框,建议关闭掉,太烦人了. -
dont_prepend_clang_includes
: true或false。这个选项控制是否不包括clang自身的头文件。建议用true取消包括,以防产生冲突。 -
options
。这个是配置的大头。可以注意到这里的形式和clang命令行调用时的编译选项是一样的(只是不包括指明lib的部分)。简单的修改它Default项中头文件目录的选项很可能是没有效果的(尤其在Windows下)。我最常使用MinGW GCC参考SublimeClang某issue里一位用户提供的配置,修改了那几个目录之后工作完全正常。可以看到里面还有很多宏定义的问题。这个必须要参考自身的开发环境。下面是我的配置:
4.配置插件实现一键保存加编译
按键配置
{"keys": ["alt+`"],"command": "saveandbuild"},
新建插件,写下如下代码
import sublime, sublime_plugin
class SaveandbuildCommand(sublime_plugin.TextCommand): //注意saveandbuild首字母大写了但是命令里要小写
def run(self, edit):
self.view.run_command("save");
sublime.active_window().run_command('build');
保存到user文件夹,文件名saveandbuild.py
一键保存文件加编译,OK~~
关于那个
cb_console_runner.exe
/* * This file is part of the Code::Blocks IDE and licensed under the GNU General Public License, version 3 * http://www.gnu.org/licenses/gpl-3.0.html * * $Revision: 6159 $ * $Id: main.cpp 6159 2010-02-15 04:08:57Z biplab $ * $HeadURL: file:///svnroot/repos/codeblocks/trunk/src/tools/ConsoleRunner/main.cpp $ */#define __WXMSW__ // 单独编译 WXMSW#include <stdio.h> #include <stdlib.h> #include <time.h> // VC编译修改了#ifdef __WXMSW__ #include <windows.h> #include <conio.h> #define wait_key getch#else #define wait_key getchar#endif#if defined(__unix__) || defined(__unix) #include <sys/wait.h>#endif#include <string.h> #ifdef __MINGW32__int _CRT_glob = 0;#endifbool hasSpaces(const char* str) { char last = 0; while (str && *str) { if ((*str == ' ' || *str == '/t') && last != '//') return true; last = *str++; } return false; }int execute_command(char *cmdline) { #ifdef __WXMSW__ //Windows's system() seems to not be able to handle parentheses in //the path, so we have to launch the program a different way. SetConsoleTitle(cmdline); STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); ZeroMemory( &pi, sizeof(pi) ); // Start the child process. CreateProcess( NULL, TEXT(cmdline), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ); // Wait until child process exits. WaitForSingleObject( pi.hProcess, INFINITE ); // Get the return value of the child process DWORD ret; GetExitCodeProcess( pi.hProcess, &ret ); // Close process and thread handles. CloseHandle( pi.hProcess ); CloseHandle( pi.hThread ); return ret;#else int ret = system(cmdline); if(WIFEXITED(ret)) { return WEXITSTATUS(ret); } else { return -1; }#endif}int gettimeofday(struct timeval *tp, void *tzp) // VC编译修改了,VC没有 gettimeofday() 添加了{ time_t clock; struct tm tm; SYSTEMTIME wtm; GetLocalTime(&wtm); tm.tm_year = wtm.wYear - 1900; tm.tm_mon = wtm.wMonth - 1; tm.tm_mday = wtm.wDay; tm.tm_hour = wtm.wHour; tm.tm_min = wtm.wMinute; tm.tm_sec = wtm.wSecond; tm. tm_isdst = -1; clock = mktime(&tm); tp->tv_sec = clock; tp->tv_usec = wtm.wMilliseconds * 1000; return (0); }int main(int argc, char** argv) { if (argc < 2) { printf("显示调用的程序的返回参数和执行时间。/n/n"); printf("Usage: cb_console_runner <filename> <args ...>/n"); printf("用 法: cb_console_runner <可执行文件> <参数 ...>/n"); return 1; } // count size of arguments // 参数的计数大小 size_t fullsize = 0; for (int i = 1; i < argc; ++i) { fullsize += strlen(argv[i]); } // add some slack for spaces between args plus quotes around executable // 添加可执行程序周围一些参数加引号之间的空间懈怠 fullsize += argc + 32; char* cmdline = new char[fullsize]; memset(cmdline, 0, fullsize); // 1st arg (executable) enclosed in quotes to support filenames with spaces // 第一个参数(可执行文件),以支持在引号用空格文件名 bool sp = hasSpaces(argv[1]); if (sp) strcat(cmdline, "/""); strcat(cmdline, argv[1]); if (sp) strcat(cmdline, "/""); strcat(cmdline, " "); for (int i = 2; i < argc; ++i) { sp = hasSpaces(argv[i]); if (sp) strcat(cmdline, "/""); strcat(cmdline, argv[i]); if (sp) strcat(cmdline, "/""); strcat(cmdline, " "); } timeval tv; gettimeofday(&tv, NULL); double cl = tv.tv_sec + (double)tv.tv_usec / 1000000; int ret = execute_command(cmdline); gettimeofday(&tv, NULL); cl = (tv.tv_sec + (double)tv.tv_usec / 1000000) - cl; printf("/nProcess returned %d (0x%X) execution time : %0.3f s", ret, ret, cl); printf("/n进程返回 %d (0x%X) 执行时间 : %0.3f 秒", ret, ret, cl); printf ( "/n请按"#ifdef __WXMSW__ "任意键"#else "ENTER"#endif "继续.../n" ); wait_key(); // 前面宏定义 #define wait_key getch delete[] cmdline; return ret; }
View Code
/*- * Copyright (C) 2008 mymtom (mymtom@hotmail.com) * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $Id: src/lib/libtrace/gettimeofday.c,v 1.1 2008/03/28 12:08:44 mymtom Exp $ */#include <time.h> #ifdef WIN32 # include <windows.h>#else# include <sys/time.h>#endif#ifdef WIN32intgettimeofday(struct timeval *tp, void *tzp) { time_t clock; struct tm tm; SYSTEMTIME wtm; GetLocalTime(&wtm); tm.tm_year = wtm.wYear - 1900; tm.tm_mon = wtm.wMonth - 1; tm.tm_mday = wtm.wDay; tm.tm_hour = wtm.wHour; tm.tm_min = wtm.wMinute; tm.tm_sec = wtm.wSecond; tm. tm_isdst = -1; clock = mktime(&tm); tp->tv_sec = clock; tp->tv_usec = wtm.wMilliseconds * 1000; return (0); }#endif /* WIN32 */
View Code
http://xui.ptlogin2.qq.com/cgi-bin/xlogin?appid=716027609&pt_3rd_aid=101222510&style=35&s_url=http%3A%2F%2Fconnect.qq.com&refer_cgi=authorize&which=&response_type=code&client_id=101222510&state=ed39411621e53dd29065fd06e6ff5667&scope=get_user_info%2Cadd_share%2Clist_album%2Cadd_album%2Cupload_pic%2Cadd_topic%2Cadd_one_blog%2Cadd_weibo&redirect_uri=http%3A%2F%2Fxuji.pro%2F%3Fconnect%3Dqq%26action%3Dcallback