Sublime text3配置C/C++編譯環境


安裝sublime text3后,一直很喜歡使用它看代碼(這個高亮配色真的很好看)。它默認的運行環境就有C/C++,在寫了一個hello world!后正常輸出,但在加入scanf()輸入后就不行了。在網上搜了一下,這個問題好像無解,可以自己安裝gcc/g++,然后配置一下,通過調用命令窗口解決。這里簡單記錄一下配置過程。

准備工作

下載sublime text3,並安裝

下載MinGW(包括gcc/g++),並安裝

windows系統安裝后,需要右鍵計算機->屬性->高級系統設置->環境變量,雙擊path,把我們MinGW的安裝路徑 C:\MinGW\bin 添加進去。

新建C編譯環境

打開sublime text3,選擇如下

中文版:工具 -> 編譯系統 -> 新建編譯系統
英文版:Tools -> Build System -> New Build System

VrY8Df.jpg

輸入下列代碼

// windows環境
{

	"working_dir": "$file_path",
	
	"cmd": "gcc -Wall \"$file_name\" -o \"$file_base_name\"",
	
	"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
	
	"selector": "source.c",
	
	"variants":
	
	[
	
	{
	
	     "name": "Run",
	
	     "shell_cmd": "gcc -Wall \"$file\" -o \"$file_base_name\" && start cmd /c \"${file_path}/${file_base_name} & pause\""
	
	}
	
	]

}
// mac環境
{
    "cmd": ["gcc", "${file}", "-o", "${file_path}/${file_base_name}"],
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c",

    "variants":
    [
        {
            "name": "Run",
            "cmd": ["bash", "-c", "gcc '${file}' -o '${file_path}/${file_base_name}' && open -a Terminal.app '${file_path}/${file_base_name}'"]
        }
    ]
}

保存配置文件

將untitled.sublime-build配置文件保存為C.sublime-build。

新建C++編譯環境

和新建C編譯環境的操作步驟一樣,輸入的代碼需要變一下,如下所示:

// windows系統
{
	"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": ["cmd", "/c", "g++", "${file}", "-o", "${file_path}/${file_base_name}", "&&", "cmd", "/c", "${file_path}/${file_base_name}"]
		},
		{
			"name": "RunInCommand",
			"cmd": ["cmd", "/c", "g++", "${file}", "-o", "${file_path}/${file_base_name}", "&&", "start", "cmd", "/c", "${file_path}/${file_base_name} & pause"]
		}
	]
}
// mac系統
{
	"cmd": ["bash", "-c", "g++ '${file}' -std=c++11 -stdlib=libc++ -o '${file_path}/${file_base_name}'"],
	    "file_regex": "^(..{FNXX==XXFN}*):([0-9]+):?([0-9]+)?:? (.*)$",
	    "working_dir": "${file_path}",
	    "selector": "source.c, source.c++",
	    "variants":
	    [
	        {
	          "name": "Run",
	          "cmd": ["bash", "-c", "g++ '${file}' -std=c++11 -stdlib=libc++ -o '${file_path}/${file_base_name}' && open -a Terminal.app '${file_path}/${file_base_name}'"]
	        }
	    ]
}

將untitled.sublime-build配置文件保存為C++.sublime-build。

現在就可以開始編譯C/C++程序了。


個人主頁:

www.codeapes.cn


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM