红联Linux门户
Linux帮助

Sublime text 3编译环境配置C++11

发布时间:2016-06-29 15:18:59来源:linux网站作者:坤哥玩CSDN

关于Sublime text3配置编译环境使其支持C++11,在网上找了很多,却发现大部分材料都对Linux和Windows下的环境配置没有做区分,无独有偶在网上找到了一个老外写的有关配置sublime Text3 的内容,十分详细,而且还可以解决在sublime text下编译运行C++不能输入的问题,感觉比国内某些文章写的好多了,特摘抄下来,方便日后使用:


# Why sublime-text?
# Because, it is lightweight, portable, and runs on Win/Linux/MacOS.
# and it is really awesome.
# it is more flexible and extensible.
# You can turn your Sublime-text editor into a FULL featured IDE.
# I really use it for C/C++/HTML/JS/CSS/Python/Java, and it is great.
# Sublime-Text Editor is more real, because it allows you to customize almost everything.
# For Compiled languages, you can use pre-built BUILD SYSTEM or create your own.
# Snippet, that is a great idea and that would be great if you learn, how to write snippet and use them.

# Why we are here?
# What is Build-system in Sublime-Text Editor?
# Build-system allows you to write and build programs directly ( Just hit, CTRL+b ) and if you want to run build binary ( Just hit, SHIFT+CTRL+b )
# Build System ( RUN, BUILD ) basically allows you to run Two COMMANS using their shortcut key, that really does not matter which command you want to invoke with CTRL+b and CTRL+SHIFT+b.

Sublime text 3编译环境配置C++11

# What about pre-built BUILD SYSTEM for C++
# Hmm, that is great, but we want some more functionality ( I want to pass some command line parameters ) , this is why we want to write our own Custom BUILD SYSTEM for C++.

# Sublime-Text Build System Script for C/C++
## When, You want to use Flags like, -std=c++11
#### C++11.sublime-build ( FOR LINUX )

{
"shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++, source.cpp",

"variants":
[
{
"name": "Run",
"shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\" -std=c++11 && \"${file_path}/${file_base_name}\""
}
]
}
####

## When, you want to run your program in Terminal ( xterm ) instead of sublime-Text console, Becaue you can pass input using Sublime-Text Console.
## NOTE, that you may not be able to see terminal because your program won't wait for your response.
## So, you need to stop your C/C++ program from being exiting automatically.
#### C++11.sublime-build

{
"shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++, source.cpp",

"variants":
[
{
"name": "Run",
"shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\" -std=c++11 && \"${file_path}/${file_base_name}\""
}
]
}
####

# When, you want to run your program in a separate terminal process, that won't exit automatically even if program exists itself or you can tell xTerm, not to close itself.
# FINAL Version : filename: C++11.sublime-build
# PATH : $USER_HOME/.config/sublime-text-3/Packages/User/
# FOR LINUX Operating System
#### C++11.sublime-build

{
"shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++, source.cpp",

"variants":
[
{
"name": "Run",
"shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\" -std=c++11 && xterm -fg green -bg black -hold -e \"${file_path}/${file_base_name}\""
}
]
}
####


注意上述代码中的默认Xterm中文是显示不全的,某些汉字会显示不出来,这时可以在上面xterm后面加上以下参数:

-fa \"文泉驿等宽正黑\" -fs 9

# FINAL Version : filename: C++11.sublime-build
# PATH : %sublime-text-3%\Packages\User\
# For Windows:-
# NOTE, Ensure that GNU GCC Compiler is in your System PATH environment variable.
# If you want to check whether g++/gcc BINARY is in your system PATH.
# use  which   Command for LINUX (  which -a g++ )
# use  where   Command for Windows  (  where gcc )

# So if you are a Windows user and you might want to use some external Terminal Emulator Programs for Windows, just like, ( mintty, console2, cmder, conemu ) .
# I recommend you to use mintty terminal emulator for windows that works just as  xterm   in Linux.

# Command Example:-

mintty --hold always -e g++ sample.cpp -o sample.exe

# One, more TIP for windows user( Optional for Linux Users)
# Sometimes you might want to supply some input to your program.
# There are actually two ways, first, You are going to type each and every required value, but for sublime, let me tell you one thing, Sublime-Text-3 console is not Interactive, means it will not ask you for any input, this just runs and show you the output.
# BUT you have second option, When you want to automate your compiled program, OK, we are going to write every required query for proper functioning of our program in a separate text file and tell Sublime-Text-3 to take input data from this file Instead of STDIN FILE.


"shell_cmd": "g++ -std=c++11 \"${file}\" -o \"${file_path}/${file_base_name}.exe\"", 
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$", 
"working_dir": "${file_path}", 
"selector": "source.c, source.c++, source.cpp", 

"variants": 


"name": "Run", 
"shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\" -std=c++11 && mintty --hold always -e \"${file_path}/${file_base_name}.exe\"" 


Sublime text 3编译环境配置C++11

# Take a look :-

Sublime text 3编译环境配置C++11


注意关于上文中提到的xterm终端模拟器,系统不一定默认安装了,首先需要通过命令按转,比如在Centos系统下通过 yum install xterm安装。在xterm中还可以在运行C++程序时输入。


本文永久更新地址:http://www.linuxdiyf.com/linux/21941.html